本文介绍了WinAPI/GDI:为什么大窗口的快照包含任务栏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用GDI +拍摄窗口快照,代码为:

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);

之前使用以下方式设置了hDC:

where hDC is set before with:

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

这非常适合小窗户,但是一旦我尝试大窗户比屏幕要好.

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

即任务栏也被保存.会带来什么?

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

推荐答案

这是正常现象,类似这样的屏幕截图可为您提供您在显示器上正在查看的内容.包括任务栏.您将需要将捕获的区域限制为要捕获的窗口的边界.使用GetWindowRect()并相应地调整位图的大小和传递给BitBlt()的参数.

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.

仅当目标窗口实现WM_PRINT和WM_PRINTCLIENT消息时,PrintWindow才能工作.易于实现,但经常被忽略.

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

这篇关于WinAPI/GDI:为什么大窗口的快照包含任务栏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 14:30