Mouse Move Paint using VB.NET

In this article, we will create a program that paints the form using the mouse movements through clicking. Now, let's start this tutorial! 1. Let's start with creating a Windows Form Application for this tutorial by following the following steps in Microsoft Visual Studio: Go to File, click New Project, and choose Windows Application. 2. You don't have to design your interface because we will only focus on the mouse event. 3. Now put this code for your code module.
  1. Public Class Form1
  2.  
  3.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  4.         Cursor = Cursors.Hand
  5.     End Sub
  6.  
  7.     Dim mustPaint As Boolean = False
  8.  
  9.     Private Sub MouseEvent_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseDown
  10.         mustPaint = True
  11.     End Sub
  12.  
  13.     Private Sub MouseEvent_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseMove
  14.         If mustPaint Then
  15.             Dim graphic As Graphics = CreateGraphics()
  16.             graphic.FillEllipse(New SolidBrush(Color.Green), e.X, e.Y, 10, 5)
  17.         End If
  18.     End Sub
  19.  
  20.     Private Sub MouseEvent_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseUp
  21.         mustPaint = False
  22.     End Sub
  23. End Class
We used the selected mouse event functions of vb.net such as the MouseMove, MouseUp, and MouseDown. This event will function as we have the motion of the mouse and when we click the mouse. We have created first a variable mustPaint As Boolean that is equal to False, which means to say when the first run of the program, even if we hover the mouse to the form it will not paint any shapes. In MouseMove Event, If mustPaint is true then we have initialized variable graphics as Graphics. This Graphics class delivers methods for drawing objects to the display form. Our graphics variable has the FillEllipse Method which fills the inner of an ellipse defined by a rectangle specified by a pair of coordinates (x and y in our program), a width (that is 10), and a height (5). The SolidBrush Class defines a brush of a single color in which we use Green color. In the Form_Load, we initialized that our cursor is a Hand Cursor when making a paint to the form.

Output:

output Download the source code below and try it! :) For more inquiries and need programmer for your thesis systems in any kind of programming languages, just contact my number below. Best Regards, Engr. Lyndon Bermoy IT Instructor/System Developer/Android Developer/Freelance Programmer Mobile: 09488225971 Landline: 826-9296 E-mail:[email protected] Add and Follow me on Facebook: https://www.facebook.com/donzzsky Visit and like my page on Facebook at: https://www.facebook.com/BermzISware

Add new comment