Paul Kaap
Paul Kaap

Reputation: 31

How to check if Mouse Pointer is visible?

Is there a way within C#.Net to check if the mouse pointer is visible? (As it is on Touch devices for example)

Or the symbol type of it? (Pointer, Loading-circle, hidden)

Upvotes: 3

Views: 3855

Answers (5)

Ingmar
Ingmar

Reputation: 111

I found empirically that Cursor.Current == null does not indicate the cursor hidden status (Windows 10 Pro, .Net 4.7, Windows.Forms, 2020.04.07).

To clarify the problem, I want to check (not set) the cursor hidden status because that seems to be the only way to detect reliably if a mouse event was raised by mouse/touchpad (cursor is visible) or by finger touch (cursor not visible).

Diving into Win32 calls allows to check this state successfully:

#region Cursor info

public static class CursorExtensions {

  [StructLayout(LayoutKind.Sequential)]
  struct PointStruct {
    public Int32 x;
    public Int32 y;
  }

  [StructLayout(LayoutKind.Sequential)]
  struct CursorInfoStruct {
    /// <summary> The structure size in bytes that must be set via calling Marshal.SizeOf(typeof(CursorInfoStruct)).</summary>
    public Int32 cbSize;
    /// <summary> The cursor state: 0 == hidden, 1 == showing, 2 == suppressed (is supposed to be when finger touch is used, but in practice finger touch results in 0, not 2)</summary>
    public Int32 flags;
    /// <summary> A handle to the cursor. </summary>
    public IntPtr hCursor; 
    /// <summary> The cursor screen coordinates.</summary>
    public PointStruct pt; 
  }

  /// <summary> Must initialize cbSize</summary>
  [DllImport("user32.dll")]
  static extern bool GetCursorInfo(ref CursorInfoStruct pci);

  public static bool IsVisible(this Cursor cursor) {
    CursorInfoStruct pci = new CursorInfoStruct();
    pci.cbSize = Marshal.SizeOf(typeof(CursorInfoStruct));
    GetCursorInfo(ref pci);
    // const Int32 hidden = 0x00;
    const Int32 showing = 0x01;
    // const Int32 suppressed = 0x02;
    bool isVisible = ((pci.flags & showing) != 0);
    return isVisible;
  }

}

#endregion Cursor info

The client code is now quite convenient:

bool isTouch = !Cursor.Current.IsVisible();

Upvotes: 4

Peter Ritchie
Peter Ritchie

Reputation: 35869

If you're talking about a WPF variant, then the Cursor property of a framework element should be None if it's not visible.

Upvotes: 0

Parimal Raj
Parimal Raj

Reputation: 20595

You can use System.Windows.Forms.Cursor class to get the info;

Using Cursor.Current property!

if (Cursor.Current == null)
{
    //
}

Upvotes: 1

Harkonnen
Harkonnen

Reputation: 813

According to MSDN :

Property Value Type: System.Windows.Forms.Cursor A Cursor that represents the mouse cursor. The default is null if the mouse cursor is not visible.

So this code should do the job :

If (Cursor.Current == null)
{
    // cursor is invisible
}
else
{
    // cursor is visible
}

Upvotes: 2

Adriaan Stander
Adriaan Stander

Reputation: 166576

Have a look at using Cursor.Current

A Cursor that represents the mouse cursor. The default is null if the mouse cursor is not visible.

So something like

Cursor current = Cursor.Current;

if(current == null)
    //the cursor is not visible
else
    //the cursor is visible

Upvotes: 4

Related Questions