Windows provides the function PrintWindow
for getting an image of a window. Here is an example, taken from my sources, it places a resulting screenshot into clipboard (it takes into account that the function may not be supported on some Windows versions, but it seems unimportant to the question):
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);
}
}