C# GetWindowRect简介及使用说明 |
本文标签:GetWindowRect 函数功能:该函数返回指定窗口的边框矩形的尺寸 。该尺寸以相对于屏幕坐标左上角的屏幕坐标给出 。 函数原型:BOOL GetWindowRect(HWND hWnd,LPRECTlpRect); 参数: hWnd:窗口句柄 。 lpRect:指向一个RECT结构的指针,该结构接收窗口的左上角和右下角的屏幕坐标 。 返回值:如果函数成功,返回值为非零:如果函数失败,返回值为零 。若想获得更多错误信息,请调用GetLastError函数 。 C#中使用该函数首先导入命名空间: 复制代码 代码如下: using System.Runtime.InteropServices; 然后写API引用部分的代码,放入 class 内部 复制代码 代码如下: [DllImport("user32.dll")] private static extern int GetWindowRect(IntPtr hwnd,out Rect lpRect); 这个函数有两个个参数,第一个参数是指定窗口句柄;第二个参数接收窗口的左上角和右下角的屏幕坐标,它是Rect结构 。Rect结构定义如下: 复制代码 代码如下: public struct Rect { public int Left; public int Top; public int Right; public int Bottom; } 演示代码: IntPtr hwnd = FindWindow("", "计算器"); Rect rect = new Rect(); GetWindowRect(hwnd, out lpRect); |