{"id":107350,"date":"2022-11-03T07:00:00","date_gmt":"2022-11-03T14:00:00","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/oldnewthing\/?p=107350"},"modified":"2022-11-04T11:15:07","modified_gmt":"2022-11-04T18:15:07","slug":"20221103-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20221103-00\/?p=107350","title":{"rendered":"How does the dialog manager calculate the average width of a character?"},"content":{"rendered":"<p>Some time ago, I explained that <a href=\"https:\/\/devblogs.microsoft.com\/oldnewthing\/20211021-00\/?p=105818\"> the <code>Map\u00adDialog\u00adRect<\/code> function requires the handle to a dialog box<\/a> 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.<\/p>\n<p>I noted that if you don&#8217;t have a dialog box, and you don&#8217;t want to find or make one, then you can simulate the calculations yourself using the standard formulas:<\/p>\n<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td align=\"right\">8 vertical dlu =\u00a0<\/td>\n<td>1 character tall<\/td>\n<\/tr>\n<tr>\n<td align=\"right\">4 horizontal dlu =\u00a0<\/td>\n<td>1 average character wide<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>According to <a href=\"https:\/\/jeffpar.github.io\/kbarchive\/kb\/145\/Q145994\/\"> Knowledge Base article Q145994<\/a>, the calculation of the character height and width are performed as follows:<\/p>\n<p>For height, call <code>GetTextMetrics<\/code> and use the <code>tmHeight<\/code>.<\/p>\n<p>For average width, get the text extent of the string<\/p>\n<pre>ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz\r\n<\/pre>\n<p>and divide it by 52, rounding to the nearest integer. Do not use the <code>tmAveCharWidth<\/code> from the text metrics. Despite its name, it is not the average of anything. It&#8217;s just the width of the character <tt>x<\/tt>.<\/p>\n<p><b>Bonus chatter<\/b>: Maybe the font people interpreted it to mean &#8220;the width of an average character&#8221;, rather than &#8220;the average width of a character.&#8221;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>It&#8217;s a simple formula, perhaps too simple.<\/p>\n","protected":false},"author":1069,"featured_media":111744,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[25],"class_list":["post-107350","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>It&#8217;s a simple formula, perhaps too simple.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/107350","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/users\/1069"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/comments?post=107350"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/107350\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media\/111744"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media?parent=107350"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=107350"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=107350"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}