How To Calculate Dialog Base Units with Non-System-Based Font

This article was previously published under Q125681
This article demonstrates how to calculate the dialog base unit for thedialog box using a font other than System Font in Windows 95. You can usethis calculation to build dialog box templates in memory or calculatedialog box dimensions.
Each dialog box template contains measurements that specify the position,width, and height of the dialog box and the controls it contains. Thesemeasurements are device-independent, so an application can use a singletemplate to create the same dialog box for all types of display devices.This ensures that a dialog box will have the same proportions andappearance on all screens despite differing resolutions and aspect ratiosbetween screens.

Further, dialog box measurements are given in dialog base units. Onehorizontal base unit is equal to one-fourth of the average character widthfor the system font. One vertical base unit is equal to one-eighth of theaverage character height for the system font. An application can retrievethe number of pixels per base unit for the current display by using theGetDialogBaseUnits function. The low-order word of the return value, fromthe GetDialogBaseUnits function, contains the horizontal base units and thehigh-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 dialogbox 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 acontrol also changes.

One dialog box base unit is equivalent to the number of pixels per dialogbox unit as follows:
1 horz dialog base unit == (2 * average char width  dialog font /                                 average char width  system font) pixels1 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 = (;avgHeight = (WORD)tm.tmHeight;				
The tmAveCharWidth field of the TEXTMETRIC structure only approximates theactual average character width (usually it gives the width of the letter"x") and so the true average character width must be calculated to matchthe value used by the system.

You can use the MapDialogRect function to convert dialog box units intopixels, but there is no function that will convert pixels into dialog boxunits. You can use the formulas shown here to perform this conversion.
For more information on this topic, please see the following article in the Microsoft Knowledge Base:
145994 How To Calculate Dialog Units When Not Using the System Font

Article ID: 125681 - Last Review: 07/11/2005 19:29:53 - Revision: 2.3

Microsoft Platform Software Development Kit-January 2000 Edition

  • kbdlg kbhowto KB125681