Introduction
This article explains the MouseMove Event and how to draw various types of shapes in a Windows Forms application using the MouseMove Event. The MouseMove event is raised on an event of the specified selected control.
MouseMove Event
The MouseMove Event is used when the mouse pointer is moved to a selected area of the control or an event occurs when the mouse pointer moves inside the element.
MouseMove Event Members
- X: The X-CORDINATE of the mouse click.
- Y: The Y-CORDINATE of the mouse click.
- Button: Indicates which mouse button was pressed.
- Delta: Indicates a signed count of the number of detents the mouse wheel has rotated.
- Clicks: Indicates the number of times the mouse button was pressed and released.
Now I will show you how to use the MouseMove Event in a Windows Forms application. Use the following procedure to create a sample of that.
Step 1
Open Visual Studio then select "Create New Project" -->"F# Console Application".
Step 2
Now go to the Solution Explorer, on the right side of the application. Right-click on "References" and select "Add references".
Step 3
After selecting "Add References", in the framework template you need to select "System.Windows.Forms" and System.Drawing while holding down the Ctrl key and click on "Ok".
Step 4
Write the following code for moving the mouse in an Arc shape in the F# editor.
open System
open System.Windows.Forms
open System.Drawing
let drawarcform = new Form(Text="Draw Arc MouseMoves")
drawarcform.BackColor<-Color.Gray
let exitbutton=new Button(Top=200,Left=200)
exitbutton.Text<-"Exit"
exitbutton.BackColor<-Color.Ivory
let crgrphics=drawarcform.CreateGraphics()
drawarcform.Controls.Add(exitbutton)
drawarcform.MouseMove.Add(fun move->crgrphics.DrawArc(Pens.Violet,new Rectangle(move.X,move.Y,15,15),200.0f,-200.0f))
exitbutton.Click.Add(fun close->drawarcform.Close())
Application.Run(drawarcform)
Step 5
Debug the application by pressing F5 to execute the Windows Forms application. After debugging the application the output will be as in the following figure:
Step 6
Write the following code for moving the mouse in a Rectangle shape in the F# editor.
open System
open System.Windows.Forms
open System.Drawing
let drawrectangleform = new Form(Text="Draw Rectangle MouseMoves")
drawrectangleform.BackColor<-Color.Gray
let exitbutton=new Button(Top=200,Left=200)
exitbutton.Text<-"Exit"
exitbutton.BackColor<-Color.Ivory
let crgraphics=drawrectangleform.CreateGraphics()
drawrectangleform.Controls.Add(exitbutton)
drawrectangleform.MouseMove.Add(fun move->crgraphics.DrawRectangle(Pens.DarkBlue,new Rectangle(move.X,move.Y,10,10)))
exitbutton.Click.Add(fun close>drawrectangleform.Close())
Application.Run(drawrectangleform)
Step 7
Debug the application by pressing F5 to execute the Windows Forms application. After debugging the application the output will be as in the following figure:
Step 8
Write the following code to move the mouse in an Ellipse shape in the F# editor.
open System
open System.Windows.Forms
open System.Drawing
let drawellipseform = new Form(Text="Draw Ellipse MouseMoves")
drawellipseform.BackColor<-Color.Gray
let exitbutton=new Button(Top=200,Left=200)
exitbutton.Text<-"Exit"
exitbutton.BackColor<-Color.Ivory
let crgraphics=drawellipseform.CreateGraphics()
drawellipseform.Controls.Add(exitbutton)
drawellipseform.MouseMove.Add(fun move->crgraphics.DrawEllipse(Pens.Black,new Rectangle(move.X,move.Y,15,15)))
exitbutton.Click.Add(fun close->drawellipseform.Close())
Application.Run(drawellipseform)
Step 9
Debug the application by pressing F5 to execute the Windows Forms application. After debugging the application the output will be as in the following figure:
Step 10
Write the following code to move the mouse in a DrawLine shape in the F# editor.
open System
open System.Windows.Forms
open System.Drawing
let drawlineform = new Form(Text="DrawLines MouseMoves")
drawlineform.BackColor<-Color.Gray
let exitbutton=new Button(Top=200,Left=200)
exitbutton.Text<-"Exit"
exitbutton.BackColor<-Color.Ivory
let crgraphics=drawlineform.CreateGraphics()
drawlineform.Controls.Add(exitbutton)
drawlineform.MouseMove.Add(fun move->gr.DrawLine(Pens.Red,move.X,move.Y,move.X+5,move.Y+5))
exitbutton.Click.Add(fun close->drawlineform.Close())
Application.Run(drawlineform)
Step 11
Debug the application by pressing F5 to execute the Windows Forms application. After debugging the application the output will be as in the following figure:
Step 12
Write the following code to move the mouse in a FillArc shape in the F# editor.
open System
open System.Windows.Forms
open System.Drawing
let drawfillpieform = new Form(Text="Draw FilPie MouseMouse")
drawfillpieform.BackColor<-Color.Gray
let exitbutton=new Button(Top=200,Left=200)
exitbutton.Text<-"Exit"
exitbutton.BackColor<-Color.Ivory
let crgraphics=drawfillpieform.CreateGraphics()
drawfillpieform.Controls.Add(exitbutton)
drawfillpieform.MouseMove.Add(fun move->crgraphics.FillPie(Brushes.MidnightBlue,new Rectangle(move.X,move.Y,15,15),170.0f,170.0f))
exitbutton.Click.Add(fun close->drawfillpieform.Close())
Application.Run(drawfillpieform)
Step 13
Debug the application by pressing F5 to execute the Windows Forms application. After debugging the application the output will be as in the following figure:
Step 14
Write the following code to move the mouse in an FillEllipse shape in the F# editor.
open System
open System.Windows.Forms
open System.Drawing
let fillellipseform = new Form(Text="Draw FillEllipse MouseMove")
fillellipseform.BackColor<-Color.Gray
let exitbutton=new Button(Top=200,Left=200)
exitbutton.Text<-"Exit"
exitbutton.BackColor<-Color.Ivory
let crgraphics=fillellipseform.CreateGraphics()
fillellipseform.Controls.Add(exitbutton)
fillellipseform.MouseMove.Add(fun move->crgraphics.FillEllipse(Brushes.Red,new Rectangle(move.X,move.Y,15,15)))
exitbutton.Click.Add(fun close->fillellipseform.Close())
Application.Run(fillellipseform)
Step 15
Debug the application by pressing F5 to execute the Windows Forms application. After debugging the application the output will be as in the following figure:
Step 16
Write the following code to move the mouse in a FillRectangle shape in the F# editor.
open System
open System.Windows.Forms
open System.Drawing
let fillrecform = new Form(Text="Draw FillRectangle MouseMove")
fillrecform.BackColor<-Color.Gray
let exitbutton=new Button(Top=200,Left=200)
exitbutton.Text<-"Exit"
exitbutton.BackColor<-Color.Ivory
let crgraphics=fillrecform.CreateGraphics()
fillrecform.Controls.Add(exitbutton)
fillrecform.MouseMove.Add(fun move->crgraphics.FillRectangle(Brushes.Indigo,new Rectangle(move.X,move.Y,15,15)))
exitbutton.Click.Add(fun close->fillrecform.Close())
Application.Run(fillrecform)
Step 17
Debug the application by pressing F5 to execute the Windows Forms application. After debugging the application the output will be as in the following figure:
Summary
This article explained the MouseMove Event and how to draw various types of shapes and fill the shapes in a Windows Forms application using the MouseMove Event.