1 / 24

VisualBasic7

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:

paul
Télécharger la présentation

VisualBasic7

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. VisualBasic7 Mouse and key events Some graphics

  2. Index of projects • Mouse draw (dots) • Mouse draw: two color dots • Mousedraw with a draw panel, color and size radiobuttons • KeyEvents

  3. Mouse draw

  4. In the code view:

  5. 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

  6. Mouseclick (on the form)…for example

  7. For a draw dot on mousedown project • We select mousedown event on the form • We define a colored circle (size)

  8. Adding the mousedown event handler

  9. 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

  10. 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.

  11. Now, add a MouseUp event handler

  12. 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

  13. Should mouse draw? Private Const blobsize As Integer = 8 Private graphicsobj As Graphics = CreateGraphics() Private ShouldPaint As Boolean = False

  14. 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

  15. 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

  16. A sort of picture

  17. 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

  18. One of your choices for this week: build this

  19. Clear the panel • Put a button on the form to clear the image • Use panel.refresh() to clear the panel

  20. 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.

  21. Be sure to add the mouse events to the panel, not the form

  22. Keyprocessing

  23. The keypressed event

  24. 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.

More Related