Capture and Print the Screen, a Form, or any Window

This program shows how to capture any form or window, including the screen or desktop window, into a picture object. Then the image can be sent to the printer using the PaintPicture method of Visual Basic's Printer object. This program illustrates how to:

  • Capture the entire contents of a form.
  • Capture the client area of a form.
  • Capture the entire screen.
  • Capture the active window.
  • Capture any portion of any window.
  • Create a Picture object from a bitmap and a palette.
  • Print a Picture object as large as possible on the page.

 

Capture and Print a Form, the Screen, or any Window.
Download Source Code

Capturing and Printing a Window's Contents

Visual Basic contains a Picture object which is really a standard OLE type and is documented in the Control Developer's Kit (CDK). The CDK includes the OleCreatePictureIndirect method which can be used to create new Picture objects. The CreateBitmapPicture procedure in the program calls OleCreatePictureIndirect to build a Picture object from a handle to a bitmap and a handle to a palette. If the Picture includes a valid handle, VB will know how to use it when rendering the Picture to the screen or printer. The CreateBitmapPicture routine is used by the CaptureWindow procedure to construct a Picture object containing a bitmap of all or part of a window.

The CaptureWindow procedure captures any part of a window given a window handle. The routine includes several parameters for describing the exact portion of the window to capture. CaptureWindow works by copying the on-screen image of a window into a new bitmap. It also checks to see if the screen has a palette and if so it copies it. CaptureWindow calls CreateBitmapPicture to build a bitmap from the newly created bitmap and palette.

The CaptureForm, CaptureClient, CaptureScreen, and CaptureActiveWindow procedures all use CaptureWindow to capture specific windows.

Once the desired image is captured it can be printed using the PaintPicture method of the Printer object. Also, the image can easily be copied to the Clipboard using its SetData method.

Notes

This sample is based on Microsoft's Knowledge Base article Q161299.

API Functions Used

This app uses a ton of cool APIs such as: CreateCompatibleBitmap, GetDeviceCaps, GetSystemPaletteEntries, CreateCompatibleDC, CreatePalette, SelectPalette, RealizePalette, BitBlt, GetDC, ReleaseDC, GetWindowRect, GetDesktopWindow and OleCreatePictureIndirect.

Instructions

Run the code and experiment. The Print Picture option scales the captured image to fit on a printed page. The image is sized vertically or horizontally which ever is most appropriate. You can resize the form to better view the captured image.




About TheScarms
About TheScarms


Sample code
version info

If you use this code, please mention "www.TheScarms.com"

Email this page


© Copyright 2024 TheScarms
Goto top of page