窗体在Windows 8上显示错误的大小 - 如何获得实际大小?

Uwe*_*eim 5 .net c# windows-xp winforms windows-8

SizableWindows 8上将表单边框样式设置为WinForms表单时,该DesktopBounds属性将指示正确的值:

在此输入图像描述

相反,当具有表单边框样式时FixedDialog,值是错误的:

在此输入图像描述

在Windows XP上,值始终是正确的:

在此输入图像描述

在此输入图像描述

我的问题是:

如何获得包含完整非客户区域的窗口的实际大小?

更新1:

似乎它与这个SO问题有关.我会试着看看这是否能在这里解决我的问题.

更新2:

为了完整起见,以下是VMware Windows 7的结果:

在此输入图像描述

在此输入图像描述

更新3:

最后找到了一个解决方案,它涉及将DwmGetWindowAttribute函数DWMWA_EXTENDED_FRAME_BOUNDS一起使用.我将在下面发布一个答案.

Uwe*_*eim 8

为了回答我自己的问题,我终于找到了一个解决方案,它涉及将DwmGetWindowAttribute函数一起使用DWMWA_EXTENDED_FRAME_BOUNDS

答案的灵感来自于这个源代码,它提供了一个似乎适用于所有系统的功能.核心是一个功能:

public static Rectangle GetWindowRectangle(IntPtr handle)
{
    if (Environment.OSVersion.Version.Major < 6)
    {
        return GetWindowRect(handle);
    }
    else
    {
        Rectangle rectangle;
        return DWMWA_EXTENDED_FRAME_BOUNDS(handle, out rectangle) 
                   ? rectangle 
                   : GetWindowRect(handle);
    }
}
Run Code Online (Sandbox Code Playgroud)

完整代码如下:

public static class WindowHelper
{
    // https://code.google.com/p/zscreen/source/browse/trunk/ZScreenLib/Global/GraphicsCore.cs?r=1349

    /// <summary>
    /// Get real window size, no matter whether Win XP, Win Vista, 7 or 8.
    /// </summary>
    public static Rectangle GetWindowRectangle(IntPtr handle)
    {
        if (Environment.OSVersion.Version.Major < 6)
        {
            return GetWindowRect(handle);
        }
        else
        {
            Rectangle rectangle;
            return DWMWA_EXTENDED_FRAME_BOUNDS(handle, out rectangle) ? rectangle : GetWindowRect(handle);
        }
    }

    [DllImport(@"dwmapi.dll")]
    private static extern int DwmGetWindowAttribute(IntPtr hwnd, int dwAttribute, out Rect pvAttribute, int cbAttribute);

    private enum Dwmwindowattribute
    {
        DwmwaExtendedFrameBounds = 9
    }

    [Serializable, StructLayout(LayoutKind.Sequential)]
    private struct Rect
    {
        // ReSharper disable MemberCanBePrivate.Local
        // ReSharper disable FieldCanBeMadeReadOnly.Local
        public int Left;
        public int Top;
        public int Right;
        public int Bottom;
        // ReSharper restore FieldCanBeMadeReadOnly.Local
        // ReSharper restore MemberCanBePrivate.Local

        public Rectangle ToRectangle()
        {
            return Rectangle.FromLTRB(Left, Top, Right, Bottom);
        }
    }

    private static bool DWMWA_EXTENDED_FRAME_BOUNDS(IntPtr handle, out Rectangle rectangle)
    {
        Rect rect;
        var result = DwmGetWindowAttribute(handle, (int)Dwmwindowattribute.DwmwaExtendedFrameBounds,
            out rect, Marshal.SizeOf(typeof(Rect)));
        rectangle = rect.ToRectangle();
        return result >= 0;
    }

    [DllImport(@"user32.dll")]
    [return: MarshalAs(UnmanagedType.Bool)]
    private static extern bool GetWindowRect(IntPtr hWnd, out Rect lpRect);

    private static Rectangle GetWindowRect(IntPtr handle)
    {
        Rect rect;
        GetWindowRect(handle, out rect);
        return rect.ToRectangle();
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 该消息源似乎已经移到这里:https://github.com/ShareX/ShareX/blob/e81176a8398993d3208f9ca83b0422f6e53ef48d/ShareX.HelpersLib/Helpers/CaptureHelpers.cs#L310 (2认同)