SAMPLE: Right and Decimal-Alignment in Owner-Draw Controls

Last reviewed: July 22, 1997
Article ID: Q65792
3.00 3.10 WINDOWS kbprg kbfile kbcode

The information in this article applies to:

  • Microsoft Windows Software Development Kit (SDK) for Windows versions 3.0 and 3.1

SUMMARY

In the Microsoft Windows graphical environment, the primary feature of an owner-draw control is the ability to customize the manner that a control displays information and to extend its capabilities. An example of a customized presentation is to decimal align a column of numbers in an owner- draw list box. This article describes how to perform this alignment, and then describes some example code in the Microsoft Software Library.

ODLIST2 is a file in the Microsoft Software Library that demonstrates this method to right align a substring of digits. The ODLIST2 application implements a horizontally scrollable list box that contains a small database of account records. The titles of the "Account", "Balance", and "Name" fields scroll with their associated column data because the application subclasses the main database list box to process WM_HSCROLL messages. When a scroll message is received by the data list box, it sends a corresponding WM_HSCROLL message to the title list box.

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

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

MORE INFORMATION

To right align a string (of digits or of text) at a particular horizontal position "X," the application must compute the length of the string. Then, move that distance to the left of "X" before drawing the string. The width of a string is contained in the low-order word of the value returned by the GetTextExtent() or GetTabbedTextExtent() functions.

For example, if the following are true

   X         is the horizontal position for the right edge of the
                text
   hDC       is a handle to a device context
   szDigits  is an array of characters

the right-aligned text would be drawn starting at the following position:

   X - LOWORD(GetTextExtent(hDC, szDigits, lstrlen(szDigits))

Because digit characters have equal widths, aligning the right side of the digit strings in an owner-draw list box to the same horizontal position keeps all the columns lined up. If the decimal point is located consistently, as for dollars and cents, the numbers are easily aligned. For example:

     123.45
    6543.12

One technique to right align substrings is to use multiple calls to the TextOut function when drawing each list box item. Calculate the origin of each substring as follows:

   (the desired right position) - (the width of the substring)

Another way to right align a string or substring of digits is to use tab stops. A tab can be defined so that its position, plus the width of the digit string, is equal to the desired right position "X."

The code sample below implements a tab stop before a digit substring. To use this code, place it into the code that processes the WM_DRAWITEM message for an owner-draw list box with the LBS_USETABSTOPS style:

   nWidth     is the width (x-extent) of the digit substring.
   lpdis      is the far pointer to a DRAWITEMSTRUCT obtained as the
                 lParam in a WM_DRAWITEM message.
   nTabStops  is an integer array of tab stops. If the current mapping
                 mode is MM_TEXT, the values in the array are in
                 pixels.

   // Get the x extent of digit substring
   nWidth = LOWORD(GetTextExtent(lpdis->hDC, szDigits,
                lstrlen(szDigits)));

   // Set first tab stop to align the right side of the digit
   // substring with the right border of the list box
   nTabStops[0] = X - nWidth;

   // Draw digit string at tabbed position
   TabbedTextOut(lpdis->hDC, lpdis->rcItem.left, lpdis->rcItem.top,
        (LPSTR)szDigits, lstrlen(szDigits), 1, (LPINT)nTabStops);


Additional reference words: 3.00 3.10 listbox softlib ODLIST2.EXE
KBCategory: kbprg kbfile kbcode
KBSubcategory: UsrCtl
Keywords : kb16bitonly UsrCtl kbcode kbfile kbprg
Version : 3.00 3.10
Platform : WINDOWS


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: July 22, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.