2

I'm dynamically (in runtime) creating a CRichEditCtrl control and when I set the text to it, the font is different from the other controls (e.g. CStatic - see the pictures) in the dialog (I didn't use any formatting for the rich edit control). The difference is best seen with the characters from alphabets like Chinese or Japanese. I tried using SetDefaultCharFormat method but it was not having the effect I wanted. Then I found out, that I first need to set the text to the rich edit control, and only then use the formatting function.

    // 1. dynamically created rich edit ctrl
    CRichEditCtrl* dynamic_rich = new CRichEditCtrl();
    dynamic_rich->Create(WS_CHILD | WS_TABSTOP | ES_LEFT | ES_MULTILINE | WS_VSCROLL |
        WS_HSCROLL | ES_WANTRETURN | WS_VISIBLE | WS_BORDER | ES_AUTOHSCROLL | ES_AUTOVSCROLL,
        CRect(10, 0, 184, 23), this, 1234);
    dynamic_rich->ModifyStyleEx(WS_EX_CLIENTEDGE, 0, SWP_DRAWFRAME);
    dynamic_rich->SetWindowTextW(L"アラビア語"); // calling it here results in the 3rd picture
    // which is the closest to the desired result, though it's still not the same as in CStatic

    CHARFORMAT cf = { 0 };
    dynamic_rich->GetDefaultCharFormat(cf);
    _tcscpy_s(cf.szFaceName, L"MS Shell Dlg 2");
    cf.dwMask = CFM_FACE;
    dynamic_rich->SetDefaultCharFormat(cf);

    // 2. statically created rich edit ctrl - defined in .rc file
    CRichEditCtrl* static_rich = (CRichEditCtrl*)this->GetDlgItem(IDC_RICHEDIT);
    static_rich->SetWindowTextW(L"アラビア語");
    
    // 3. statically created cstatic - for the font comparison
    CStatic* label = (CStatic*)this->GetDlgItem(IDC_STATIC);
    label->SetWindowTextW(L"アラビア語");

When I leave the default font (without calling SetDefaultCharFormat) - seems blurry

enter image description here

When I call SetDefaultCharFormat, then set the text - the same result

enter image description here

When I set the text, then call SetDefaultCharFormat - not quite the same, but close

enter image description here

The font in the rich edit control still doesn't look the same as in CStatic (seems a bit different and also bigger), but that I can live with. Calling SetDefaultCharFormat every time I change a text is a problem though because I need to be constantly changing the text in the runtime and it could potentially cause an overhead. Does anybody have a better solution? Thanks.

mrdecompilator
  • 155
  • 1
  • 8
  • You are explicitly setting the rich edit control's font to `MS Shell Dlg 2`. Does that actually match the font of the other controls? – IInspectable Jul 20 '20 at 15:14
  • Yes. I'm not messing with other control's fonts, I used CWnd::GetFont to verify that, they all have MS Shell Dlg 2 by default. Maybe the problem is the font size - if the fonts were the same size, they should be displayed similarly right? - but the height cannot be specified using CHARFORMAT structure and if I use SetFont method on a rich edit control, it simply ignores it (works with the other controls). – mrdecompilator Jul 20 '20 at 16:03
  • You could try to use this struct [`CHARFORMAT2`](https://learn.microsoft.com/en-us/windows/win32/api/richedit/ns-richedit-charformat2w_1) with `dwMask = CFM_WEIGHT`. – Drake Wu Jul 21 '20 at 07:03
  • When creating dialog controls dialog manager sends `WM_SETFONT` to each of them. Try doing the same for dynamic rich edit before setting any text. – Daniel Sęk Jul 21 '20 at 08:38
  • Thanks for your responses. @DanielSęk In my real app, I create dialog dynamically and I send `WM_SETFONT` to all the controls - this works for every control except `CRichEditCtrl`. @Drake Wu this does absolutely nothing, it's weird that it does not have `lfHeight` field, like the `LOGFONT` structure. But I just realized, this problem is only in the static controls, when I create them dynamically, the font height is the same for all of them. The only question now is, if there's another way than to call `SetDefaultCharFormat` after every `SetWindowText` call. – mrdecompilator Jul 21 '20 at 09:28
  • @mrdecompilator It works for me (TM) [https://imgur.com/a/bbTmncw](https://imgur.com/a/bbTmncw) `HWND ctrl = ::GetDlgItem( m_hWnd, 99 ); HFONT font = (HFONT)::SendMessage( ctrl, WM_GETFONT, 0, 0 ); dynamic_rich->SendMessage( WM_SETFONT, (WPARAM)font, FALSE ); dynamic_rich->SetWindowTextW(L"アラビア語");` There is small diference relative to static control because rich edit uses more advanced text processing. – Daniel Sęk Jul 21 '20 at 10:52
  • Without `WM_SETFONT` second rich edit control has slightly larger font [https://imgur.com/a/eZOrUxP](https://imgur.com/a/eZOrUxP) – Daniel Sęk Jul 21 '20 at 11:00
  • @DanielSęk Yes, but both of your rich edit controls have a different font than your `CStatic` control above. I was not able to match the fonts using `WM_SETFONT` - the font changes a little bit but it's certainly not `MS Shell Dlg 2` like in the `CStatic`. – mrdecompilator Jul 21 '20 at 11:14

0 Answers0