Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何检查鼠标指针是否可见?_C#_.net - Fatal编程技术网

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

C# 如何检查鼠标指针是否可见?,c#,.net,C#,.net,在C#.Net中是否有方法检查鼠标指针是否可见? (例如,在触摸设备上) 还是它的符号类型? (指针,加载圆,隐藏)看看如何使用 表示鼠标光标的光标如果出现以下情况,则默认值为null 鼠标光标不可见。 大概是 Cursor current = Cursor.Current; if(current == null) //the cursor is not visible else //the cursor is visible 财产价值 类型:System.Windows.

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

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

看看如何使用

表示鼠标光标的光标如果出现以下情况,则默认值为null 鼠标光标不可见。

大概是

Cursor current = Cursor.Current;

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

财产价值 类型:System.Windows.Forms.Cursor 表示鼠标光标的光标。如果鼠标光标不可见,则默认值为null

因此,这段代码应该完成以下工作:

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

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

使用
Cursor.Current
属性

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

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

我根据经验发现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

WPF、WinForm、Silverlight/Store、HTML,哪个?一个“等待”光标是可见的。
bool isTouch = !Cursor.Current.IsVisible();