views:

60

answers:

1

Hi,

I have the following code to take screenshots of a window:

HDC WinDC;
HDC CopyDC;
HBITMAP hBitmap;
RECT rt;

GetClientRect (hwnd, &rt);
WinDC = GetDC (hwnd);
CopyDC = CreateCompatibleDC (WinDC);

hBitmap = CreateCompatibleBitmap (WinDC,
 rt.right - rt.left, //width
 rt.bottom - rt.top);//height

SelectObject (CopyDC, hBitmap);

//Copy the window DC to the compatible DC
BitBlt (CopyDC,   //destination
 0,0,
 rt.right - rt.left, //width
 rt.bottom - rt.top, //height
 WinDC,    //source
 0, 0,
 SRCCOPY);

ReleaseDC(hwnd, WinDC);
ReleaseDC(hwnd, CopyDC);

It is someone elses code, slightly modified, as I am not really familiar with DC and how windows draws stuff to screen.

When I have one window slightly covering another, the covering window appears on screenshots of the covered one, which is kind of inconvenient. Also, when the window is minimized, this code produces nothing much interesting.

Is there any way around this? I would imagine that taking screenshots of a minimized application would be quite difficult, but I hope that getting screenshots of covered windows is possible. Perhaps there is a different method of implementing this to get around these problems?

+3  A: 

No, a screenshot is exactly what it sounds like. You'll read the pixels out of the video adapter, what you get is what you see. You'll have to restore the window and bring it to the foreground to get the full view. WM_SYSCOMMAND+SC_RESTORE and SetForegroundWindow() respectively. Plus some time to allow the app to repaint its window if necessary.

The WM_PRINT message is available to ask a window to draw itself into a memory context. That could handle the overlapped window problem. But that can only work if is your window. And rarely has the expected result, programmers don't often implement WM_PRINT correctly.

Hans Passant
The WM_PRINT thing might be handy, and shouldn't be too difficult. Thanks.
Oliver
To be fair, you're not likely reading the pixels directly off the video adapter - probably from an internal buffer held by Windows. I could be wrong, but I would be surprised if it wasn't done this way.
George Edison