The information in this article applies to:
- Microsoft Windows Software Development Kit (SDK) for Windows
version 3.1
SUMMARY
DRAGBMP shows how to drag images smoothly on the screen. This is achieved
through the use of several bitmaps and a series of BitBlt operations.
Before you can drag a bitmap, you must store the following information:
- A bitmap of the desired image, such as the cards in Solitaire or
the domino used in this sample.
- A bitmap of what lies underneath the desired image. This
information is necessary to redraw the underlying area when the
image is dragged elsewhere.
- The current position of the image (you can also store or retrieve
the width and height of the image using GetObject() in the image's
bitmap).
Once this information is stored, the image is ready to drag. The user
selects the image by clicking the left mouse button when the mouse pointer
is on the image, drags the image by moving the mouse while the mouse button
is depressed, and ends the dragging by releasing the mouse button.
In this sample, the image is a yellow domino. Choose Draw Image! to draw
the domino, then drag the domino using the mouse. Hit-testing in
WM_LBUTTONDOWN determines when the domino is selected. Once selected, the
dragging process begins. On each WM_MOUSEMOVE, a new background bitmap is
created, the old background bitmap is restored, and the domino is moved to
its new location. The WM_LBUTTONUP message ends the dragging process.
(x',y')
+------------------------- new position
|\ |
| \(dx,dy) |
| \ |
| + . . . . . . . . . . . . . . . . . old position
| .(x,y) | .
| . | .
| . | .
----.--------------------- .
. .
. .
. . . . . . . . . . . . . . . . . .
The basic steps to perform the bitmap dragging are as follows:
- Calculate the delta x and delta y in the mouse movement (dx,dy).
- BitBlt() the screen at the new position onto a new bitmap. This
corresponds to a rectangle with origin at (x',y') in the figure
above, and becomes the new background bitmap.
- Since the new background bitmap still contains part of the domino's
image starting at point (x,y) in the figure above, BitBlt() the old
background bitmap to that point on the new background bitmap,
offsetting it by dx and dy. The resulting bitmap contains the
correct background for the new position.
- BitBlt() the domino's bitmap to the screen at its new location:
origin at point (x',y') in the figure above.
- The domino is now in the correct position on the screen and we have
a bitmap of what is underneath, but a portion of the domino still
appears at the lower right. To erase this portion, first BitBlt() the
bitmap of the domino onto the old background's bitmap, offsetting
it by -dx and -dy, and then BitBlt this modified old background
bitmap to the screen at point (x,y). This erases the old portion of
the domino without flashing.
- Dragging is now complete. Store the new background bitmap and
delete the old one.
The DRAG.C file contains the code for dragging and hit-testing. The
MAINWND.C file contains the code for processing the mouse and paint
messages.
DRAGBMP was built and tested under Microsoft Windows version 3.1.
Download DRAGBMP.EXE, a self-extracting file, from the Microsoft Software
Library (MSL) on the following services: