如何检查“鼠标指针”是否可见

本文关键字:鼠标指针 是否 何检查 检查 | 更新日期: 2023-09-27 18:07:49

在c#中有办法吗?Net检查鼠标指针是否可见?(就像在Touch设备上一样)

或者它的符号类型?(指针,加载圆,隐藏)

如何检查“鼠标指针”是否可见

看看如何使用游标。当前

表示鼠标光标的游标。默认为空

比如

Cursor current = Cursor.Current;
if(current == null)
    //the cursor is not visible
else
    //the cursor is visible

我凭经验发现游标。Current == null是否not表示光标隐藏状态(Windows 10 Pro, . net 4.7, Windows. net)。形式,2020.04.07)。

为了澄清这个问题,我想检查(而不是设置)光标隐藏状态,因为这似乎是可靠地检测鼠标事件是由鼠标/触摸板(光标可见)还是通过手指触摸(光标不可见)引发的唯一方法。

进入Win32调用允许成功检查此状态:

#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

客户端代码现在非常方便:

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

根据MSDN:

属性值类型:System.Windows.Forms.Cursor表示鼠标光标的光标。如果鼠标光标不可见,则默认为空。

所以这段代码应该完成工作:

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

您可以使用System.Windows.Forms.Cursor类来获取信息;

使用Cursor.Current属性!

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

如果你谈论的是WPF的变体,那么光标属性的框架元素应该是None,如果它是不可见的