|
|
Title | Simulate drawing with the mouse in Visual Basic 2005 |
Description | This example shows how to simulate drawing with the mouse in Visual Basic 2005 by using the OnMouseDown, OnMouseMove, and OnMouseUp methods. |
Keywords | VB2005, Visual Basic 2005, mouse move, click, OnMouseDown, OnMouseMove, OnMouseUp |
Categories | API, VB.NET, Tips and Tricks |
|
|
This technique shows how you can use the OnMouseDown, OnMouseMove, and OnMouseUp methods to simulate mouse actions. This method can be helpful in testing graphicsal user interface code.
The program uses the following event handler to draw when you click and drag the mouse on the form. It starts drawing in the MouseDown event handler, continues drawing when it receives a MouseMove event, and stops drawing when it receives a MouseUp event.
|
|
Private m_Drawing As Boolean = False
Private m_LastPoint As Point = Nothing
' Start drawing.
Private Sub Form1_MouseDown(ByVal sender As Object, ByVal e _
As System.Windows.Forms.MouseEventArgs) Handles _
Me.MouseDown
m_Drawing = True
m_LastPoint = New Point(e.X, e.Y)
Dim gr As Graphics = Me.CreateGraphics()
gr.Clear(Me.BackColor)
End Sub
' Continue drawing.
Private Sub Form1_MouseMove(ByVal sender As Object, ByVal e _
As System.Windows.Forms.MouseEventArgs) Handles _
Me.MouseMove
If Not m_Drawing Then Exit Sub
Dim gr As Graphics = Me.CreateGraphics()
gr.DrawLine(Pens.Blue, m_LastPoint, New Point(e.X, e.Y))
m_LastPoint = New Point(e.X, e.Y)
End Sub
' Stop drawing.
Private Sub Form1_MouseUp(ByVal sender As Object, ByVal e _
As System.Windows.Forms.MouseEventArgs) Handles _
Me.MouseUp
m_Drawing = False
End Sub
|
|
When you click the Simulated Draw button, the program uses the following code to simulate drawing with the mouse. It makes a MouseEventArgs object representing a moused down event and calls the form's OnMouseDown method to raise the MouseDown event. Then in a loop it similarly calls the OnMouseMove method to generate MouseMove events. Finally the program uses OnMouseUp to raise a MouseUp event.
|
|
' Simulate manual drawing by using mouse_event.
Private Sub btnSimulatedDraw_Click(ByVal sender As _
System.Object, ByVal e As System.EventArgs) Handles _
btnSimulatedDraw.Click
Dim cx As Integer = Me.ClientSize.Width \ 2
Dim cy As Integer = Me.ClientSize.Height \ 2
Dim radius As Integer = Math.Min(Me.ClientSize.Width, _
Me.ClientSize.Height) \ 3
' MouseDown.
Dim down_args As New MouseEventArgs( _
Windows.Forms.MouseButtons.Left, _
1, _
CInt(cx + radius * Cos(0)), _
CInt(cy + radius * Sin(0)), _
0)
Me.OnMouseDown(down_args)
' Move the mouse around in a randomized circle.
Dim rnd As New Random
For angle As Double = PI / 10 To 6 * PI Step PI / 10
Dim move_args As New MouseEventArgs( _
Windows.Forms.MouseButtons.Left, _
1, _
CInt(cx + radius * Cos(angle) + rnd.Next(-20, _
20)), _
CInt(cy + radius * Sin(angle) + rnd.Next(-20, _
20)), _
0)
Me.OnMouseMove(move_args)
Next angle
' MouseUp.
Dim up_args As New MouseEventArgs( _
Windows.Forms.MouseButtons.Left, _
1, _
CInt(cx + radius * Cos(0)), _
CInt(cy + radius * Sin(0)), _
0)
Me.OnMouseMove(up_args)
Me.OnMouseUp(up_args)
End Sub
|
|
This example was written in Visual Basic 2005 but you should be able to copy and paste the code to make it work in earlier versions of Visual Basic .NET.
|
|
|
|
|
|