6 Replies - 4990 Views - Last Post: 20 September 2012 - 08:23 PM Rate Topic: -----

#1 TopHat   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 10
  • Joined: 19-September 12

How to make line tool

Posted 20 September 2012 - 05:14 PM

Hey again,

I'm trying to make a form with a picture box that can draw lines like a line tool. I only need to draw them and clear them after like paint. I can't find to much info on this other then your typical basic brush and using inkcanvas which confused me and didnt help with lines.

My lines will be going in every direction so any help would be great. i didnt think it would be so hard to figure this out.
Is This A Good Question/Topic? 0
  • +

Replies To: How to make line tool

#2 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 14243
  • View blog
  • Posts: 57,137
  • Joined: 12-June 08

Re: How to make line tool

Posted 20 September 2012 - 05:25 PM

You will need to do a few things.. first track the points which means mouse clicks... keep the pair of 'point objects' in a collection.. then manipulate the form's paint event to draw these lines when the form is refreshed.. as for removing them you'll need to track a mouse click to see if it is ON an existing line and if so change it's color and be ready for the 'delete key' to be pushed to remove said start and stop point object pairs from your list.

You can eye ball a tutorial I did on how to manipulate the paint event.

http://www.dreaminco...bberband-shape/
Was This Post Helpful? 0
  • +
  • -

#3 TopHat   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 10
  • Joined: 19-September 12

Re: How to make line tool

Posted 20 September 2012 - 05:49 PM

I found a great example that will help me learn for future reference but im having one problem with it. the line draws but i cant see the line as its drawing so i cant see if my line will be perfect. Any ideas on how to make it visable while drawing?

    Public Class Form1
     
        'The lines that have been drawn but not saved.
        Private lines As New List(Of Line)
     
        'The start point of the line currently being drawn.
        Private start As Point
     
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            'Place a blank image in the PictureBox control.
            Me.PictureBox1.Image = New Bitmap(Me.PictureBox1.Width, Me.PictureBox1.Height)
        End Sub
     
        Private Sub PictureBox1_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown
            'Remember the point at which the line started.
            Me.start = e.Location
        End Sub
     
        Private Sub PictureBox1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseUp
            'Remember the point at which the line ended.
            Dim [end] As Point = e.Location
     
            'Add the new line to the list.
            Me.lines.Add(New Line(Me.start, [end]))
     
            Dim area As New Rectangle(Math.Min(Me.start.X, [end].X), _
                                      Math.Min(Me.start.Y, [end].Y), _
                                      Math.Abs(Me.start.X - [end].X), _
                                      Math.Abs(Me.start.Y - [end].Y))
     
            'Inflate the rectangle by 1 pixel in each direction to ensure every changed pixel will be redrawn.
            area.Inflate(1, 1)
     
            'Force the control to repaint so the new line is drawn.
            Me.PictureBox1.Invalidate(area)
            Me.PictureBox1.Update()
        End Sub
     
        Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint
            'Draw each line on the control.
            Me.DrawLines(e.Graphics)
        End Sub
     
        Private Sub Save()
            'Create a Graphics object from the Image in the PictureBox.
            Using g As Graphics = Graphics.FromImage(Me.PictureBox1.Image)
                'Draw each line on the image to make them permanent.
                Me.DrawLines(g)
            End Using
     
            'Clear the temporary lines that were just saved.
            Me.Clear()
        End Sub
     
        Private Sub Clear()
            'Clear all unsaved lines.
            Me.lines.Clear()
     
            'Force the control to repaint so the lines are removed.
            Me.PictureBox1.Refresh()
        End Sub
     
        Private Sub DrawLines(ByVal g As Graphics)
            For Each line As Line In Me.lines
                g.DrawLine(Pens.Black, line.Start, line.End)
            Next line
        End Sub
     
    End Class
     
    Public Class Line
     
        'The line's start point.
        Private _start As Point
     
        'The line's end point.
        Private _end As Point
     
        'The line's start point.
        Public Property Start() As Point
            Get
                Return Me._start
            End Get
            Set(ByVal value As Point)
                Me._start = value
            End Set
        End Property
     
        'The line's end point.
        Public Property [End]() As Point
            Get
                Return Me._end
            End Get
            Set(ByVal value As Point)
                Me._end = value
            End Set
        End Property
     
        Public Sub New()
            Me.New(Point.Empty, Point.Empty)
        End Sub
     
        Public Sub New(ByVal start As Point, ByVal [end] As Point)
            Me._start = start
            Me._end = [end]
        End Sub
     
    End Class


Was This Post Helpful? 1
  • +
  • -

#4 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 14243
  • View blog
  • Posts: 57,137
  • Joined: 12-June 08

Re: How to make line tool

Posted 20 September 2012 - 07:53 PM

Have a boolean bit that's for when you have clicked once and want to draw a line.. and if that state is true the mouse move refreshes the form's paint event but the line is drawn from the start click to the current mouse's position. . when you click the second time it flips this state off.
Was This Post Helpful? 0
  • +
  • -

#5 TopHat   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 10
  • Joined: 19-September 12

Re: How to make line tool

Posted 20 September 2012 - 08:03 PM

is there anything i can do?
Was This Post Helpful? 0
  • +
  • -

#6 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 14243
  • View blog
  • Posts: 57,137
  • Joined: 12-June 08

Re: How to make line tool

Posted 20 September 2012 - 08:07 PM

... ah yeah follow what I said?
Was This Post Helpful? 0
  • +
  • -

#7 TopHat   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 10
  • Joined: 19-September 12

Re: How to make line tool

Posted 20 September 2012 - 08:23 PM

sorry im new and im trying to understand that source but im sure ill understand it at one point.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1