SAMPLE: Owner-Draw: 3-D Push Button Made from Bitmaps with Text

Last reviewed: February 15, 1996
Article ID: Q64328
The information in this article applies to:
  • Microsoft Windows Software Development Kit (SDK) for Windows versions 3.0 and 3.1

SUMMARY

An owner-draw button can be defined and drawn to provide any desired appearance, even to mimic the three-dimensional push buttons of Windows version 3.0. With an owner-draw button, the application can control the button face color, unlike a standard push button, which always has a gray face.

MORE INFORMATION

ODBUTTON is a file in the Microsoft Software Library that contains an example of an owner-draw button. The button uses two 64-by-64-bit bitmaps. The first bitmap represents the default button state. In this state, the button has a light gray face, a light left and upper border, and the button's text is centered in the bitmap. The second bitmap represents the pressed button state. In this state, the button has a solid, dark gray face, and the button's text is shifted to the right and down two pixels.

Download ODBUTTON.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 ODBUTTON.EXE (size: 25257 bytes) 
    
  • Internet (anonymous FTP)

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

The ODBUTTON sample source code can be used as a template for creating other applications. The main task involves creating appropriate bitmaps.

Under Windows 2.x, an application can change the color of the button face by processing WM_CTLCOLOR messages. This functionality is not available in Windows 3.0. However, the more powerful owner-draw feature can be used to achieve the same effect.

Like a Windows 3.0 standard push button, this sample owner-draw button indicates that it has the focus by drawing a dashed, black rectangular border around the button text. This is accomplished by using the new Windows 3.0 function, DrawFocusRect(). Like the text of the button, the focus rectangle must also be shifted right and down two pixels when the button is pressed.

Bitmaps are created in a fixed size, in pixels, regardless of the display resolution used. An application can determine which display is in use by reading the SYSTEM.INI file. Two alternatives are available to create buttons that are an appropriate size on different displays, as follows:

  1. Create different pairs of default/pressed bitmaps for each display resolution the application is targeted (for example, Hercules monochrome, CGA, EGA, VGA, super VGA, and 8514).

  2. Create separate bitmaps for the borders of the button and for the button faces. During the initialization of the application, make four calls to StretchBlt(). The first call should modify the left and right borders vertically. The second call should change the top and bottom borders horizontally. The third and fourth calls should modify each button face bitmap both horizontally and vertically. Doing separate calls eliminates distortion to the border widths as a display-specific bitmap is created.

Apart from the information discussed above, owner-draw buttons are handled like other owner-draw controls. However, Windows does not send the application a WM_MEASUREITEM message to define the dimensions of the button control dynamically. The dimensions of the owner-draw button are specified in a dialog box template or CreateWindow() call, similar to the dimensions of nonowner-draw buttons.

Like other owner-draw controls, the application draws the owner-draw button in response to a WM_DRAWITEM message. Drawing should take into account the selection and focus states of the control, as well as the normal state without either selection or focus.


Additional reference words: 3.00 3.10 softlib ODBUTTON.EXE
KBCategory: kbprg kbfile
KBSubcategory: UsrCtl


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.