I want to take a screenshot of some specific window (e.g calculator).
Here is the code I have written according to this discussion:
// Get the window handle of calculator application.
HWND hWnd = ::FindWindow(0, _T("Calculator"));
RECT r;
GetWindowRect(hWnd, &r);
int x[2]; int y[2];
x[0] = r.top; x[1] = r.bottom;
y[0] = r.left; y[1] = r.right;
HDC hScreen = GetWindowDC(hWnd);
HDC hDC = CreateCompatibleDC(hScreen);
HBITMAP hBitmap = CreateCompatibleBitmap(hScreen, y[1] - y[0], x[1] - x[0]);
HGDIOBJ old_obj = SelectObject(hDC, hBitmap);
BitBlt(hDC, 0, 0, y[1] - y[0], x[1] - x[0], hScreen, y[0], x[0], SRCCOPY);
Afterwards, I save the bitmap as a .bmp image.
The result has correct size and position of the calculator window but the resulting bmp is all black.
I tried to screenshot the full desktop and then cut the calculator part and that worked. But I want to be able to make a screenshot of the window even if it is minimized or covered by another window.
Any ideas why this code is not working or is there any other way to do it?
Thanks.