应用Melon的宽度和高度是438 x 615像素,:: GetWindowRect()函数正确抓取它。
但是,:: PrintWindow()函数绘制的尺寸较小,为348 x 489像素,其余部分填充黑色空白(可能没有绘制)
......可能一张图片会比数百张描述更好。
这是代码的结果
bool result = true;
HWND appHWnd = ::FindWindow(nullptr, TEXT("Melon"));
RECT appWindowRect; ::GetWindowRect(appHWnd, &appWindowRect);
HDC appDC = ::GetWindowDC(appHWnd);
// HDC appDC = ::GetDC(appHWnd); // same issue occured either
// HDC appDC = ::GetDC(nullptr);
HDC memoryDC = ::CreateCompatibleDC(appDC);
HBITMAP capturedScreenBitmap = ::CreateCompatibleBitmap(
appDC,
appWindowRect.right - appWindowRect.left,
appWindowRect.bottom - appWindowRect.top
);
HBITMAP memoryBitmap = static_cast<HBITMAP>(::SelectObject(memoryDC, capturedScreenBitmap));
result = ::PrintWindow(appHWnd, memoryDC, 0);
//copy to clipboard
OpenClipboard(nullptr);
EmptyClipboard();
SetClipboardData(CF_BITMAP, capturedScreenBitmap);
CloseClipboard();
::SelectObject(memoryDC, memoryBitmap);
::DeleteObject(capturedScreenBitmap);
::DeleteDC(memoryDC);
::ReleaseDC(appHWnd, appDC);
奇怪的是,C#版本的代码正常工作。导入相同的user32库,使用相同的并输出不同的结果?为什么?
这将取决于DPI意识 - 大卫赫弗南
:: GetWindowRect,用于Visual Studio中的c#项目和C ++控制台项目,不受dpi识别缩放的影响。但是,习惯qt studio的东西会受到影响。
这是我的解决方案。
RECT appWindowRect; {
::GetWindowRect(hwnd, &appWindowRect);
}
POINT appWindowSize; {
qreal dotsPerInch = QApplication::screens().at(0)->logicalDotsPerInch();
appWindowSize.x = static_cast<LONG>((appWindowRect.right - appWindowRect.left) * 96 / dotsPerInch);
appWindowSize.y = static_cast<LONG>((appWindowRect.bottom - appWindowRect.top) * 96 / dotsPerInch);
}