打印窗口出了什么问题?
下面的代码有什么问题?为什么PrintWindow
返回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);
}
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);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
PrintWindow
是一个相当简单的操作。它真正做的是将WM_PRINT
消息发送到有问题的窗口(在本例中为桌面),并希望该窗口能够正确响应WM_PRINT
(如果有的话)(请参阅此处和此处)。我重复了你的行为,但我也不是 100% 确定它失败的原因。也许您无法在不属于您的进程的
HWND
上调用PrintWindow
,或者桌面可能不响应WM_PRINT
消息。上面的第二个链接包含有关使用
的评论BitBlt
相反:也许这有帮助。
PrintWindow
is a fairly thin operation. What it really does is send aWM_PRINT
message to the window in question, in this case the desktop, and hopes that that window will respond toWM_PRINT
correctly if at all (see here and here).I repro'd your behavior but I'm not 100% sure why it's failing either. Perhaps you cannot call
PrintWindow
on anHWND
that your process does not own, or perhaps the desktop does not respond toWM_PRINT
messages.The second link above includes a comment about using
BitBlt
instead:Maybe this helps.
看起来
GetDesktopWindow()
返回一个虚拟 HWND,其值在所有 Windows 计算机上普遍为 0x0010010。此虚拟 HWND 不符合通常的 PrintWindow 行为,因此 PrintWindow() 返回 FALSE,并且 GetLastError() 在此 PrintWindow 调用上不报告任何错误代码。要使 PrintWindow() 正常工作,您可以使用
GetShellWindow()
中的 HWND,该 HWND 的标题来自下图 WinSpy++ 中的“程序管理器”。It looks like
GetDesktopWindow()
returns a virtual HWND whose value is universally 0x0010010 on all Windows machines. This virtual HWND does not conform to usual PrintWindow behavior so the PrintWindow() returns FALSE, and GetLastError() reports no error code on this PrintWindow call.To make PrintWindow() work, you can instead use the HWND from
GetShellWindow()
, which has the title "Program Manager" from the WinSpy++ figure below.替换:
为:
我不确定这是否得到你想要的。如果您想截取整个当前桌面(包括任何可见的顶级窗口)的屏幕截图,那么 BitBlt 就是您想要采取的路线。
如果您还想获得任务栏,您仍然可以使用此方法,但您必须截取两张屏幕截图并将结果拼接在一起。
Replace:
With:
I'm not sure whether this gets what you want though. If you want to take a screenshot of the entire current desktop (including whatever top level windows are visible) then BitBlt is the route you want to take.
If you want to get the taskbar as well, you can still use this method but you'll have to take 2 screenshots and stitch the results together.