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

Top 5 Contributors of the Month
Lee Hnetinka

Post New Resource Bookmark and Share   

Draw Rectangle On Mouse Down Event

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

Draw Rectangle, Where The Mouse Down Start From There & Then When Mouse Up It Will Draw Up To That Rectangle...

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
            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
            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:


No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply

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