c# - DwmGetWindowAttribute 使用 PInvoke 返回 0

标签 c# windows pinvoke

我正在尝试通过捕获特定窗口来进行屏幕捕获,为了准确计算出要捕获的窗口的大小,我想使用 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;
    }

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

最佳答案

基于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();
    }

关于c# - DwmGetWindowAttribute 使用 PInvoke 返回 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38685405/

相关文章:

c++ - 直接访问PCI串口

c# - PInvoke - 找不到指定的模块。如何检查缺少的依赖项?

c# - WPF是否有鼠标滚轮上下滚动事件

c# - 带有 .Net5.0 的 Microsoft Interop Excel 无法正常工作

c# - Process.WaitForExit 不使用 __debugbreak 触发

c# - 逆向工程串行通信

c++ - Windows C++过滤声音输出

c# - C# 中的字符数组编码(marshal)处理

c# - C++ DLL 函数似乎在 C# 中不起作用

c# - 错误 MSB4019 : The imported project "C:\Program Files\dotnet\sdk\1.0.3\Microsoft\Portable\v5.0\Microsoft.Portable.CSharp.targets" was not found