Right and Decimal-Alignment in Owner-Draw Controls

ID: Q65792


The information in this article applies to:


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.


MORE INFORMATION

The following file is available for download from the Microsoft Software Library:

~ Odlist2.EXE

For additional information about downloading files from the Microsoft Software Library, please see the following article in the Microsoft Knowledge Base:

Q119591 How to Obtain Microsoft Support Files from Online Services


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 query words:


Keywords          : kbfile kbsample kb16bitonly kbComboBox kbCtrl kbListBox kbSDKPlatform kbGrpUser kbUser 
Version           : WINDOWS:3.1
Platform          : WINDOWS 
Issue type        : kbinfo 

Last Reviewed: June 9, 1999