.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Code Snippets >> Collections >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

Draw Rectangle On Mouse Down Event

Posted By:Ghanashyam Nayak       Posted Date: December 10, 2010    Points: 15    Category: Collections    URL: http://www.dotnetspark.com  

Draw Rectangle When Mouse Click & Up To There Where Mouse Click Up...
 


Public Sub MyMouseDown(ByVal sender As Object, ByVal e As MouseEventArgs) Handles MyBase.MouseDown
        IsMouseDown = True
        StartPoint.X = e.X
        StartPoint.Y = e.Y
        EndPoint.X = -1
        EndPoint.Y = -1
    End Sub

    Private Sub DrawRectangel(ByVal point1 As Point, ByVal point2 As Point)
        Dim MyRect As Rectangle

        point1 = PointToScreen(point1)
        point2 = PointToScreen(point2)

        If point1.X < point2.X Then
            MyRect.X = point1.X
            MyRect.Width = point2.X - point1.X
        Else
            MyRect.X = point2.X
            MyRect.Width = point1.X - point2.X
        End If

        If point1.Y < point2.Y Then
            MyRect.Y = point1.Y
            MyRect.Height = point2.Y - point1.Y
        Else
            MyRect.Y = point2.Y
            MyRect.Height = point1.Y - point2.Y
        End If

        ControlPaint.DrawReversibleFrame(MyRect, Color.Yellow, FrameStyle.Thick)

    End Sub

    Public Sub MyMouseUp(ByVal sender As Object, ByVal e As MouseEventArgs) Handles Me.MouseUp

        IsMouseDown = False

        If EndPoint.X <> -1 Then
            Dim currentPoint As New Point(e.X, e.Y)
            DrawRectangel(StartPoint, EndPoint)
        End If

        Using RectGraphics As Graphics = Me.CreateGraphics()
            RectGraphics.FillRectangle(Brushes.Red, StartPoint.X, StartPoint.Y, (EndPoint.X - StartPoint.X), (EndPoint.Y - StartPoint.Y))
        End Using

        EndPoint.X = -1
        EndPoint.Y = -1
        StartPoint.X = -1
        StartPoint.Y = -1
    End Sub

    Public Sub MyMouseMove(ByVal sender As Object, ByVal e As MouseEventArgs) Handles Me.MouseMove

        Dim CurrentPoint As New Point(e.X, e.Y)

        If IsMouseDown Then
            If EndPoint.X <> -1 Then
                DrawRectangel(StartPoint, EndPoint)
            End If

            EndPoint = CurrentPoint
            DrawRectangel(StartPoint, CurrentPoint)
        End If

    End Sub

    Protected Overrides Sub OnLoad(ByVal e As EventArgs)
        IsMouseDown = False
    End Sub


     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend