当前位置:Gxlcms > asp.net > C# GetWindowRect简介及使用说明

C# GetWindowRect简介及使用说明

时间:2021-07-01 10:21:17 帮助过:50人阅读

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

人气教程排行