开发者

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

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?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜