SAMPLE: DRAGBMP Demonstrates Dragging Bitmaps on the Screen

Last reviewed: February 15, 1996
Article ID: Q97188
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:

  1. A bitmap of the desired image, such as the cards in Solitaire or the domino used in this sample.

  2. A bitmap of what lies underneath the desired image. This information is necessary to redraw the underlying area when the image is dragged elsewhere.

  3. 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:

  1. Calculate the delta x and delta y in the mouse movement (dx,dy).

  2. 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.

  3. 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.

  4. BitBlt() the domino's bitmap to the screen at its new location: origin at point (x',y') in the figure above.

  5. 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.

  6. 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:

  • Microsoft Download Service (MSDL)

          Dial (206) 936-6735 to connect to MSDL
          Download DRAGBMP.EXE (size: 41906 bytes) 
    
  • Internet (anonymous FTP)

          ftp ftp.microsoft.com
          Change to the \SOFTLIB\MSLFILES directory
          Get DRAGBMP.EXE (size: 41906 bytes) 
    


Additional reference words: 3.10 softlib DRAGBMP.EXE
KBCategory: kbprg kbfile
KBSubcategory: GdiBmp


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: February 15, 1996
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.