Because of the functions provided with the Windows API, capturing the screen to the Clipboard becomes a relatively simple task. This tutorial assumes that you have a basic knowledge of C++ and some understanding of the Windows environment.

In the sample code that complements this tutorial, the following calls have been added to the WinMain procedure. However, your application does much more, and you will have identified a more suitable place to add these.

1. Include stdafx.h. The functions and constants we will require will be declared/defined in winuser.h and wingdi.h, which are included in windows.h - in turn included in stdafx.h.

2. Get the device context of the Desktop by calling the GetDC API and passing it the Desktop handle (HWND_DESKTOP).

3. We can now determine the screen dimensions.

4. Next, we create a compatible memory context and bitmap.

If this was successful, hBitmap should not be NULL, i.e. "if (hBitmap)" should evaluate to true.

5. Assuming step 4 was successful, we can select the bitmap into the memory context we just created.

6. Next, we copy the Desktop to the memory device context.

7. The clipboard - open, clear, copy, and close.

OK, not so fast. We opened the clipboard providing the handle to our application window. In the sample code, there is no application window, so we passed NULL. This associated the Clipboard with the current task.

When we emptied the Clipboard, we both cleared it's current contents and temporarily took ownership of the Clipboard. We placed the bitmap data onto the Clipboard by specifying the data type with CF_BITMAP and providing a handle to our data. Then, we released ownership of the Clipboard.

8. Finally, we cleanup by deleting the memory context and releasing the device context.

The completed procedure is as follows:


Copyright © 2001-2005 sebastianfernandes.com | Terms of Use

Valid XHTML 1.0!