1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
|
int CaptureAnImage(HWND hwnd)
{
// Create a topmost window and print image in the window, overlapping everything on screen.
HWND testhwnd = CreateWindow(L"STATIC", NULL,
SS_BITMAP| WS_POPUPWINDOW,
0, 0,
GetSystemMetrics(SM_CXVIRTUALSCREEN),
GetSystemMetrics(SM_CYVIRTUALSCREEN),
NULL, NULL, NULL, NULL);
HDC hdcScreen;
HDC hdcWindow;
HDC hdcMemDC = NULL;
HBITMAP hbmScreen = NULL;
BITMAP bmpScreen;
// Retrieve the handle to a display device context for the client
// area of the window.
hdcScreen = GetDC(NULL);
hdcWindow = GetDC(testhwnd);
// Create a compatible DC which is used in a BitBlt from the window DC
hdcMemDC = CreateCompatibleDC(hdcWindow);
if(!hdcMemDC)
{
MessageBox(hwnd, L"CreateCompatibleDC has failed",L"Failed", MB_OK);
goto done;
}
// Get the client area for size calculation
RECT rcClient;
GetClientRect(testhwnd, &rcClient);
//This is the best stretch mode
SetStretchBltMode(hdcWindow,HALFTONE);
//The source DC is the entire screen and the destination DC is the current window (HWND)
if(!StretchBlt(hdcWindow,
0,0,
GetSystemMetrics (SM_CXVIRTUALSCREEN)-10,
GetSystemMetrics (SM_CYVIRTUALSCREEN)-10,
//rcClient.right, rcClient.bottom,
hdcScreen,
0,0,
GetSystemMetrics (SM_CXVIRTUALSCREEN),
GetSystemMetrics (SM_CYVIRTUALSCREEN),
SRCCOPY))
{
MessageBox(hwnd, L"StretchBlt has failed",L"Failed", MB_OK);
goto done;
}
ShowWindow(testhwnd, SW_SHOW);
//Clean up
done:
DeleteObject(hbmScreen);
DeleteObject(hdcMemDC);
ReleaseDC(NULL,hdcScreen);
ReleaseDC(testhwnd,hdcWindow);
return 0;
}
|