PrintWindow会在标题栏中导致闪烁,奇怪的文物

时间:2013-11-13 22:27:54

标签: c++ windows winapi screenshot

我正在尝试拍摄Chrome窗口的屏幕截图。它看起来像这样:

Chrome window

当我使用PrintWindow获取屏幕截图时,我可以在窗口标题栏/ Chrome标签区域看到闪烁。捕获的屏幕截图包含Windows Basic样式中标题栏的奇怪呈现(即使我的机器运行Aero主题):

Captured image

我注意到其他一些应用也表现出类似的行为,它们在屏幕上闪烁,但标题栏工件在捕获的屏幕截图中不可见。执行此操作的应用程序包括Office 2010,IE 10和Trillian选项卡式聊天窗口 - 换句话说,扩展非客户区域的窗口似乎存在此问题。

重现这一点的代码很简单:

void Screenshot(HWND hWnd) {

    RECT rc;
    GetClientRect(hWnd, &rc);

    HDC hdcScreen = GetDC(NULL);
    HDC hdc = CreateCompatibleDC(hdcScreen);
    HBITMAP hbmp = CreateCompatibleBitmap(hdcScreen, 
        rc.right - rc.left, rc.bottom - rc.top);
    SelectObject(hdc, hbmp);

    //Print to memory hdc
    PrintWindow(hWnd, hdc, PW_CLIENTONLY);

}

为什么我会看到闪烁和奇怪的视觉文物?我能做些什么来阻止它?

2 个答案:

答案 0 :(得分:1)

如果启用了Aero,请改用BitBlt。

chromium desktop capture source code中的这条评论特别有用:

// When desktop composition (Aero) is enabled each window is rendered to a
// private buffer allowing BitBlt() to get the window content even if the
// window is occluded. PrintWindow() is slower but lets rendering the window
// contents to an off-screen device context when Aero is not available.

答案 1 :(得分:0)

对于那些有相同问题的人,请执行以下操作:

const uint PW_RENDERFULLCONTENT = 0x00000002;
PrintWindow(hWnd, hDC, PW_RENDERFULLCONTENT);
相关问题