DwmGetWindowAttribute使用PInvoke返回0

本文关键字:返回 PInvoke 使用 DwmGetWindowAttribute | 更新日期: 2023-09-27 17:59:19

我试图通过捕获特定窗口来进行屏幕捕获,为了准确计算要捕获的窗口的大小,我想使用DwmGetWindowAttribute()。当我在Windows 10上用PInvoke调用此函数时,即使结果值为0(成功),Rect结构也始终为空。传入的Window句柄也是有效的,因为存在调用GetWindowRect()的回退代码,该代码有效(尽管存在边界问题)。

我有点不知所措。我以前使用过同样的代码(也许是在Windows 8.1上?),同样的代码似乎也在工作,但现在无论我做什么,对函数的调用总是返回一个空结构。

这是相关代码。

定义:

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

    [Flags]
    public enum DwmWindowAttribute : uint
    {
        DWMWA_NCRENDERING_ENABLED = 1,
        DWMWA_NCRENDERING_POLICY,
        DWMWA_TRANSITIONS_FORCEDISABLED,
        DWMWA_ALLOW_NCPAINT,
        DWMWA_CAPTION_BUTTON_BOUNDS,
        DWMWA_NONCLIENT_RTL_LAYOUT,
        DWMWA_FORCE_ICONIC_REPRESENTATION,
        DWMWA_FLIP3D_POLICY,
        DWMWA_EXTENDED_FRAME_BOUNDS,
        DWMWA_HAS_ICONIC_BITMAP,
        DWMWA_DISALLOW_PEEK,
        DWMWA_EXCLUDED_FROM_PEEK,
        DWMWA_CLOAK,
        DWMWA_CLOAKED,
        DWMWA_FREEZE_REPRESENTATION,
        DWMWA_LAST
    }
    [Serializable, StructLayout(LayoutKind.Sequential)]
    public struct Rect
    {
        public int Left;
        public int Top;
        public int Right;
        public int Bottom;
        public Rectangle ToRectangle()
        {
            return Rectangle.FromLTRB(Left, Top, Right, Bottom);
        }
    }

进行捕获的代码:

    public static Rectangle GetWindowRectangle(IntPtr handle)
    {
        Rectangle rected = Rectangle.Empty;
        Rect rect = new Rect();
        if (Environment.OSVersion.Version.Major < 6)
        {
            GetWindowRect(handle, out rect);
            rected = rect.ToRectangle();
        }      
        else
        {
            int size = Marshal.SizeOf(typeof(Rect));
            int res = DwmGetWindowAttribute(handle, (int)DwmWindowAttribute.DWMWA_EXTENDED_FRAME_BOUNDS, out rect, size);
            Debug.WriteLine(res.ToString("x") + " " + size + " " + handle + " " + (int) DwmWindowAttribute.DWMWA_EXTENDED_FRAME_BOUNDS);
            // allow returning of desktop and aero windows
            if (rected.Width == 0)
            {
                GetWindowRect(handle, out rect);
                rected = rect.ToRectangle();
                Debug.WriteLine("Using GetWindowRect");
            }
        }
        Debug.WriteLine(rected.ToString());
        return rected;
    }

感觉这里缺少了一些简单的东西。有什么想法吗?

DwmGetWindowAttribute使用PInvoke返回0

基于Rick Strahl原始代码以及Hans Passant校正,我创建了一个更紧凑的GetWindowsRectangle版本。我在Windows 10上测试了它,以下是代码,以防它在未来对某人有所帮助:

public static Rectangle GetWindowRectangle(IntPtr handle)
    {
        Rect rect = new Rect();
                        
        if (Environment.OSVersion.Version.Major >= 6)
        {
            int size = Marshal.SizeOf(typeof(Rect));
            DwmGetWindowAttribute(handle, (int)DwmWindowAttribute.DWMWA_EXTENDED_FRAME_BOUNDS, out rect, size);
        }
        else if (Environment.OSVersion.Version.Major < 6 || rect.ToRectangle().Width == 0)
        {
            GetWindowRect(handle, out rect);
        }
        return rect.ToRectangle();
    }

使用GetWindowRect而不是DwmGetWindowAttribute来接收窗口的RECT

[DllImport("user32.dll", SetLastError = true)]
public static extern bool GetWindowRect(IntPtr hwnd, out RECT lpRect);