0

I managed to get custom text color in REC, but I can't make it to use the desired font (Consolas).

(EDIT) Creating REC:

LoadLibrary(TEXT("Msftedit.dll"));
hEdit = CreateWindowEx(WS_EX_CLIENTEDGE, MSFTEDIT_CLASS, L"",
                   WS_CHILD | WS_VISIBLE | ES_MULTILINE,
                   0, 0, 300, 300,
                   hwnd, NULL, GetModuleHandle(NULL), NULL);

-

CHARFORMAT cf = {};

cf.cbSize = sizeof(CHARFORMAT);

cf.dwMask = CFM_COLOR | CFM_FACE;
cf.crTextColor = RGB(255, 0, 255);

cf.bPitchAndFamily = DEFAULT_PITCH | FF_MODERN;
memcpy(cf.szFaceName, L"Consolas", sizeof(L"Consolas"));

SendMessage(hEdit, EM_SETCHARFORMAT, NULL, (LPARAM)&cf);

I'm using

#ifndef UNICODE
  #define UNICODE
#endif

Few answers that i found but didn't help me much... Win32 : set default font and text color for rich edit, How to set a font in rich edit 4?

Note: the REC has no text in it

This is what i get... 5 'i' and 5 '0' should have same spacing with Consolas, Right?

  • Just to confirm, you do have that font, correct? (Vista+ should have it) – Anders Oct 26 '19 at 12:21
  • Do you already have text in the control that you want to change or is it empty? – Anders Oct 26 '19 at 12:24
  • Don't understand why people prefer `memcpy` instead of `strcpy`. You have normal string. `strcpy( cf.szFaceName, TEXT("Consolas") )`. Also it is better to reset `cf` structure explicitly with `memset( ct, 0, sizeof cf )`. – i486 Oct 26 '19 at 12:24
  • @Anders, what do you mean?.. I have win10 pro. – Ivan Ambic Oct 26 '19 at 12:24
  • Sample code, note how it doesn't forget to set yHeight: https://referencesource.microsoft.com/#System.Windows.Forms/winforms/Managed/System/WinForms/RichTextBox.cs,2916 – Hans Passant Oct 26 '19 at 12:26
  • @Anders, before SendMessage, I check with debugger that struct, and there it says L"Consolas". – Ivan Ambic Oct 26 '19 at 12:27
  • 1
    I'm not asking about the font name, I'm asking if the RichEdit already has text in it that you want to apply the font to or if the control has no text in it. – Anders Oct 26 '19 at 12:29
  • @Anders, no, It's empty – Ivan Ambic Oct 26 '19 at 12:31
  • Your code works fine for me. – Anders Oct 26 '19 at 14:43
  • @Anders I'm using Msftedit.dll with MSFTEDIT_CLASS and it doesn't work.. But when i switch to Riched20.dll with RICHEDIT_CLASS, it works... I'm confused – Ivan Ambic Oct 26 '19 at 15:32
  • @i486 `strcpy()` doesn't work with Unicode strings, use `wcscpy()` instead. Or, if you insist on using `TEXT()`, use `_tcscpy()`. – Remy Lebeau Oct 26 '19 at 18:17
  • @RemyLebeau I agree - I use `lstrcpy` for Unicode. – i486 Oct 27 '19 at 08:58
  • @IvanAmbic `Msftedit.dll` is for RichEdit 4.1, `Riched##.dll` is for earlier versions. The 1st link in your question has an answer containing a quote from MSDN that specifically states to use `CHARFORMAT2` for RichEdit 4.1, but you are using `CHARFORMAT` instead. Have you tried using `CHARFORMAT2` yet? – Remy Lebeau Oct 27 '19 at 17:11
  • @RemyLebeau Yes. New thing i discovered, when I set initial text WS_EX_CLIENTEDGE, MSFTEDIT_CLASS, L"iiiiiii", WS_CHI..., the text shows up in Consolas (as I set it), but when I start typing something it shows it in differed font (but same color)... wtf?! – Ivan Ambic Oct 27 '19 at 17:32
  • @IvanAmbic Setting a new font does not apply to existing text unless you select the text and specify the `SCF_SELECTION` flag in `EM_SETCHARFORMAT`. Clearly the insertion point has a different font set than what you are expecting. Set the font before adding any text at all. – Remy Lebeau Oct 27 '19 at 19:04

0 Answers0