.net 为什么GetWindowRect会为一个坐标返回一个巨大的值?

46qrfjad  于 2023-02-10  发布在  .NET
关注(0)|答案(1)|浏览(114)

对于分辨率为1536x960的单台显示器上Windows 10桌面应用程序的最大化活动窗口,我通过以下方式检索其坐标:

IntPtr hwnd = GetForegroundWindow();

Rectangle bounds = GetWindowRect(hwnd);

Console.WriteLine("Left: " + bounds.Left);
Console.WriteLine("Right: " + bounds.Right);
Console.WriteLine("Top: " + bounds.Top);
Console.WriteLine("Bottom: " + bounds.Bottom);

这将输出:

Left: 0  
Right: -2080342032  
Top: 0  
Bottom: 695

右坐标如此混乱的可能原因是什么?

slmsl1lt

slmsl1lt1#

您调用GetWindowRect的方式不正确。请尝试执行以下操作,注意它返回了一个bool,边界在out参数中给出:

[DllImport("user32.dll")]
static extern bool GetWindowRect(IntPtr hwnd, out Rectangle rectangle);

就像这样称呼它:

if (!GetWindowRect(hwnd, out var bounds))
{
    throw new Win32Exception();
}

Console.WriteLine("Left: " + bounds.Left);
Console.WriteLine("Right: " + bounds.Right);
Console.WriteLine("Top: " + bounds.Top);
Console.WriteLine("Bottom: " + bounds.Bottom);

相关问题