Search code examples
vb.netwinformscad

Create dyamically re-sizable line\shape winforms


I am new to programming. Can anybody help me with creating line/shape in picturebox with grips on the line/shape. Like we do it in CAD softwares.

And i want to know how to create a line on mouse click until another mouse click event occurs. enter image description here

Public Class Form1

Dim isDrag As Boolean = False
Dim theRectangle As New Rectangle(New Point(0, 0), New Size(0, 0))
Dim startPoint As Point
Dim IsDimension As Boolean = False
Dim LineLocationStPoint As Point = Nothing
Dim LineLocationEndPoint As Point = Nothing
Dim cnt As Integer = 0
Dim LineArray As New ArrayList
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Dim fd As OpenFileDialog = New OpenFileDialog()
    Dim strFileName As String

    fd.Title = "Open File Dialog"
    fd.Filter = "(*.PDF;*.DWG;*.TIFF;*.TIF)|*.PDF;*.DWG;*.TIFF;*.TIF|All files (*.*)|*.*"
    fd.FilterIndex = 2
    fd.RestoreDirectory = True

    If fd.ShowDialog() = DialogResult.OK Then
        strFileName = fd.FileName
        'ShowFileInWebBrowser(WebBrowser1, strFileName)
        PictureBox1.Load(strFileName)
    End If
End Sub
Private Sub Form1_MouseDown(ByVal sender As Object, ByVal e As  _
    System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown
    If (e.Button = MouseButtons.Right) Then
        isDrag = True
    End If

    Dim control As Control = CType(sender, Control)


    startPoint = control.PointToScreen(New Point(e.X, e.Y))

    If (e.Button = MouseButtons.Left) Then
        IsDimension = True
        LineLocationStPoint = e.Location
        LineArray.Add(e.Location)
    End If
End Sub

Private  Sub Form1_MouseMove(ByVal sender As Object, ByVal e As  _
System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove


    If (isDrag) Then

        ControlPaint.DrawReversibleFrame(theRectangle, Me.BackColor, _
            FrameStyle.Dashed)


         Dim endPoint As Point = CType(sender, Control).PointToScreen(New Point(e.X, e.Y))
         Dim width As Integer = endPoint.X - startPoint.X
         Dim height As Integer = endPoint.Y - startPoint.Y
         theRectangle = New Rectangle(startPoint.X, startPoint.Y, _
            width, height)

         ControlPaint.DrawReversibleFrame(theRectangle, Me.BackColor, _
             FrameStyle.Dashed)
    End If
    If IsDimension Then
        LineLocationEndPoint = e.Location
        Dim g As Graphics = PictureBox1.CreateGraphics()
        g.DrawLine(Pens.Red, LineLocationStPoint, e.Location)
        g.Dispose()
    End If
End Sub

Private Sub Form1_MouseUp(ByVal sender As Object, ByVal e As  _
System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseUp

    If IsDimension Then
        PictureBox1.Refresh()
    ElseIf isDrag Then
        ' If the MouseUp event occurs, the user is not dragging.
        isDrag = False

        ' Draw the rectangle to be evaluated. Set a dashed frame style 
        ' using the FrameStyle enumeration.
        ControlPaint.DrawReversibleFrame(theRectangle, Me.BackColor, _
            FrameStyle.Dashed)

        ' Find out which controls intersect the rectangle and change their color.
        ' The method uses the RectangleToScreen method to convert the 
        ' Control's client coordinates to screen coordinates.
        Dim i As Integer
        Dim controlRectangle As Rectangle
        For i = 0 To Controls.Count - 1
            controlRectangle = Controls(i).RectangleToScreen _
                (Controls(i).ClientRectangle)
            If controlRectangle.IntersectsWith(theRectangle) Then
                Controls(i).BackColor = Color.BurlyWood
            End If
        Next

        ' Reset the rectangle.
        theRectangle = New Rectangle(0, 0, 0, 0)
    End If       

End Sub

But it creates the line continuously from selected point. Whereas I want to create a line only for showing user the path of line. And i have implemented selection rectangle an right click button

Procedure for working: Toolbar will contain line and Area

  • Open a file(image file)
  • click on line button of toolbar
  • *Come to picturebox
  • *click on one point of screen
  • *dynamic line starts drawing on screen (line will be from the 1st clicked point to where ever mouse mouse)
  • *when user clicks the next time the line is created.

  • click area of toolbar

  • *come back to picturebox

  • *operation same like line but when user clicks third point on picture box a shaded rectangle should appear.


Solution

  • http://www.vb-helper.com/howto_2005_line_control.html

    For those who want to implement line with resizing grip in it. For adding re sizable line you need to add a customized control of your own. Use this custom control and add/use it in form for further use. Thanks everyone for helping