背景:IM軟體內開發截圖工具,有個需求就是在截圖之后隱藏主視窗然后截取桌面,在win7的basic主題下會高概率的出現主視窗的殘影,但在aero主題下沒這個問題
截圖介面:QWindowsScreen::grabWindow
具體內部實作:
QPixmap QWindowsScreen::grabWindow(WId window, int x, int y, int width, int height) const
{
RECT r;
HWND hwnd = window ? reinterpret_cast<HWND>(window) : GetDesktopWindow();
GetClientRect(hwnd, &r);
if (width < 0) width = r.right - r.left;
if (height < 0) height = r.bottom - r.top;
// Create and setup bitmap
HDC display_dc = GetDC(0);
HDC bitmap_dc = CreateCompatibleDC(display_dc);
HBITMAP bitmap = CreateCompatibleBitmap(display_dc, width, height);
HGDIOBJ null_bitmap = SelectObject(bitmap_dc, bitmap);
// copy data
HDC window_dc = GetDC(hwnd);
BitBlt(bitmap_dc, 0, 0, width, height, window_dc, x, y, SRCCOPY | CAPTUREBLT);
// clean up all but bitmap
ReleaseDC(hwnd, window_dc);
SelectObject(bitmap_dc, null_bitmap);
DeleteDC(bitmap_dc);
const QPixmap pixmap = qt_pixmapFromWinHBITMAP(bitmap);
DeleteObject(bitmap);
ReleaseDC(0, display_dc);
return pixmap;
}
目前分析:aero主題注重外觀,basic注重系統運行速度,在渲染方面aero效果較佳。上述代碼中// copy data
HDC window_dc = GetDC(hwnd);
BitBlt(bitmap_dc, 0, 0, width, height, window_dc, x, y, SRCCOPY | CAPTUREBLT);
如果在這段代碼前面加Sleep(100)出現截圖程式主視窗殘留的問題會降低非常多,但是還是有概率出現。有人知道具體原因嗎,有啥方案可以規避。參考了競品微信和釘釘都沒這個問題。
uj5u.com熱心網友回復:
把 HDC window_dc = GetDC(hwnd);BitBlt(bitmap_dc, 0, 0, width, height, window_dc, x, y, SRCCOPY | CAPTUREBLT);
// clean up all but bitmap
ReleaseDC(hwnd, window_dc); 這一段
用 PrintWindow 試試看, 有可能視窗還沒有重繪
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/48566.html
標籤:硬件/系統
下一篇:Apache 啟動不成功
