Click here to Skip to main content
15,897,718 members
Articles / Programming Languages / Visual Basic

Changing the Systems Cursor in VB.NET

Rate me:
Please Sign up or sign in to vote.
4.15/5 (8 votes)
15 Jul 2008CPOL 60.1K   1.2K   16  
Replace a cursor system-wide and restore it to the original cursor.
'Dan Dombrowski
'July 2008

Public Class Form1

    'API declarations
    Private Declare Function SetSystemCursor Lib "user32.dll" (ByVal hCursor As IntPtr, ByVal id As Integer) As Boolean
    Private Declare Function LoadCursorFromFile Lib "user32.dll" Alias "LoadCursorFromFileA" (ByVal lpFileName As String) As IntPtr

    'Cursor constants
    Private Const IDC_APPSTARTING As UInt32 = 32650
    Private Const IDC_ARROW As UInt32 = 32512
    Private Const IDC_HAND As UInt32 = 32649
    Private Const IDC_CROSS As UInt32 = 32515
    Private Const IDC_HELP As UInt32 = 32651
    Private Const IDC_IBEAM As UInt32 = 32513
    Private Const IDC_NO As UInt32 = 32648
    Private Const IDC_SIZEALL As UInt32 = 32646
    Private Const IDC_SIZENESW As UInt32 = 32643
    Private Const IDC_SIZENS As UInt32 = 32645
    Private Const IDC_SIZENWSE As UInt32 = 32642
    Private Const IDC_SIZEWE As UInt32 = 32644
    Private Const IDC_UP As UInt32 = 32516
    Private Const IDC_WAIT As UInt32 = 32514

    '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
End Class

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

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


Written By
Software Developer 12:34 MicroTechnologies, Inc.
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions