' This class exposes an event that allows event handlers to set crosshairs.
Public Class CursorPictureBox
Inherits PictureBox
' This variable keeps track of whether the current cursor is a crosshair.
Dim Crosshair As Boolean
' The event where the magic will happen.
Public Event CheckCrosshair As EventHandler(Of CrossHairEventArgs)
' This is the important stuff:
Protected Overrides Sub OnMouseMove(ByVal e As MouseEventArgs)
' We are supposed to call the base method to make sure MouseMove events are raised.
' This is not significant.
MyBase.OnMouseMove(e)
' We create our new event args class. We initialize the CrossHair
' property to False. If any event handlers feel compelled to show a crosshair,
' they should call SetCrossHair, which will set the property to true.
Dim EventArgs As New CrossHairEventArgs(e, False)
' Raise the event so that handlers can do their thing.
RaiseEvent CheckCrosshair(Me, EventArgs)
' Now, if our new Crosshair value is different than the old one
' then change the cursor to the appropriate cursor.
If EventArgs.CrossHair <> Crosshair Then
If EventArgs.CrossHair Then
Cursor = Cursors.Cross
Else
Cursor = Cursors.Arrow
End If
End If
' Store the new Crosshair value.
Crosshair = EventArgs.CrossHair
End Sub
End Class
' This class extends the MouseEventArgs and allows a handler to specify that
' a crosshair should be used.
Public Class CrossHairEventArgs
Inherits MouseEventArgs
Dim _CrossHair As Boolean
Public ReadOnly Property CrossHair() As Boolean
Get
Return _CrossHair
End Get
End Property
'This is the method that handlers should call to indicate that a crosshair should be shown.
Public Sub SetCrossHair()
_CrossHair = True
End Sub
Public Sub New(ByVal e As MouseEventArgs, ByVal CrossHair As Boolean)
MyBase.New(e.Button, e.Clicks, e.X, e.Y, e.Delta)
_CrossHair = CrossHair
End Sub
End Class