Click here to Skip to main content
Click here to Skip to main content

Changing the Systems Cursor in VB.NET

By , 15 Jul 2008
 

Introduction

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.

Background

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 LoadCursor or 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:

'Variable to save current cursor
Dim SavedCursor As Icon

Private Sub Button1_Click(ByVal sender As System.Object, _
            ByVal e As System.EventArgs) Handles Button1.Click
    'Save cursor
    SavedCursor = Icon.FromHandle(Cursors.Arrow.CopyHandle)

    'Change arrow cursor to mine
    Dim NewCursor As IntPtr = _
        LoadCursorFromFile(Application.StartupPath & "\MyCross.cur")

    'Check
    If NewCursor = IntPtr.Zero Then
        'Error loading cursor from file
        Debug.WriteLine("Error loading cursor from file.")
        Return
    End If

    'Set the system cursor
    If SetSystemCursor(NewCursor, IDC_ARROW) = 0 Then
        'Error setting system cursor
        Debug.WriteLine("Error setting system cursor.")
        Return
    End If

    'Disable/enable buttons
    Button1.Enabled = False
    Button2.Enabled = True
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles Button2.Click
    'Get old cursor
    Dim OldCursor As IntPtr = SavedCursor.Handle

    'Set the system cursor
    SetSystemCursor(OldCursor, IDC_ARROW)

    'Disable/enable buttons
    Button1.Enabled = True
    Button2.Enabled = False
End Sub

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).

History

  • Release 1 (July 15th, 2008).

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Dan Dombrowski
Software Developer 12:34 MicroTechnologies, Inc.
United States United States
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionHow to use my own cursor?memberNicolaasP10 Nov '08 - 23:15 
Thanks Dan for the cool function.
 
If I want to use my own cursur, how do I register & get the cursor constants?
 
Thanks & Regards,
 
Nicolaas
YM: nclasps@yahoo.com
GM: nclasps@gmail.com
AnswerRe: How to use my own cursor?memberDan Dombrowski16 Nov '09 - 8:05 
Sorry I'm so late on this. But if you're still seeking a solution or someone else is searching for a similar solution. All you'd have to do is change the path of the cursor used in the sample project to that of your own.
AnswerWait Cursor Class in VB.NETmemberDougie the P22 Jul '08 - 8:43 
Useage:
 
Dim WCursor as New WaitCursor 'Sets cursor to wait cursor
 
'Do Something while cursor is wait cursor
 
WCursor.Dispose 'Restores prior cursor
 
Class Definition:
 
Public NotInheritable Class WaitCursor
Implements IDisposable
 
Private m_cursor As Cursor
 
Public Sub New()
 
m_cursor = Cursor.Current
Cursor.Current = Cursors.WaitCursor
 
End Sub
 
Public Sub Dispose() Implements IDisposable.Dispose
 
Dispose(True)
GC.SuppressFinalize(Me)
 
End Sub
 
Protected Overrides Sub Finalize()
 
Dispose(False)
 
End Sub
 
Private Sub Dispose(ByVal disposing As Boolean)
 
If (disposing) Then
If Not (m_cursor Is Nothing) Then
Cursor.Current = m_cursor
m_cursor = Nothing
End If
End If
 
End Sub
 
End Class
GeneralRe: Wait Cursor Class in VB.NETmemberDan Dombrowski16 Nov '09 - 8:06 
And what exactly does this class do?
GeneralRe: Wait Cursor Class in VB.NETmemberDougie the P16 Nov '09 - 8:16 
This provides a class to save the current cursor and when disposed resets the cursor back to the original or when the varible goes out of range.
 

Doug
GeneralRe: Wait Cursor Class in VB.NETmemberDan Dombrowski20 Nov '09 - 6:56 
Ah, I see it now...that could be helpful. Thanks!
GeneralRe: Wait Cursor Class in VB.NETmemberbhogsett6 Jan '10 - 10:25 
Thanks, this works very well.
 
Bill Smile | :)
GeneralCoding style, bravo!memberBkins15 Jul '08 - 15:18 
I want to comment on your coding style. I found it very refreshing. I too get help from this site quite a bit and see some .... less then desirable code styles. thank you for taking the time to make your code readable.
GeneralRe: Coding style, bravo!memberDan Dombrowski16 Nov '09 - 8:02 
Thanks so much!

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web01 | 2.6.130523.1 | Last Updated 15 Jul 2008
Article Copyright 2008 by Dan Dombrowski
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid