我怎样才能知道,如果鼠标在顶层窗口? [英] How can I tell if the mouse is over a top-level window?

查看:119
本文介绍了我怎样才能知道,如果鼠标在顶层窗口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何才能有效地辨别鼠标在一个顶层窗口?

How can I efficiently tell if the mouse is over a top-level window?

通过过,我的意思是鼠标指针是客户端矩形内顶层窗口的有在鼠标指针的位置在我的窗口没有其他顶层窗口。换句话说,如果用户点击该事件将被发送到我的顶层窗口(或者其子窗口之一)。

By "over", I mean that the mouse pointer is within the client rectangle of the top-level window and there is no other top-level window over my window at the location of the mouse pointer. In other words, if the user clicked the event would be sent to my top-level window (or one of its child windows).

我使用Windows写在C#形式,但我不介意使用p / Invoke使Win32调用。

I am writing in C# using Windows Forms, but I don't mind using p/invoke to make Win32 calls.

推荐答案

您可以使用WinAPI的函数<一个HREF =http://msdn.microsoft.com/en-us/library/ms633558%28VS.85%29.aspx相对=nofollow> WindowFromPoint 。它的C#签名是:

You could use the WinAPI function WindowFromPoint. Its C# signature is:

[DllImport("user32.dll")]
static extern IntPtr WindowFromPoint(POINT Point);



注意 POINT 这里是不一样的为 System.Drawing.Point ,但提供的PInvoke的 POINT ,其中包括两个之间的隐式转换。声明

Note that POINT here is not the same as System.Drawing.Point, but PInvoke provides a declaration for POINT that includes an implicit conversion between the two.

如果你不知道的鼠标光标位置, GetCursorPos 发现它:

If you don’t already know the mouse cursor position, GetCursorPos finds it:

[DllImport("user32.dll")]
static extern bool GetCursorPos(out POINT lpPoint);



不过,WinAPI的调用很多事情窗口:控件的窗口内也窗口 。因此,你可能无法得到顶级的窗口的在直观的感觉(你可能会得到一个单选按钮,面板或其它东西)。你可以反复应用 的getParent 功能走到GUI层次结构:

However, the WinAPI calls lots of things "windows": controls inside a window are also "windows". Therefore, you might not get a top-level window in the intuitive sense (you might get a radio button, panel, or something else). You could iteratively apply the GetParent function to walk up the GUI hierarchy:

[DllImport("user32.dll", ExactSpelling=true, CharSet=CharSet.Auto)]
public static extern IntPtr GetParent(IntPtr hWnd);



一旦你发现没有父窗口,该窗口将是一个顶层窗口。既然你在最初传递点属于未覆盖另一个窗口控制,对顶层窗口必然是一个点所属。

Once you find a window with no parent, that window will be a top-level window. Since the point you originally passed in belongs to a control that is not covered by another window, the top-level window is necessarily the one the point belongs to.

这篇关于我怎样才能知道,如果鼠标在顶层窗口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆