Search code examples
winapifontsfallback

How to force a Win32 RichEdit fallback to SimSun with East Asian text?


I set the font linking to SimSun for my fonts at Window registry.
When I apply that font to my richedit control, it have below behavior:

  • First, it display SimSun - ok
  • When I add text that is not CKJ unicode, then add more text in CKJ, the font window selected is Microsoft Ya Hei instead of SimSun

Here is screenshot:

enter image description here

The fonts are:

  • "阿哥": SimSun
  • "◎": Segoe UI Symbol
  • "[āgē] đại ca" Arial
  • "对兄长的称呼。" Microsft Ya Hei

How I can force window to choose SimSun instead of Microsft Ya Hei in this case?

EDIT
Thank @Cody Gray for perfect answer. Here is worked code:

CHARFORMAT2 cf2;

memset(&cf2, 0, sizeof(CHARFORMAT2));
cf2.cbSize = sizeof(CHARFORMAT2);
cf2.dwMask = CFM_FACE | CFM_SIZE | CFM_CHARSET | CFM_LCID;
cf2.lcid = 0x0804;
cf2.yHeight = 280;
cf2.bCharSet = CHINESEBIG5_CHARSET;
wcscpy(cf2.szFaceName, L"SimSun");
SendMessage(rtbhWnd, EM_SETCHARFORMAT, SCF_SELECTION | SCF_ASSOCIATEFONT, (LPARAM)&cf2);

cf2.cbSize = sizeof(CHARFORMAT2);
cf2.dwMask = CFM_FACE | CFM_SIZE | CFM_CHARSET | CFM_LCID;
cf2.lcid = 0x0409;
cf2.yHeight = 220;
cf2.bCharSet = ANSI_CHARSET;
wcscpy(cf2.szFaceName, L"Segoe UI");
SendMessage(rtbhWnd, EM_SETCHARFORMAT, SCF_SELECTION | SCF_ASSOCIATEFONT2, (LPARAM)&cf2);

Solution

  • You can set the default font for character representations by sending the RichEdit control an EM_SETCHARFORMAT message with the SCF_ASSOCIATEFONT flag.

    This involves filling in a CHARFORMAT2 structure with the characteristics of the desired font as well as the LCID corresponding to the locale of the desired character representation, as outlined in the documentation.

    See also: How to Use Font Binding in Rich Edit Controls

    It looks like to me that Windows 8 introduced the "Microsoft YaHei" font as the default UI font for the Simplified Chinese script. I think (but I'm not certain) that SimSun used to be the default font for this script, so that would explain it if you were seeing this behavior on some systems and not others.