Owner-Draw: Overview and Sources of Information

Last reviewed: March 11, 1996
Article ID: Q64327
The information in this article applies to:
  • Microsoft Windows Software Development Kit (SDK) versions 3.0 and 3.1
  • Microsoft Win32 Application Programming Interface (API) included with:

        - Microsoft Windows NT versions 3.5 and 3.51
        - Microsoft Windows 95 version 4.0
    

SUMMARY

Owner-draw controls are a new feature of Windows version 3.0. Because your application does all the drawing of the contents of the controls, you can customize them any way you like. Owner-draw controls are similar to predefined controls in that Windows will handle the control's functionality and mouse and keyboard input processing. However, you are responsible for drawing the owner-draw control in its normal, selected, and focus states.

You can create owner-draw controls from the menu, button, and list-box classes. You can create owner-draw combo boxes, but they must have the CBS_DROPDOWNLIST style (equates to a static text item and a list box). The elements of an owner-draw control can be composed of strings, bitmaps, lines, rectangles, and other drawing functions in any combination, in your choice of colors.

MORE INFORMATION

The Windows SDK sample application MENU demonstrates owner-draw menu items. The SDK sample application OWNCOMBO is a fairly large example of owner-draw and predefined list boxes and combo boxes.

The Microsoft Software Library contains simplified examples of an owner- draw push button, owner-draw list boxes, and an owner-draw drop-down list style combo box. Each of these examples includes descriptive text in a related Knowledge Base article. For additional information, please see the following article(s) in the Microsoft Knowledge Base:

   ARTICLE-ID: Q64326
   TITLE     : Owner-Draw: Handling WM_DRAWITEM for Drawing Controls

   -or-

   ARTICLE-ID: Q64328
   TITLE     : Owner-Draw: 3-D Push Button made from Bitmaps with Text

   -or-

   ARTICLE-ID: Q65792
   TITLE     : Owner-Draw Example: Right - and Decimal Alignment

To respond to a WM_MEASUREITEM message, you MUST specify the height of the appropriate item in your control. Optionally, you can specify the item's width as well.

If you want to do something special when a string is deleted from a list box, you should process WM_DELETEITEM messages. Windows's default action is to erase the deleted string and to redraw the list box.

If you want to have control over the sorting for the order of items in a list box or combo box that does not have the *_HASSTRINGS style, you should specify the appropriate *_SORT style and process WM_COMPAREITEM messages. If the *_SORT and *_HASSTRINGS styles are present, Windows will automatically do the sorting without sending WM_COMPAREITEM messages. If *_SORT is not specified, WM_COMPAREITEM messages will not be generated and items will be displayed in the list box in the order in which they were inserted.

The heart of owner-draw controls is the response to WM_DRAWITEM messages. During this processing is when you draw an entire button or each individual item in a menu, list box, or combo box. Because Windows does not interfere in the drawing of owner-draw controls, your application must draw the specified control item. The display of the control must indicate the state of the control. Common states are as follows:

  1. Focus state (has the focus or not)

  2. Selection state (selection or not)

  3. Emphasis state (active, grayed, or disabled) (less common to process)

See the article titled "Owner-Draw: Handling WM_DRAWITEM for Drawing Controls" for information about drawing controls in their various states. Familiarity with the WM_DRAWITEM message and the various control states is extremely helpful before trying to follow the code examples.

Under Windows 95, the BS_BITMAP style allows buttons to display bitmaps without using owner-draw.


Additional reference words: 3.00 3.10 3.50 4.00 owndraw od owner draw
KBCategory: kbui
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: March 11, 1996
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.