I have always come to this site for help, so it's time for me to give back. This is my first post here, so be gentle :P
The point of this article is to show you how to replace a cursor system-wide, and specifically, how to restore it to the original cursor once you're ready to do so.
Changing a system cursor was cake for me, but a problem came about when I wanted to restore that system cursor back to the original cursor. Many suggested to use the
LoadImage API to restore the cursor, but it seemed (for me at least) that neither API worked in VB.NET. So, on my own, I came up with the following solution.
Using the code
I'm not sure if this is the best way to do it, but it seems to work fairly well. The main code from the sample project is as follows:
Dim SavedCursor As Icon
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
SavedCursor = Icon.FromHandle(Cursors.Arrow.CopyHandle)
Dim NewCursor As IntPtr = _
LoadCursorFromFile(Application.StartupPath & "\MyCross.cur")
If NewCursor = IntPtr.Zero Then
Debug.WriteLine("Error loading cursor from file.")
If SetSystemCursor(NewCursor, IDC_ARROW) = 0 Then
Debug.WriteLine("Error setting system cursor.")
Button1.Enabled = False
Button2.Enabled = True
Private Sub Button2_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button2.Click
Dim OldCursor As IntPtr = SavedCursor.Handle
Button1.Enabled = True
Button2.Enabled = False
Points of Interest
This sample was written and tested in Visual Studio 2008 using .NET Framework 3.5. Any feedback would be great (especially if anyone has any improvements).
- Release 1 (July 15th, 2008).