窗口相对屏幕的位置,你有窗口的句柄还是自己的窗体? 如果不是自己写的窗体,是获得的窗口句柄,可以用GetWindowRect。 如果是自己的窗体。这到容易 Rectangle rect = new Rectangle(窗体对象.Left,窗体对象.Top,窗体对象.Width,窗体对象.Height);
算了。估计你也看不明白如何实现,写个例子给你好了。[System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "GetForegroundWindow")] public static extern IntPtr GetForegroundWindow(); [System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "GetWindowRect")] public static extern int GetWindowRect(IntPtr hwnd, ref System.Drawing.Rectangle lpRect); private static void TestGetForegroundWindowRectangle() { IntPtr hForeground = GetForegroundWindow(); System.Drawing.Rectangle rect = new System.Drawing.Rectangle(); GetWindowRect(hForeground, ref rect); //这里的rect就是你你要的 }
如果不是自己写的窗体,是获得的窗口句柄,可以用GetWindowRect。
如果是自己的窗体。这到容易
Rectangle rect = new Rectangle(窗体对象.Left,窗体对象.Top,窗体对象.Width,窗体对象.Height);
public static extern IntPtr GetForegroundWindow();
[System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "GetWindowRect")]
public static extern int GetWindowRect(IntPtr hwnd, ref System.Drawing.Rectangle lpRect);
private static void TestGetForegroundWindowRectangle()
{
IntPtr hForeground = GetForegroundWindow();
System.Drawing.Rectangle rect = new System.Drawing.Rectangle();
GetWindowRect(hForeground, ref rect);
//这里的rect就是你你要的
}
用API也可的。楼上给出了。
你真知道?你知道还问?GetForegroundWindow是获得当前操作系统被激活的窗体句柄
GetWindowRect是获得窗体的位置这不是你要的?如果不是,那显然你没理解你要的是什么,如果是想说鼠标悬停位置的窗体。你就要遍历所有当前窗体,获得rect,然后看鼠标落在谁的范围内。