A program for my nieces: The ABCs, part 2, choosing a font


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­Font function. All I have to do is call it.

#include <commdlg.h>

void ChangeFont(HWND hwnd)
{
  LOGFONT lf;
  GetObject(g_hfEdit, sizeof(lf), &lf);
  CHOOSEFONT cf = { sizeof(cf) };
  cf.hwndOwner = hwnd;
  cf.lpLogFont = &lf;
  cf.Flags = CF_INITTOLOGFONTSTRUCT | CF_SCREENFONTS;
  if (ChooseFont(&cf)) {
    HFONT hfNew = CreateFontIndirect(&lf);
    if (hfNew) {
      DeleteObject(g_hfEdit);
      g_hfEdit = hfNew;
      SetWindowFont(g_hwndChild, g_hfEdit, TRUE);
    }
  }
}

I tell the common font dialog to initialize itself from the LOGFONT I passed in, which I initialize from the font itself. If the user picks a font, the Choose­Font function puts the result in the same LOGFONT, and I use that to create the new font and swap it into the edit control.

The rest is just hooking up this function.

void OnCommand(HWND hwnd, int id, HWND hwndCtl, UINT codeNotify)
{
  switch (id) {
  case 1:
    ChangeFont(hwnd);
    break;
 }
}

    HANDLE_MSG(hwnd, WM_COMMAND, OnCommand);

We hook up a WM_COMMAND handler which responds to command number 1 by changing the font.

Now to hook up the command to a secret hotkey: Ctrl+F.

// scratch.rc
1 ACCELERATORS
BEGIN
 "F", 1, VIRTKEY, NOINVERT, CONTROL
END

// scratch.cpp

...
    ShowWindow(hwnd, nShowCmd);

    HACCEL hacc = LoadAccelerators(hinst, MAKEINTRESOURCE(1));
    while (GetMessage(&msg, NULL, 0, 0)) {
      if (!TranslateAccelerator(hwnd, hacc, &msg)) {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
      }
    }
    DestroyAcceleratorTable(hacc);
...

There we go, now we can change the font on the fly. Like I said, this was a feature I added pre-emptively, and it turns out I never needed it.

Next time, we’ll look at changes inspired by actual usability issues.

Comments (11)
  1. Pseudo-Anonymous says:

    What font did you use for your ABC program?

  2. Joshua Ganes says:

    From The ABCs, part 1: blogs.msdn.com/…/10432277.aspx

    g_hfEdit = CreateFont(-72, 0, 0, 0, FW_NORMAL,

                           FALSE, FALSE, FALSE, DEFAULT_CHARSET,

                           OUT_DEFAULT_PRECIS,

                           CLIP_DEFAULT_PRECIS,

                           DEFAULT_QUALITY,

                           DEFAULT_PITCH,

                           TEXT("Miriam"));

  3. Nicholas says:

    > Miriam

    fontzone.net/…/miriam.png

    Oh, sad… just look at all those boring straight lines.  At least now they can change it to Comic Sans MS; it's at least 20% more curvy and fun.

  4. Rick C says:

    Just out of curiosity, why did you specify NOINVERT in the resource file?  MSDN says that "[t]his attribute is obsolete and retained only for backward compatibility with resource files designed for 16-bit Windows."

  5. Alex B says:

    @Nicholas: The point of using Miriam wasn't to use the sexiest font ever, but to use a font with letter shapes Raymond's nieces recognise

  6. Silly says:

    Perhaps the choice of font is irrelevant to the final program state based on user feedback? This program is just getting off the ground, and we are dealing with a weapon here. ('Weapon' in the slang-sense of 'really skilled')

  7. @Nicholas

    The font is (more or less) Arial + Hebrew characters.

  8. Nicholas says:

    @Alex:

    Just sharing a picture for the unfamiliar font and making a funny.  And Comic Sans is anything but sexy (showing, I suppose, that not all curves are sexy! :).

  9. Mc says:

    I thought every potential feature started off with -100 points.    This one must of somehow slipped through the net.

  10. Mordachai says:

    This is just a little program for his Nieces, not a bit of Windows code that needs restraint in terms of throwing additional, irrelevant, must-be-supported as they're now canon functions into it…

  11. Mc says:

    @Steve   maybe I should of added a smilely emoticon to the end of my comment.

Comments are closed.