HOWTO: Calculate Dialog Base Units with Non-System-Based Font

Last reviewed: February 2, 1998
Article ID: Q125681
The information in this article applies to:
  • Microsoft Win32 Application Programming Interface (API) included with:

        - Microsoft Windows 95
    

SUMMARY

This article demonstrates how to calculate the dialog base unit for the dialog box using a font other than System Font in Windows 95. You can use this calculation to build dialog box templates in memory or calculate dialog box dimensions.

MORE INFORMATION

Each dialog box template contains measurements that specify the position, width, and height of the dialog box and the controls it contains. These measurements are device independent, so an application can use a single template to create the same dialog box for all types of display devices. This ensures that a dialog box will have the same proportions and appearance on all screens despite differing resolutions and aspect ratios between screens.

Further, dialog box measurements are given in dialog base units. One horizontal base unit is equal to one-fourth of the average character width for the system font. One vertical base unit is equal to one-eighth of the average character height for the system font. An application can retrieve the number of pixels per base unit for the current display by using the GetDialogBaseUnits function. The low-order word of the return value, from the GetDialogBaseUnits function, contains the horizontal base units and the high-order word of the return value, from the GetDialogBaseUnits function, contains the vertical base units.

Using this information, you can compute the dialog base units for a dialog box using font other than system font:

horz pixels == 2 * horz dialog units * (average char width of dialog font

                        / average char width of system font)
vert pixels == 2 * vert dialog units * (average char height of dialog font
                        / average char height of system font)

As the font of a dialog box changes, the actual size and position of a control also changes.

One dialog box base unit is equivalent to the number of pixels per dialog box unit as follows:

1 horz dialog base unit == (2 * average char width dialog font /

                                average char width  system font) pixels
1 vert dialog base unit == (2 * average char height dialog font /
                                average char height system font) pixels

Average character width and height of a font can be computed as follows:

hFontOld = SelectObject(hdc,hFont); GetTextMetrics(hdc,&tm); GetTextExtentPoint32(hdc,"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrst"

            "uvwxyz",52,&size);
avgWidth = (size.cx/26+1)/2; avgHeight = (WORD)tm.tmHeight;

The tmAveCharWidth field of the TEXTMETRIC structure only approximates the actual average character width (usually it gives the width of the letter "x") and so the true average character width must be calculated to match the value used by the system.

You can use the MapDialogRect function to convert dialog box units into pixels, but there is no function that will convert pixels into dialog box

units. You can use the formulas shown here to perform this conversion.

Keywords          : UsrDlgs
Version           : WIN95
Platform          : Win95
Issue type        : kbhowto


================================================================================


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