我试图了解鼠标光标是否在桌面屏幕上。这是我的代码:
[DllImport("user32.dll")]
public static extern bool GetCursorPos(out Point lpPoint);
[DllImport("user32.dll")]
public static extern IntPtr WindowFromPoint(Point Point);
[DllImport("user32.dll", SetLastError = false)]
public static extern IntPtr GetDesktopWindow();
public static bool IsMouseOverDesktop()
{
Point mouseCursor;
GetCursorPos(out mouseCursor);
return WindowFromPoint(mouseCursor) == GetDesktopWindow();
}
但它不起作用。当鼠标光标在桌面上时,WindowFromPoint和GetDesktopWindow返回不同的值。
答案 0 :(得分:1)
现在有了我的想法,你可以像这样解决你的问题:
在表单代码中使用此方法。
public bool IsMouseOverDesktop()
{
bool IsMouseOverDesktop = false;
if ((Cursor.Position.X > this.Location.X) && ((Cursor.Position.X - this.Location.X) < this.Width) && (Cursor.Position.Y > this.Location.Y) && ((Cursor.Position.Y - this.Location.Y) < this.Height))
IsMouseOverDesktop = false;
else
IsMouseOverDesktop = true;
return IsMouseOverDesktop;
}
然后在一个事件中调用此方法,并检查鼠标是否在桌面上。