Yes, I do realize there have been many question like this, but none of them worked for me. I need to check whether the system cursor is hidden.
I have tried Cursor.Current == null
. It did not do anything, ever. ( I hid the cursor with fullscreen Youtube, Discord and Krita ).
I also tried using GetCursorInfo
from user32.dll and checking if the flag is 0 ( hidden ). Nothing.
[StructLayout( LayoutKind.Sequential )]
struct POINT {
public Int32 x;
public Int32 y;
}
[StructLayout( LayoutKind.Sequential )]
struct CURSORINFO {
public Int32 cbSize;
public Int32 flags;
public IntPtr hCursor;
public POINT ptScreenPos;
}
[DllImport( "user32.dll" )]
static extern bool GetCursorInfo ( out CURSORINFO pci );
public static bool CursorHidden () {
CURSORINFO cinfo;
cinfo.cbSize = Marshal.SizeOf( typeof( CURSORINFO ) );
GetCursorInfo( out cinfo );
return cinfo.flags == 0;
}
My goal is to hide a screen overlay UI when the cursor hides.
So, how do I check if the system cursor is hidden in any other way?
Edit:
Okay, I've made a discovery with the aid of Ahmed Abdelhameed. Basically, browsers and the like don't render the cursor, but its still considered visible by the system. However, the handle is changed. There is a limited amout of handles the system offers by default ( Cursors.<Name>.Handle
), and the invisible browser cursor uses none of them.
enum CursorTypes { ... }
...
CURSORINFO cinfo;
... // see code above
Cursor[] cursors = new Cursor[] { ... }; // listed all of them in the same order as CursorTypes
int type = 1;
foreach ( Cursor cursor in cursors ) {
if ( cinfo.hCursor == cursor.Handle ) {
return (CursorTypes)type;
}
type++;
}
return CursorTypes.Other;
This is not a complete sulution however because browsers and other software can create a new type of cursor other than invisible ( like tilted backwards in VS ) which will also return CursorTypes.Other
.
As Herohtar pointed out, hidden cursors usually have large pointer values ( In my case, Math.Abs( pointer ) > 10000000
worked most of the time ). That covers most browsers.
I honestly don't think there is much more we can do in terms of detecting a hidden cursor. In the rare case that a cusor is not hidden but my app thinks it is or the opposite, I decided to just have an option to let the user click a button in the settings that after a set amout of time adds the current cursor handle to exceptions.