EDN Admin
Well-known member
Hi,
I have a form in which i draw a rectangle and a line depending on the button click. I basically want a dynamic line/ rectangle that moves when the mouse is clicked on another point on the picturebox. I found some code online for a rectangle, and im trying
to extend it to draw a line too, but im having some issues. The line is not displayed when the mouse is lifted(mouse up event). Please help me with where im going wrong. Thanks!
Heres my code:
<pre class="prettyprint lang-vb Public Class Form1
Dim selection As Rectangle = Nothing
Dim m_Drawing As Boolean = False
Dim m_Start As Point
Dim square_click As Boolean = False
Dim line_click As Boolean = False
try
Dim points(1) As Point
Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown
Remember where mouse was pressed
m_Start = e.Location
Clear the selection rectangle
selection = Nothing
PictureBox1.Invalidate()
Selection drawing is on
m_Drawing = True
End Sub
Private Sub PictureBox1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove
If m_Drawing Then
If square_click = True Then
selection = RectangleFromPoints(m_Start, e.Location)
ElseIf line_click = True Then
points = LineFromPoints(m_Start, e.Location)
End If
Update selection
PictureBox1.Invalidate()
End If
End Sub
Private Function LineFromPoints(ByVal p1 As Point, ByVal p2 As Point) As Point()
Dim x As Integer
Dim y As Integer
Dim g As Graphics
Dim movepoint(1) As Point
g = PictureBox1.CreateGraphics
If p1.X <= p2.X Then
x = p1.X
Else
x = p2.X
End If
If p1.Y <= p2.Y Then
y = p1.Y
Else
y = p2.Y
End If
p1 = New Point(x, y)
movepoint(0) = p1
movepoint(1) = p2
g.DrawLine(New Pen(Color.White), p1, p2)
Return movepoint
End Function
Returns a rectangle from 2 points
Private Function RectangleFromPoints(ByVal p1 As Point, ByVal p2 As Point) As Rectangle
Dim x As Integer
Dim y As Integer
If p1.X <= p2.X Then
x = p1.X
Else
x = p2.X
End If
If p1.Y <= p2.Y Then
y = p1.Y
Else
y = p2.Y
End If
Return New Rectangle(x, y, Math.Abs(p1.X - p2.X), Math.Abs(p1.Y - p2.Y))
End Function
Private Sub PictureBox1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseUp
Stop drawing
m_Drawing = False
End Sub
Dim hbr As New SolidBrush(Color.FromArgb(128, Color.Yellow))
Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint
If selection <> Nothing Then
There is a selection rectangle so draw it
e.Graphics.FillRectangle(hbr, selection)
If square_click = True Then
e.Graphics.DrawRectangle(New Pen(Color.White), selection)
ElseIf line_click = True Then
do smtin
e.Graphics.DrawLine(New Pen(Color.White), points(0), points(1))
End If
End If
End Sub
Private Sub ToolStripButton1_Click(sender As System.Object, e As System.EventArgs) Handles ToolStripButton1.Click
If line_click = True Then
line_click = False
End If
square_click = True
End Sub
Private Sub ToolStripButton2_Click(sender As System.Object, e As System.EventArgs) Handles ToolStripButton2.Click
If square_click = True Then
square_click = False
End If
line_click = True
End Sub
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
ToolStripButton1.ToolTipText = "Draw Square"
ToolStripButton2.ToolTipText = "Draw Line"
ToolStripButton3.ToolTipText = "Move Motor"
End Sub
End Class
[/code]
<br/>
View the full article
I have a form in which i draw a rectangle and a line depending on the button click. I basically want a dynamic line/ rectangle that moves when the mouse is clicked on another point on the picturebox. I found some code online for a rectangle, and im trying
to extend it to draw a line too, but im having some issues. The line is not displayed when the mouse is lifted(mouse up event). Please help me with where im going wrong. Thanks!
Heres my code:
<pre class="prettyprint lang-vb Public Class Form1
Dim selection As Rectangle = Nothing
Dim m_Drawing As Boolean = False
Dim m_Start As Point
Dim square_click As Boolean = False
Dim line_click As Boolean = False
try
Dim points(1) As Point
Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown
Remember where mouse was pressed
m_Start = e.Location
Clear the selection rectangle
selection = Nothing
PictureBox1.Invalidate()
Selection drawing is on
m_Drawing = True
End Sub
Private Sub PictureBox1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove
If m_Drawing Then
If square_click = True Then
selection = RectangleFromPoints(m_Start, e.Location)
ElseIf line_click = True Then
points = LineFromPoints(m_Start, e.Location)
End If
Update selection
PictureBox1.Invalidate()
End If
End Sub
Private Function LineFromPoints(ByVal p1 As Point, ByVal p2 As Point) As Point()
Dim x As Integer
Dim y As Integer
Dim g As Graphics
Dim movepoint(1) As Point
g = PictureBox1.CreateGraphics
If p1.X <= p2.X Then
x = p1.X
Else
x = p2.X
End If
If p1.Y <= p2.Y Then
y = p1.Y
Else
y = p2.Y
End If
p1 = New Point(x, y)
movepoint(0) = p1
movepoint(1) = p2
g.DrawLine(New Pen(Color.White), p1, p2)
Return movepoint
End Function
Returns a rectangle from 2 points
Private Function RectangleFromPoints(ByVal p1 As Point, ByVal p2 As Point) As Rectangle
Dim x As Integer
Dim y As Integer
If p1.X <= p2.X Then
x = p1.X
Else
x = p2.X
End If
If p1.Y <= p2.Y Then
y = p1.Y
Else
y = p2.Y
End If
Return New Rectangle(x, y, Math.Abs(p1.X - p2.X), Math.Abs(p1.Y - p2.Y))
End Function
Private Sub PictureBox1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseUp
Stop drawing
m_Drawing = False
End Sub
Dim hbr As New SolidBrush(Color.FromArgb(128, Color.Yellow))
Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint
If selection <> Nothing Then
There is a selection rectangle so draw it
e.Graphics.FillRectangle(hbr, selection)
If square_click = True Then
e.Graphics.DrawRectangle(New Pen(Color.White), selection)
ElseIf line_click = True Then
do smtin
e.Graphics.DrawLine(New Pen(Color.White), points(0), points(1))
End If
End If
End Sub
Private Sub ToolStripButton1_Click(sender As System.Object, e As System.EventArgs) Handles ToolStripButton1.Click
If line_click = True Then
line_click = False
End If
square_click = True
End Sub
Private Sub ToolStripButton2_Click(sender As System.Object, e As System.EventArgs) Handles ToolStripButton2.Click
If square_click = True Then
square_click = False
End If
line_click = True
End Sub
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
ToolStripButton1.ToolTipText = "Draw Square"
ToolStripButton2.ToolTipText = "Draw Line"
ToolStripButton3.ToolTipText = "Move Motor"
End Sub
End Class
[/code]
<br/>
View the full article