PrintWindow API的第三个参数

问题描述 投票:0回答:1

最近我在使用一个名为

PrintWindow()
的 Win32 API,它可以截取特定窗口。在某些启用了硬件加速的窗口中,图像将为空白。但是,如果我将第三个参数从
0
更改为
3
,它就可以再次工作。

我还发布了用C#编写的演示代码:

[DllImport("user32.dll")]
public static extern bool GetWindowRect(IntPtr hWnd, out RECT lpRect);
[DllImport("user32.dll")]
public static extern bool PrintWindow(IntPtr hWnd, IntPtr hdcBlt, int nFlags);

public struct RECT
{
    public int Left;
    public int Top;
    public int Right;
    public int Bottom;
}

public static Bitmap GetScreenshot_PrintWindow(IntPtr hWnd, Rectangle screenBounds)
{
    RECT windowRect;
    if (!GetWindowRect(hWnd, out windowRect))
        return null;
    Rectangle windowBounds = new Rectangle(windowRect.Left, windowRect.Top, windowRect.Right - windowRect.Left, windowRect.Bottom - windowRect.Top);
    Rectangle intersection = Rectangle.Intersect(screenBounds, windowBounds);
    if (intersection.IsEmpty)
        return null;
    Bitmap screenshot = new Bitmap(intersection.Width, intersection.Height);
    using (Graphics g = Graphics.FromImage(screenshot))
    {
        IntPtr hdc = g.GetHdc();
        PrintWindow(hWnd, hdc, 3);
        g.ReleaseHdc(hdc);
    }

    return screenshot;
}

在官方文档中,微软只列出了

PW_CLIENTONLY
,并没有太多关于这个参数的信息,所以我想知道它是什么,我可以使用哪些值,以及它们的作用是什么。

windows winapi
1个回答
0
投票

我可以在文件头中找到另一个未提及的标志:

#define PW_CLIENTONLY           0x00000001
#if(_WIN32_WINNT >= 0x0603)
#define PW_RENDERFULLCONTENT    0x00000002
#endif /* _WIN32_WINNT >= 0x0603 */

此代码仅表示

PW_RENDERFULLCONTENT
标志只能在 Windows NT 6.1 (Windows 8.1) 及更高版本上使用。因此,如果您将其设置为 2,它将被视为有效。但文档讨论了另一个未提及的标志:

...如果指定了

PW_PRINTCLIENT
标志...

我认为有 3 件事可能导致此异常:

  1. PW_PRINTCLIENT
    标志的值为3(Windows API标头中没有任何具有该名称的标志)。
  2. 0作为参数无效,有些窗口无法理解。
  3. 值 3 无效,需要进行检查以设置为有效值,即。 e.如果大于 2。

希望能帮到你。

© www.soinside.com 2019 - 2024. All rights reserved.