GetWindowRect()未返回矩形

本文关键字:返回 GetWindowRect | 更新日期: 2023-09-27 18:24:13

我有一段C#代码,如下所示:

[DllImport("user32.dll")]
private static extern bool GetWindowRect(IntPtr hWnd, ref Rect rect);
Rect rect = Rect.Zero;
NativeMethods.GetWindowRect(hWnd, ref rect);

然而,当我运行此代码时,rect变量仍然等于Rect.Zero(上/下/左/右均为零),即使GetWindowRect()返回成功。当在C++中实现时,这个函数调用序列可以很好地工作,但当在C#中通过P/Invoke调用时就不行了。

我确信我正在正确地呼叫GetWindowRect();这不是问题所在。问题是,无论我如何更改函数调用或如何定义P/Invoke,GetWindowRect()总是返回{0,0,0,0}RECT

GetWindowRect()未返回矩形

事实证明,我传递给GetWindowRect()的句柄不是有效的HWND