Location>code7788 >text

NET Hide/Customize windows system cursor

Popularity:862 ℃/2024-10-22 16:31:02

This article describes how to operate windows system cursor. Normal we set/hide the cursor, can only change the scope of the current form or control, can not operate the windows cursor globally. Received a demand, want to hide windows global mouse cursor display, the following talk about how to do it

First understand the system mouse cursor, in the mouse properties - customization list can be seen in a total of 13 types, corresponding to 13 kinds of work status:

The operating system provides a set of predefined cursors, such as arrows, hands, hourglasses, etc., located in the C:\Windows\Cursors directory.

The corresponding enumeration:

 1   public enum CursorType
 2   {
 3     None,
 4     No,
 5     Arrow,
 6     AppStarting,
 7     Cross,
 8     Help,
 9     IBeam,
10     SizeAll,
11     SizeNESW,
12     SizeNS,
13     SizeNWSE,
14     SizeWE,
15     UpArrow,
16     Wait,
17     Hand,
18     Pen,
19     ScrollNS,
20     ScrollWE,
21     ScrollAll,
22     ScrollN,
23     ScrollS,
24     ScrollW,
25     ScrollE,
26     ScrollNW,
27     ScrollNE,
28     ScrollSW,
29     ScrollSE,
30     ArrowCD,
31   }

Cursor display logic:

  • Global Cursor Settings: Use the default system cursor on the desktop or in non-control areas.
  • Window control settings: Each window control can set its own cursor type. When the mouse is moved over the control, it will automatically switch to that set cursor. If not set then the system cursor is displayed
  • When the mouse moves, clicks, or performs other operations, the system detects and updates the cursor shape accordingly. Applications can also change the cursor for operations such as drag and drop.

If you need to get the current mouse status, you can get the current mouse cursor id and handle through GetCursorInfo:

1     private IntPtr GetCurrentCursor()
2     {
3         CURSORINFO cursorInfo;
4          = (typeof(CURSORINFO));
5         GetCursorInfo(out cursorInfo);
6         var cursorId = ;
7         var cursorHandle = CopyIcon(cursorId);
8         return cursorHandle;
9     }

So how do you hide the system cursor? The system cursor can be hidden with theSetSystemCursor function () - Win32 apps | Microsoft Learnfunction setup, but it doesn't seem to have an entry point for hiding the cursor

You can think differently and just create a blank cursor. I made one:Do-it-yourself windows mouse cursor file (.cur format) - CSDN Blogs

Then hide the system cursor:

1   private void HideCursor()
2   {
3       _cursorHandle = GetCurrentCursor();
4       //Replace with a blank mouse cursor
5       var cursorFile = (, "");
6       IntPtr cursor = LoadCursorFromFile(cursorFile);
7       SetSystemCursor(cursor, OcrNormal);
8  }

Restore the display of the system cursor by setting the previous cursor Handle back:

1 var success = SetSystemCursor(_cursorHandle, OcrNormal);

Above is the realization of the current cursor replacement. But above has introduced the mouse cursor state has 13 kinds, will switch according to the application state, so the other cursor should also be dealt with.

For all 13 cursors replaced with blank cursors, the 13 cursor CursorId values in thesetSystemCursorThe documentation has instructions:

 1     private readonly int[] _systemCursorIds = new int[] { 32512, 32513, 32514, 32515, 32516, 32642, 32643, 32644, 32645, 32646, 32648, 32649, 32650 };
 2     private readonly IntPtr[] _previousCursorHandles = new IntPtr[13];
 3     private void HideCursor()
 4     {
 5         for (int i = 0; i < _systemCursorIds.Length; i++)
 6         {
 7             var cursor = LoadCursor(, _systemCursorIds[i]);
 8             var cursorHandle = CopyIcon(cursor);
 9             _previousCursorHandles[i] = cursorHandle;
10             //Replace with a blank mouse cursor
11             var cursorFile = (, "");
12             IntPtr blankCursor = LoadCursorFromFile(cursorFile);
13             SetSystemCursor(blankCursor, (uint)_systemCursorIds[i]);
14         }
15     }

Run the validation:Cursor editing states on the system desktop, application forms such as VisualStudio, and web pages are successfully hidden.

Restore the cursor state:

1     private void ShowCursor()
2     {
3         for (int i = 0; i < _systemCursorIds.Length; i++)
4         {
5             SetSystemCursor(_previousCursorHandles[i], (uint)_systemCursorIds[i]);
6         }
7     }

The User32 and parameter classes used:

 1    [DllImport("", SetLastError = true)]
 2    public static extern IntPtr LoadCursor(IntPtr hInstance, int lpCursorName);
 3    [DllImport("")]
 4    public static extern IntPtr CopyIcon(IntPtr cusorId);
 5    [DllImport("")]
 6    public static extern IntPtr LoadCursorFromFile(string lpFileName);
 7    [DllImport("")]
 8    public static extern bool SetSystemCursor(IntPtr hcur, uint id);
 9    [DllImport("")]
10    static extern bool GetCursorInfo(out CURSORINFO pci);
11 
12    [StructLayout()]
13    public struct POINT
14    {
15        public Int32 x;
16        public Int32 y;
17    }
18 
19    [StructLayout()]
20    public struct CURSORINFO
21    {
22        public Int32 cbSize;        // Specifies the size, in bytes, of the structure. 
23                                    // The caller must set this to (typeof(CURSORINFO)).
24        public Int32 flags;         // Specifies the cursor state. This parameter can be one of the following values:
25                                    //    0             The cursor is hidden.
26                                    //    CURSOR_SHOWING    The cursor is showing.
27        public IntPtr hCursor;          // Handle to the cursor. 
28        public POINT ptScreenPos;       // A POINT structure that receives the screen coordinates of the cursor. 
29    }
View Code

It should be noted that the system cursor modification, please handle with caution, cursor modification after manual operation is not very easy to restore, the application exit, crash and other cases to do a good job of cursor recovery operations.

The above demo code is available at:kybs00/HideSystemCursorDemo: Hide windows system cursor () 

References:

- Your #1 source for using API-functions in Visual Basic! ()

createCursor function () - Win32 apps | Microsoft Learn

SetSystemCursor function () - Win32 apps | Microsoft Learn

Keywords: customize cursor, hide/show cursor, windows system cursor display