Windows提供用于获取窗口图像的功能PrintWindow。下面是一个例子,从我的消息来源采取放置一个产生截图到剪贴板(它考虑到该功能可能无法在某些Windows版本的支持,但似乎并不重要的问题):
void PrintCapturedWindow(HWND hwnd)
{
HDC hdc = GetDC(hwnd);
if (hdc)
{
HDC hdcMem = CreateCompatibleDC(hdc);
if (hdcMem)
{
RECT rc;
GetClientRect(hwnd, &rc);
HBITMAP hbitmap = CreateCompatibleBitmap(hdc, rc.right-rc.left, rc.bottom-rc.top);
ReleaseDC(hwnd, hdc); hdc = 0;
if (hbitmap)
{
typedef BOOL WINAPI (* pPrintWindow)(HWND hwnd, HDC hdcBlt, UINT nFlags);
pPrintWindow ppw;
HMODULE user_hand = GetModuleHandle("user32.dll");
ppw = (pPrintWindow)GetProcAddress(user_hand, "PrintWindow");
if(ppw)
{
SelectObject(hdcMem, hbitmap);
(*ppw)(hwnd, hdcMem, 0);
}
::OpenClipboard(NULL);
::EmptyClipboard();
::SetClipboardData(CF_BITMAP, hbitmap);
::CloseClipboard();
DeleteObject(hbitmap);
}
DeleteObject(hdcMem);
}
if(hdc != 0) ReleaseDC(hwnd, hdc);
}
}