November 3rd, 2022

How does the dialog manager calculate the average width of a character?

Some time ago, I explained that the Map­Dialog­Rect function requires the handle to a dialog box because the mapping from dialog units to pixels is dependent upon the default font of the dialog box, so you need to know which dialog box you are converting.

I noted that if you don’t have a dialog box, and you don’t want to find or make one, then you can simulate the calculations yourself using the standard formulas:

8 vertical dlu =  1 character tall
4 horizontal dlu =  1 average character wide

According to Knowledge Base article Q145994, the calculation of the character height and width are performed as follows:

For height, call GetTextMetrics and use the tmHeight.

For average width, get the text extent of the string

ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz

and divide it by 52, rounding to the nearest integer. Do not use the tmAveCharWidth from the text metrics. Despite its name, it is not the average of anything. It’s just the width of the character x.

Bonus chatter: Maybe the font people interpreted it to mean “the width of an average character”, rather than “the average width of a character.”

Topics
Code

Author

Raymond has been involved in the evolution of Windows for more than 30 years. In 2003, he began a Web site known as The Old New Thing which has grown in popularity far beyond his wildest imagination, a development which still gives him the heebie-jeebies. The Web site spawned a book, coincidentally also titled The Old New Thing (Addison Wesley 2007). He occasionally appears on the Windows Dev Docs Twitter account to tell stories which convey no useful information.

2 comments

Discussion is closed. Login to edit/delete existing comments.

  • skSdnW

    Pretty sad that you have to link to a 3rd-party because Microsoft keeps deleting useful KB articles for some insane reason.

    • sugrob 9000

      Similar to the treatment of the blog platform.