WinAPI/GDI: why snapshot of large windows includes taskbar?

老子叫甜甜 提交于 2020-04-30 08:53:00

问题


I am using GDI+ to take window snapshot, the code is:

CComBSTR bstrfname (fname); 

HDC hdc = CreateCompatibleDC (hDC); 
HBITMAP hbmp = CreateCompatibleBitmap (hDC, CFG_WIDTH, CFG_HEIGHT); 
HBITMAP hbmp0 = (HBITMAP)SelectObject (hdc, hbmp); 
BitBlt (hdc, 0, 0, CFG_WIDTH, CFG_HEIGHT, hDC, 0, 0, SRCCOPY); 

Gdiplus::Bitmap *bmp = new Gdiplus::Bitmap (hbmp, NULL); 
CLSID encoderClsid; GetEncoderClsid (L"image/png", &encoderClsid); 
bmp->Save (bstrfname, &encoderClsid, NULL); 
delete bmp; 

SelectObject (hdc, hbmp0); 
DeleteObject (hbmp); 
DeleteDC (hdc); 

where hDC is set before with:

hWnd=CreateWindowEx(...); hDC=GetDC(hWnd); 

this works perfectly for small windows, but once I try windows bigger than screen.

i.e. taskbar is getting saved too. what gives?


回答1:


This is normal, a screen-shot like this gives you exactly what you are looking at on your monitor. Including the taskbar. You will need to restrict the area you capture to the bounds of the window you want to capture. Use GetWindowRect() and adjust the size of the bitmap and the arguments you pass to BitBlt() accordingly.

PrintWindow can only work if the target window implements the WM_PRINT and WM_PRINTCLIENT message. Easy to implement but often overlooked.



来源:https://stackoverflow.com/questions/3223039/winapi-gdi-why-snapshot-of-large-windows-includes-taskbar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!