|Title||Make a silly program that tracks the mouse's current position with a pair of eyes in Visual Basic .NET|
|Description||This example shows how to make a silly program that tracks the mouse's current position with a pair of eyes in Visual Basic .NET.|
|Keywords||eyes, mouse, mouse cursor, current position, track, VB.NET|
|Categories||Miscellany, Graphics, Puzzles and Games|
The form contains a single Timer. Its Tick event handler uses the MousePosition method to see where the mouse is. If the position has changed since the last time this event handler ran, the code invalidates the form to force a redraw.
' The previous mouse location.
Private m_MousePos As Point = New Point(-1, -1)
' See if the mouse has moved.
Private Sub tmrLook_Tick(...) Handles tmrLook.Tick
' See if the cursor has moved.
Dim new_pos As Point = Me.MousePosition()
If new_pos.Equals(m_MousePos) Then Exit Sub
m_MousePos = new_pos
The form's Paint event handler draws two eyes pointing toward the mouse's current position. It uses the PointToClient method to convert the mouse's position to the form's coordinate system. It calculates the size for the eyes, determines their positions, and then calls subroutine DrawEye to draw the eyes.
' Draw the eyes.
Private Sub Form1_Paint(...) Handles MyBase.Paint
' Convert the cursor position into form units.
Dim local_pos As Point = Me.PointToClient(m_MousePos)
' Calculate the size of the eye.
Dim hgt As Integer = CInt(Me.ClientSize.Height * 0.9)
Dim wid As Integer = CInt(Me.ClientSize.Width * 0.45)
' Find the positions of the eyes.
Dim y As Integer = (Me.ClientSize.Height - hgt) \ 2
Dim x1 As Integer = CInt((Me.ClientSize.Width - wid * _
2) / 3)
Dim x2 As Integer = wid + 2 * x1
e.Graphics.SmoothingMode = _
' Draw the eye.
DrawEye(e.Graphics, local_pos, x1, y, wid, hgt)
DrawEye(e.Graphics, local_pos, x2, y, wid, hgt)
Subroutine DrawEye draws a white exterior for an eye. It then calculates a unit vector pointing from the eye's center towards the mouse's current position. It moves along this direction 1/4 of the way from the eye's center to its edge. The routine then draws the pupil centered at this position.
' Draw an eye here.
Private Sub DrawEye(ByVal gr As Graphics, ByVal local_pos _
As Point, ByVal x1 As Integer, ByVal y1 As Integer, _
ByVal wid As Integer, ByVal hgt As Integer)
' Draw the outside.
gr.FillEllipse(Brushes.White, x1, y1, wid, hgt)
gr.DrawEllipse(Pens.Black, x1, y1, wid, hgt)
' Find the center of the eye.
Dim cx As Integer = (x1 + wid \ 2)
Dim cy As Integer = (y1 + hgt \ 2)
' Get the difference to the mouse position.
Dim dx As Double = (local_pos.X - cx)
Dim dy As Double = (local_pos.Y - cy)
Dim dist As Double = Sqrt(dx * dx + dy * dy)
dx /= dist
dy /= dist
' This point is 1/4 of the way
' from the center to the edge of the eye.
Dim px As Double = cx + dx * wid / 4
Dim py As Double = cy + dy * hgt / 4
' Draw an ellipse 1/2 the size of the eye
' centered at (px, py).
gr.FillEllipse(Brushes.Blue, CInt(px - wid / 4), _
CInt(py - hgt / 4), wid \ 2, hgt \ 2)