240 likes | 612 Views
VisualBasic7 Mouse and key events Some graphics Index of projects Mouse draw (dots) Mouse draw: two color dots Mousedraw with a draw panel, color and size radiobuttons KeyEvents Mouse draw In the code view: In the code view:
E N D
VisualBasic7 Mouse and key events Some graphics
Index of projects • Mouse draw (dots) • Mouse draw: two color dots • Mousedraw with a draw panel, color and size radiobuttons • KeyEvents
In the code view: • Under the menu bar icons and the view tabs there are two pull-down selectors. • Select events for the component you want to add an event to in the left pull-down menu • Select the event you want in the right pull-down menu. • This will paste in a sub template
For a draw dot on mousedown project • We select mousedown event on the form • We define a colored circle (size)
Field declarations Private Const blobsize As Integer = 8 Private graphicsobj As Graphics = CreateGraphics() Notice that we did several things: • Declared constants whose values may not be changed • Initialized values at declaration time
Mousedown event code- remember about putting statements on one line Private Sub frmMouseDraw_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseDown graphicsobj.FillEllipse(New SolidBrush(Color.BlueViolet), e.X, e.Y, blobsize, blobsize) End Sub Notice that e.x and e.y (mouseeventargs.x and mouseeventargs.y) return the current mouse position.
MouseUp event handler Private Sub frmMouseDraw_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseUp graphicsobj.FillEllipse(New SolidBrush(Color.Green), e.X, e.Y, blobsize, blobsize) End Sub
Should mouse draw? Private Const blobsize As Integer = 8 Private graphicsobj As Graphics = CreateGraphics() Private ShouldPaint As Boolean = False
New MouseUp event handler Private Sub frmMouseDraw_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseUp shouldpaint=false End Sub
Add mouse move event handler and adjust your other events Private Sub frmMouseDraw_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseDown ShouldPaint = True ‘paint when mouse down End Sub Private Sub frmMouseDraw_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseUp ShouldPaint = False ‘don’t paint for mouse up End Sub Private Sub frmMouseDraw_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseMove If ShouldPaint Then graphicsobj.FillEllipse(New SolidBrush(Color.BlueViolet), e.X, e.Y, blobsize, blobsize) End If End Sub
Distinguishing mouse buttons inside your event handler Suppose the mouseeventarg is called e (the default name VB gives it): If e.button=mousebuttons.left then ‘handle left button stuff Else if e.button =mousebuttons.right then ‘handle right button End if
Clear the panel • Put a button on the form to clear the image • Use panel.refresh() to clear the panel
Some notes on mouse painter • It uses a panel, and you’ll have to use panelname.creategraphics() to get the correct graphics object. • Similarly, you’ll have to define the mouseUp, mouseDown and MouseMove events associated with the panel, not the form.
Keypress • Keypress, keyup and keydown can be added to your form’s events • Event.KeyChar returns the keypress value. • In this example, it is simply appended to a String and displayed. • The clear button sets the string to empty and gives the textbox the focus. • The keypress event appends a char to message and displays it in the textbox.