c#知道窗口句柄,如何获取窗口大小

来源:百度知道 编辑:UC知道 时间:2024/05/28 04:34:28
如题,只能获得该窗口的句柄,有没有函数通过句柄来获取该窗口的大小。我查到c++中的GetWindowRect函数貌似可以,请问该c#中是否有替代函数,该如何使用,请帮帮忙,给出具体代码,谢谢
111

不记得有替代函数 ,你还是用 GetWindowRect吧

// 引用
using System.Runtime.InteropServices;

// 获得窗口矩形
[DllImport("user32.dll")]
public static extern int GetWindowRect(IntPtr hWnd, out RECT lpRect);

// 获得客户区矩形
[DllImport("user32.dll")]
public static extern int GetClientRect(IntPtr hWnd, out RECT lpRect);

// 矩形结构
[StructLayout(LayoutKind.Sequential)]
public struct RECT
{
int left;
int top;
int right;
int bottom;
}

// 调用

IntPtr hwnd = .....; // 窗口句柄

RECT rc;

GetWindowRect(hwnd, out rc);
GetClientRect(hwnd, out rc);

Form aa = Form.FromHandle("你的句柄");
int width = aa.Width;
int height = aa.Height;
Rectangle bb= aa.RectangleToClient();
Rectangle cc = aa.Rectangle