PrintWindow 有什么问题? [英] What is wrong with PrintWindow?
问题描述
下面的代码有什么问题?为什么 PrintWindow
返回 0?
What is wrong with the following code? Why does PrintWindow
return 0?
HWND hwnd = GetDesktopWindow();
CHK(hwnd);
HDC hdc = GetWindowDC(hwnd);
CHK(hdc);
if (hdc)
{
HDC hdcMem = CreateCompatibleDC(hdc);
CHK(hdcMem);
if (hdcMem)
{
RECT rc;
CHK(GetWindowRect(hwnd, &rc));
HBITMAP hbitmap = CreateCompatibleBitmap(hdc, rc.right-rc.left, rc.bottom-rc.top);
CHK(hbitmap);
if (hbitmap)
{
SelectObject(hdcMem, hbitmap);
CHK(PrintWindow(hwnd, hdcMem, 0)); //HERE return 0
DeleteObject(hbitmap);
}
DeleteObject(hdcMem);
}
ReleaseDC(hwnd, hdc);
}
推荐答案
PrintWindow
是一个相当精简的操作.它真正做的是将 WM_PRINT
消息发布到相关窗口的队列中,在这种情况下是桌面,并希望该窗口能够正确响应 WM_PRINT
全部(参见此处和此处).
PrintWindow
is a fairly thin operation. What it really does is post a WM_PRINT
message to the queue for the window in question, in this case the desktop, and hopes that that window will respond to WM_PRINT
correctly if at all (see here and here).
我重现了你的行为,但我也不能 100% 确定它失败的原因.也许您无法在您的进程不拥有的 HWND
上调用 PrintWindow
,或者桌面不响应 WM_PRINT
消息.
I repro'd your behavior but I'm not 100% sure why it's failing either. Perhaps you cannot call PrintWindow
on an HWND
that your process does not own, or perhaps the desktop does not respond to WM_PRINT
messages.
上面的第二个链接包含关于使用 BitBlt
代替:
The second link above includes a comment about using BitBlt
instead:
尝试获取句柄 (HWND)桌面窗口 - 并使用 BitBlt
捕获所有内容.提个醒 -你只会捕捉到什么是可见的屏幕.
Try getting a handle (HWND) to the desktop window - and use
BitBlt
to capture all the contents. Mind you - you'll only capture what is visible on the screen.
也许这会有所帮助.
这篇关于PrintWindow 有什么问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!