You are on page 1of 2

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 cl
ient area to the Windows Clipboard application. You can save data to the Clipboar
d 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 applic
ation, you need to create a memory device context that is compatible with the bi
tmap file format. (A memory device context is simply a block of memory that repr
esents a display surface, such as a window.) Next, you use the SelectObject func
tion 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 t
hat let your program modify the Clipboard application. In the example below, we
first open communication with the Clipboard by calling the OpenClipboard functio
n. Likewise, when we have finished using the Clipboard, we execute the CloseClip
board function. To actually write data to the Clipboard, we call the SetClipboar
dData function.
To declare the SetClipboardData function within your program, include the follow
ing Declare statement in the Global Module or General Declarations section of yo
ur 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 Clipbo
ard data formats that can be used.
hMem An integer value containing the memory block's global memory handle. This d
ata 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 S
etClipboardData function by passing it the MF_BITMAP constant, which tells the f
unction 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 execut
ing this program, you can verify that the data was saved to the Clipboard by run
ning Clipboard Viewer from the Accessories group in Program Manager.
Create a new project in Visual Basic. Form1 is created by default.
Add the following Constant and Declare statements to the General Declarations se
ction 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
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.ScaleWidt
h)
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

You might also like