{"id":34163,"date":"2005-09-16T10:00:15","date_gmt":"2005-09-16T10:00:15","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2005\/09\/16\/fiddling-with-the-fonts-part-2-keeping-the-english-font-small\/"},"modified":"2005-09-16T10:00:15","modified_gmt":"2005-09-16T10:00:15","slug":"fiddling-with-the-fonts-part-2-keeping-the-english-font-small","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20050916-15\/?p=34163","title":{"rendered":"Fiddling with the fonts, part 2: Keeping the English font small"},"content":{"rendered":"<p><P>\nWe concluded last time that we wanted the custom large font to\napply only to the columns containing Chinese characters and\nleave the original font in place for the English columns.\nWe do this by carrying two fonts around, choosing the appropriate\none for each column.\n<\/P>\n<PRE>\nclass RootWindow : public Window\n{\n &#8230;\nprivate:\n HWND m_hwndLV;\n HWND m_hwndEdit;\n HWND m_hwndLastFocus;\n HFONT m_hfChinese;\n <FONT COLOR=\"blue\">HFONT m_hfNormal;<\/FONT>\n int  m_cyEdit;\n &#8230;\n}<\/p>\n<p>RootWindow::RootWindow()\n : m_hfChinese(NULL)\n <FONT COLOR=\"blue\">, m_hfNormal(NULL)<\/FONT>\n{\n}<\/p>\n<p>RootWindow::~RootWindow()\n{\n if (m_hfChinese) DeleteObject(m_hfChinese);\n <FONT COLOR=\"blue\">if (m_hfNormal) DeleteObject(m_hfNormal);<\/FONT>\n}<\/p>\n<p>LRESULT RootWindow::OnCreate()\n{\n &#8230;\n ListView_SetExtendedListViewStyleEx(m_hwndLV,\n                                     LVS_EX_FULLROWSELECT,\n                                     LVS_EX_FULLROWSELECT);<\/p>\n<p> LOGFONT lf;\n if (!GetObject(GetWindowFont(m_hwndLV), sizeof(lf), &amp;lf)) {\n  return -1;\n }\n <FONT COLOR=\"blue\">m_hfNormal = CreateFontIndirect(&amp;lf);\n if (!m_hfNormal) return -1;<\/FONT>\n lf.lfHeight += lf.lfHeight \/ 2; \/\/ 50% bigger\n m_hfChinese = CreateFontIndirect(&amp;lf);\n if (!m_hfChinese) return -1;\n SetWindowFont(m_hwndLV, m_hfChinese, FALSE);<\/FONT>\n &#8230;\n}\n<\/PRE>\n<P>\nBefore we change the default font for the list view to the\nChinese font, we create a copy of the original font\n(which we rather presumptuously call &#8220;normal&#8221;) for safekeeping.\nNext, when the list view asks us to customize a column,\nwe select the appropriate font and return the &#8220;I also changed the\nfont&#8221; code.\n<\/P>\n<PRE>\nLRESULT RootWindow::OnLVCustomDraw(NMLVCUSTOMDRAW* pcd)\n{\n &#8230;\n case CDDS_ITEMPREPAINT | CDDS_SUBITEM:\n  pcd-&gt;clrText = m_clrTextNormal;\n  if (pcd-&gt;iSubItem == COL_SIMP &amp;&amp;\n    pcd-&gt;nmcd.dwItemSpec nmcd.dwItemSpec);\n    if (de.m_pszSimp) {\n      pcd-&gt;clrText = RGB(0x80, 0x00, 0x00);\n    }\n  }\n  <FONT COLOR=\"blue\"><STRIKE>\/\/ break;<\/STRIKE>\n  if (pcd-&gt;iSubItem == COL_TRAD || pcd-&gt;iSubItem == COL_SIMP) {\n    SelectFont(pcd-&gt;nmcd.hdc, m_hfChinese);\n  } else {\n    SelectFont(pcd-&gt;nmcd.hdc, m_hfNormal);\n  }\n  return CDRF_NEWFONT;<\/FONT>\n &#8230;\n}\n<\/PRE>\n<P>\nThere are several important details here.\n<\/P>\n<P>\nFirst, we set the Chinese font as the\n&#8220;overall&#8221; font for the list view.\nIt would have been easier for us not to do this;\nafter all, since we explicitly set the font for each column,\nwhy does it matter what the default font is?\nIt also would have removed the need to create a copy of the original\nfont.\nBut if you delete the <CODE>SetWindowFont(m_hwndLV, m_hfChinese);<\/CODE>\nline,\nthe bottoms of the Chinese characters get cut off.\nThe reason is that the list view uses the default font to decide\nwhat the line spacing should be.\nTherefore, the default font for the list view needs to be the\nlargest font we intend to use for any column.\n<\/P>\n<P>\nWhy does the list view use the default font to decide on the line\nspacing?\nBecause it&#8217;s not clairevoyant.\nThat&#8217;s the only font it has, after all.\nIt doesn&#8217;t know what font you&#8217;re going to select in your\n<CODE>CDDS_ITEMPREPAINT | CDDS_SUBITEM<\/CODE> notification handler.\nAll it has is the font you set with <CODE>SetWindowFont<\/CODE>.\n<\/P>\n<P>\nAnother important detail is that once we have decided to use\ndifferent fonts for different columns, we are committed to selecting\na font for <STRONG>all<\/STRONG> columns.\nThe reason for this was discussed when we discussed\n<A HREF=\"http:\/\/blogs.msdn.com\/oldnewthing\/archive\/2005\/07\/13\/438381.aspx\">\nhow to colorize the columns<\/A>.\n<\/P>\n<P>\nFinally, there is the important detail of returning the\n<CODE>CDRF_NEWFONT<\/CODE> value when we change the font.\nFor performance reasons, the list view assumes you aren&#8217;t\nchanging the font on a subitem-by-subitem basis\n(since very few list views do) and it caches many font\nproperties to avoid having to recalculate them all the time.\nReturning\n<CODE>CDRF_NEWFONT<\/CODE> indicates that the list view should\nlook at the font you selected and base its computations on that font\ninstead.\n<\/P>\n<P>\nSince boldface, italics and underline are font attributes,\nyou can use this &#8220;select a custom font&#8221; technique to make\nselected items display as boldface, italics, or underline,\nin addition to using it to change the font size as we did here.\n<\/P>\n<P>\nThat&#8217;s all for this month.\nNext month will be a rather boring one,\nadding a status bar to make the Chinese characters even more\nreadable.\nAfter that, we&#8217;ll enhance the dictionary lookup algorithm,\nwhich is itself groundwork for dynamic translation, as I may\nhave alluded to in a previous entry.\n<\/P>\n<P>\n[Raymond is currently away; this message was pre-recorded.]\n<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>We concluded last time that we wanted the custom large font to apply only to the columns containing Chinese characters and leave the original font in place for the English columns. We do this by carrying two fonts around, choosing the appropriate one for each column. class RootWindow : public Window { &#8230; private: HWND [&hellip;]<\/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-34163","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>We concluded last time that we wanted the custom large font to apply only to the columns containing Chinese characters and leave the original font in place for the English columns. We do this by carrying two fonts around, choosing the appropriate one for each column. class RootWindow : public Window { &#8230; private: HWND [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/34163","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=34163"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/34163\/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=34163"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=34163"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=34163"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}