背景资料: 我有这个我编码的MFC应用程序并且已经使用了很长时间,当用户点击Print Screen / Alt + Print Screen键时,它会自动将屏幕截图保存到硬盘上。我一直推迟使用与Aero相关的任何东西,直到现在我已经使用Windows 7 RC几周了。
问题: 我正在使用标准的GetDC / BitBlt方法来捕获窗口内容。在进行常规全屏抓取时(无论打开多少个窗口等),我对此方法没有任何问题。当我尝试捕获前景窗口(Alt + PrintScreen)时出现问题。以下是两个例子:
示例1 http://indiecodelabs.com/extern/example1.jpg
示例2 http://indiecodelabs.com/extern/example2.jpg
正如你所看到的,我的边界应该是垃圾。这在顶部更明显,我们可以在两个屏幕截图中看到工具栏的一些重复。
我一直在谷歌搜索这个已经好几个小时了,所有我能找到的文章说在DWM下BitBtl / GetDC方法不起作用,但找不到一个解释我们(开发者)应该怎么做的在DWM上运行时,能够在我们的应用程序中保持相同的功能。
任何帮助,指示和建议都将不胜感激。
答案 0 :(得分:3)
BOOL CaptureWindow(const CString& filename)
{
HWND hWnd = NULL;
hWnd = ::GetForegroundWindow();
if(!hWnd)
{
return FALSE;
}
CRect rect;
GetWindowRect(hWnd, &rect);
rect.NormalizeRect();
return DoCapture(CPoint(rect.left, rect.top), CSize(rect.Width(), rect.Height()), filename);
}
BOOL DoCapture(const POINT& coords, const SIZE& areaSize, const CString& filename)
{
CDC dc;
HDC hdc = GetDC(NULL); // <-- We use this instead of GetWindowDC.
// This is the only thing I had to change other than
// getting the window coordinates in CaptureWindow()
dc.Attach(hdc);
// Create a memory DC into which the bitmap will be captured
CDC memDC;
memDC.CreateCompatibleDC(&dc);
// If there is already a bitmap, delete it as we are going to replace it
CBitmap bmp;
bmp.DeleteObject();
ICONINFO info;
GetIconInfo((HICON)::GetCursor(), &info);
CURSORINFO cursor;
cursor.cbSize = sizeof(CURSORINFO);
GetCursorInfo(&cursor);
bmp.CreateCompatibleBitmap(&dc, areaSize.cx, areaSize.cy);
CBitmap * oldbm = memDC.SelectObject(&bmp);
// Before we copy the image in, we blank the bitmap to
// the background fill color
memDC.FillSolidRect(&CRect(0,0,areaSize.cx, areaSize.cy), RGB(255,255,255));
// Copy the window image from the window DC into the memory DC
memDC.BitBlt(0, 0, areaSize.cx, areaSize.cy, &dc, coords.x, coords.y, SRCCOPY|CAPTUREBLT);
// This part captures the mouse cursor and paints it on the image.
if(programSettings.bWantCursor)
{
int osVersion = OSCheck::GetMajorOSVersion(); // For some reason cursor icons in
// versions older than Vista are not
// top-aligned. So we compensate.
int offsetX = (osVersion >= 6) ? 0 : 10;
int offsetY = (osVersion >= 6) ? 0 : 10;
CPoint cursorOffset(cursor.ptScreenPos.x - coords.x - offsetX, cursor.ptScreenPos.y - coords.y - offsetY);
// Now draw the image of the cursor that we captured during
// the mouse move. DrawIcon will draw a cursor as well.
memDC.DrawIcon(cursorOffset, (HICON)cursor.hCursor);
}
memDC.SelectObject(oldbm);
Bitmap outputBitMap(bmp, NULL);
// Optionally copy the image to the clipboard.
if(programSettings.bWantClipboard)
{
if(OpenClipboard(NULL))
{
EmptyClipboard();
SetClipboardData(CF_BITMAP, bmp);
CloseClipboard();
}
}
BOOL success = DumpImage(&outputBitMap, filename);
DeleteObject(bmp.Detach());
DeleteDC(dc.Detach());
DeleteDC(memDC.Detach());
return success;
}
供参考:DumpImage几乎使用Gdi :: Bitmap的Save方法。它已被省略,因为它有一些特定于应用程序的代码与示例无关。另外一个额外的好处是,如果你想知道如何将光标包含在你的screengrab中,那么代码也在那里。希望能帮助到你。另外值得一提的是,与传统方法相反,这还包括您可能在捕获的窗口顶部覆盖的任何窗口。此外,如果您使用此代码进行全屏捕获,请注意它不会捕获视频游戏窗口。我实际上想知道如何正确地做到这一点,而不必诉诸DirectX。这仅在Aero模式下运行时影响Windows Vista / 7。
答案 1 :(得分:2)
这是一个很好的问题,我不幸地不知道确切的答案。我的第一个想法是抓住整个桌面并从中剪切出有趣的部分。
我已经挖掘了QT 4.5来源,看看他们是如何做到的,并找到了类似的东西。如果您将GetClientRect切换到GetWindowRect并删除QT样板代码,您应该得到您想要的。它虽然看起来像黑客:)
QPixmap QPixmap::grabWindow(WId winId, int x, int y, int w, int h )
{
RECT r;
GetClientRect(winId, &r);
if (w < 0) w = r.right - r.left;
if (h < 0) h = r.bottom - r.top;
// Create and setup bitmap
HDC display_dc = GetDC(0);
HDC bitmap_dc = CreateCompatibleDC(display_dc);
HBITMAP bitmap = CreateCompatibleBitmap(display_dc, w, h);
HGDIOBJ null_bitmap = SelectObject(bitmap_dc, bitmap);
// copy data
HDC window_dc = GetDC(winId);
BitBlt(bitmap_dc, 0, 0, w, h, window_dc, x, y, SRCCOPY);
// clean up all but bitmap
ReleaseDC(winId, window_dc);
SelectObject(bitmap_dc, null_bitmap);
DeleteDC(bitmap_dc);
QPixmap pixmap = QPixmap::fromWinHBITMAP(bitmap);
DeleteObject(bitmap);
ReleaseDC(0, display_dc);
return pixmap;
}