views:

1898

answers:

3

What is the best (easiest) way to take a screenshot of an running application with C++ under Windows?

+9  A: 

You have to get the device context of the window (GetWindowDC()) and copy image (BitBlt()) from it. Depending on what else you know about the application you will use different methods to find which window's handle to pass into GetWindowDC().

sharptooth
A: 

Press the button 'Print Screen' on your keyboard.

Windows and Linux will dump the screen into the clipboard.
You can then paste the picture into your favorite graphics editing application.

Martin York
Don't see why this was down voted. The question didn't specify whether it should be done programatically or not. If it's not necessary to do it programatically then 'Print Screen' is the simplest way to do it! +1 from me!
Andreas Magnusson
It was stated that is has to be done with c++ and this is stackoverflow not yahoo answers!
Thomaschaaf
@Thomaschaaf: No it stated that it was a C++ program. It did not ask how to do it using C++. Doing it programitically is just silly. There are tools that do it for you (like Print Screen). Always use the best tool for the job.
Martin York
A: 

On the keybd_event function documentation it states that you can use it to take a screenshot and save it to the clipboard. For example:

keybd_event(VK_SNAPSHOT, 0, KEYEVENTF_SILENT, 0);

In my version (Visual Studio 2005 help installed on my computer) it states that you can take a screenshot of the whole desktop by setting the second parameter to 0, or a screen shot of just the current application by setting it to 1.

Taking it out of the clipboard buffer is left as an exercise for the reader.

However I'd think carefully before doing this as it will turf whatever image data was already present in the clipboard.

Daemin