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

时间:2013-04-07 16:51:13

标签: c# .net

C#.Net中是否有办法检查鼠标指针是否可见? (例如,在Touch设备上)

还是它的符号类型? (指针,加载圈,隐藏)

5 个答案:

答案 0 :(得分:5)

查看使用Cursor.Current

  

表示鼠标光标的光标。 如果是,则默认值为null   鼠标光标不可见。

类似

Cursor current = Cursor.Current;

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

答案 1 :(得分:2)

According to MSDN :

  

物业价值   键入:System.Windows.Forms.Cursor   一个代表鼠标光标的光标。如果鼠标光标不可见,则默认值为null。

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

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

答案 2 :(得分:1)

您可以使用System.Windows.Forms.Cursor课程获取信息;

使用Cursor.Current属性!

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

答案 3 :(得分:1)

我凭经验发现 Cursor.Current == null 确实指示光标隐藏状态(Windows 10 Pro,.Net 4.7,Windows.Forms,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();

答案 4 :(得分:0)

如果您正在讨论WPF变体,那么框架元素的Cursor属性应该是None,如果它不可见。