{"id":3813,"date":"2013-07-15T07:00:00","date_gmt":"2013-07-15T07:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2013\/07\/15\/a-program-for-my-nieces-the-abcs-part-2-choosing-a-font\/"},"modified":"2013-07-15T07:00:00","modified_gmt":"2013-07-15T07:00:00","slug":"a-program-for-my-nieces-the-abcs-part-2-choosing-a-font","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20130715-00\/?p=3813","title":{"rendered":"A program for my nieces: The ABCs, part 2, choosing a font"},"content":{"rendered":"<p>\nI added a feature to my\n<a HREF=\"http:\/\/blogs.msdn.com\/b\/oldnewthing\/archive\/2013\/07\/08\/10432277.aspx\">\nABC program<\/a>\nthat it turns out I never actually used:\nChange the font.\nI added this in case my nieces were somehow\nunhappy with the font I chose,\nand this was a little escape hatch to let me select a different one.\n<\/p>\n<p>\nThe real work happens in the <code>Choose&shy;Font<\/code> function.\nAll I have to do is call it.\n<\/p>\n<pre>\n<font COLOR=\"blue\">#include &lt;commdlg.h&gt;\nvoid ChangeFont(HWND hwnd)\n{\n  LOGFONT lf;\n  GetObject(g_hfEdit, sizeof(lf), &amp;lf);\n  CHOOSEFONT cf = { sizeof(cf) };\n  cf.hwndOwner = hwnd;\n  cf.lpLogFont = &amp;lf;\n  cf.Flags = CF_INITTOLOGFONTSTRUCT | CF_SCREENFONTS;\n  if (ChooseFont(&amp;cf)) {\n    HFONT hfNew = CreateFontIndirect(&amp;lf);\n    if (hfNew) {\n      DeleteObject(g_hfEdit);\n      g_hfEdit = hfNew;\n      SetWindowFont(g_hwndChild, g_hfEdit, TRUE);\n    }\n  }\n}<\/font>\n<\/pre>\n<p>\nI tell the common font dialog to initialize itself from\nthe <code>LOGFONT<\/code> I passed in,\nwhich I initialize from the font itself.\nIf the user picks a font,\nthe <code>Choose&shy;Font<\/code> function puts the result\nin the same <code>LOGFONT<\/code>,\nand I use that to create the new font\nand swap it into the edit control.\n<\/p>\n<p>\nThe rest is just hooking up this function.\n<\/p>\n<pre>\n<font COLOR=\"blue\">void OnCommand(HWND hwnd, int id, HWND hwndCtl, UINT codeNotify)\n{\n  switch (id) {\n  case 1:\n    ChangeFont(hwnd);\n    break;\n }\n}\n    HANDLE_MSG(hwnd, WM_COMMAND, OnCommand);<\/font>\n<\/pre>\n<p>\nWe hook up a <code>WM_COMMAND<\/code> handler which\nresponds to command number 1 by changing the font.\n<\/p>\n<p>\nNow to hook up the command to a secret hotkey: <kbd>Ctrl<\/kbd>+<kbd>F<\/kbd>.\n<\/p>\n<pre>\n\/\/ scratch.rc\n<font COLOR=\"blue\">1 ACCELERATORS\nBEGIN\n \"F\", 1, VIRTKEY, NOINVERT, CONTROL\nEND<\/font>\n\/\/ scratch.cpp\n...\n    ShowWindow(hwnd, nShowCmd);\n    <font COLOR=\"blue\">HACCEL hacc = LoadAccelerators(hinst, MAKEINTRESOURCE(1));<\/font>\n    while (GetMessage(&amp;msg, NULL, 0, 0)) {\n      <font COLOR=\"blue\">if (!TranslateAccelerator(hwnd, hacc, &amp;msg)) {<\/font>\n        TranslateMessage(&amp;msg);\n        DispatchMessage(&amp;msg);\n      <font COLOR=\"blue\">}<\/font>\n    }\n    <font COLOR=\"blue\">DestroyAcceleratorTable(hacc);<\/font>\n...\n<\/pre>\n<p>\nThere we go, now we can change the font on the fly.\nLike I said, this was a feature I added pre-emptively,\nand it turns out I never needed it.\n<\/p>\n<p>\nNext time, we&#8217;ll look at changes inspired by actual usability issues.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I added a feature to my ABC program that it turns out I never actually used: Change the font. I added this in case my nieces were somehow unhappy with the font I chose, and this was a little escape hatch to let me select a different one. The real work happens in the Choose&shy;Font [&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-3813","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>I added a feature to my ABC program that it turns out I never actually used: Change the font. I added this in case my nieces were somehow unhappy with the font I chose, and this was a little escape hatch to let me select a different one. The real work happens in the Choose&shy;Font [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/3813","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=3813"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/3813\/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=3813"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=3813"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=3813"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}