Tip 48: Copying a Window's Client Area to a Bitmap

Created: April 6, 1995

Abstract

In a Visual Basic® application, you may want to save the contents of a window's client area to the Windows® Clipboard application. You can save data to the Clipboard in a variety of formats. This article explains how the client area of a window can be saved to the Clipboard in Windows as a bitmap file.

Creating Bitmap Formats in the Clipboard

To save a window's client area to a bitmap format in the Windows® Clipboard application, you need to create a memory device context that is compatible with the bitmap file format. (A memory device context is simply a block of memory that represents a display surface, such as a window.) Next, you use the SelectObject function to save the object. The BitBlt function is then used to copy the image from the memory device context to a bitmap.

The Windows application programming interface (API) provides several functions that let your program modify the Clipboard application. In the example below, we first open communication with the Clipboard by calling the OpenClipboard function. Likewise, when we have finished using the Clipboard, we execute the CloseClipboard function. To actually write data to the Clipboard, we call the SetClipboardData function.

To declare the SetClipboardData function within your program, include the following Declare statement in the Global Module or General Declarations section of your form:

Declare Function SetClipboardData Lib "User" (ByVal wFormat As Integer, ByVal 
   hMem As Integer) As Integer

Note that this statement must be typed as one single line of code.

The SetClipboardData function requires two arguments, as follows:

wFormat An integer value containing the Clipboard format that should be used to write data to the Clipboard. The CONSTANT.TXT file contains a list of the Clipboard data formats that can be used.
hMem An integer value containing the memory block's global memory handle. This data must be in the same format as specified by the wFormat argument.

Because we want to save the contents of a window to the Clipboard, we call the SetClipboardData function by passing it the MF_BITMAP constant, which tells the function to save the window's data in the Clipboard in bitmap file format.

Example Program

The following program shows how to copy the contents of a window's client area (the entire window, in this case) to the Clipboard in bitmap format. After executing this program, you can verify that the data was saved to the Clipboard by running Clipboard Viewer from the Accessories group in Program Manager.

  1. Create a new project in Visual Basic. Form1 is created by default.

  2. Add the following Constant and Declare statements to the General Declarations section of Form1 (note that each statement must be typed as a single line of text):
    Declare Function BitBlt Lib "GDI" (ByVal hDestDC As Integer, ByVal X As Integer, 
       ByVal Y As Integer, ByVal nWidth As Integer, ByVal nHeight As Integer, ByVal 
       hSrcDC As Integer, ByVal XSrc As Integer, ByVal YSrc As Integer, ByVal dwRop 
       As Long) As Integer
    
    Declare Function GetWindowDC Lib "User" (ByVal hWnd As Integer) As Integer
    
    Declare Function CreateCompatibleDC Lib "GDI" (ByVal hDC As Integer) As Integer
    
    Declare Function CreateCompatibleBitmap Lib "GDI" (ByVal hDC As Integer, ByVal 
       nWidth As Integer, ByVal nHeight As Integer) As Integer
    
    Declare Function SelectObject Lib "GDI" (ByVal hDC As Integer, ByVal hObject As 
       Integer) As Integer
    
    Declare Function OpenClipboard Lib "User" (ByVal hWnd As Integer) As Integer
    
    Declare Function SetClipboardData Lib "User" (ByVal wFormat As Integer, ByVal 
       hMem As Integer) As Integer
    
    Declare Function CloseClipboard Lib "User" () As Integer
    
    Const SRCCOPY = &HCC0020
    Const MF_BITMAP = &H4
    
  3. Add the following code to the Form_Load event for Form1:
    Sub Form_Load()
      Dim hwnddc As Integer
      Dim hmemdc As Integer
      Dim hbitmap As Integer
      Dim oldbitmap As Integer
      Dim res As Integer
      Form1.ScaleMode = 3
      hwndc% = GetWindowDC(Form1.hWnd)
      hmemdc% = CreateCompatibleDC(Form1.hDC)
      hbitmap% = CreateCompatibleBitmap(Form1.hDC, Form1.ScaleWidth, Form1.ScaleWidth)
    
      oldbitmap% = SelectObject(hmemdc%, hbitmap%)
    
      res% = BitBlt(hmemdc%, 0, 0, Form1.ScaleWidth, Form1.ScaleHeight, hwndc%, 0, 0, SRCCOPY)
    
      res% = OpenClipboard(Form1.hWnd)
      res% = SetClipboardData(hbitmap%, MF_BITMAP)
      res% = CloseClipboard()
    End Sub
    

Additional References

"Tip 31: Creating the Windows Wallpaper Effect."