1

I have a TRichEdit control on my Delphi form, and I'm assigning a background colour to certain parts of the text using the perform method to send a windows message to the control. (Text is selected using SelStart and SelLength before calling this code).

FillChar(Format, SizeOf(Format), 0);
with Format do
begin
  cbSize := SizeOf(Format);
  dwMask := CFM_BACKCOLOR;
  crBackColor := AColor;
  fRichEdit.Perform(EM_SETCHARFORMAT, SCF_SELECTION, Longint(@Format));
end;

I'm also wanting the font to be changeable by the user (the ENTIRE font, it's not selective like the background highlighting), so I'm presenting a TFontDialog to the user when they'd like to modify the font for the edit box, and I'm assigning the font to the font of the TRichEdit control.

RichEdit.Font.Assign(SelectedFont);

However, using a windows message seems to stop the font from updating. When I comment out the perform method, everything works fine, but when I uncomment the line, the font doesn't update.

I'm new to windows messages, please explain why this is happening.

Dom
  • 213
  • 3
  • 10
  • Why are you not doing this the easy way? https://stackoverflow.com/a/20186029/62576 – Ken White May 16 '19 at 13:00
  • 1
    @KenWhite I'm wanting to set the text background colour rather than the text foreground colour. With SelAttributes I can only see a way of assigning the text foreground colour. Please let me know if there's any easier way to set the background colour for the text. – Dom May 16 '19 at 13:16
  • 1
    @Dom there isn't one. You are doing the right thing by using `EM_SETCHARFORMAT` directly – Remy Lebeau May 16 '19 at 15:15

1 Answers1

1

The Font property you are setting will set the font for the entire RichEdit as a whole, not the current text selection. If you want to set a per-selection font, you need to use the szFaceName, yHeight, and bCharset fields of the CHARFORMAT record that you pass via EM_SETCHARFORMAT. There is no way to assign a TFont object, or even an HFONT handle, on a per-selection basis.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • Apologies Remy, I should have clarified further. The font should be for the entire RichEdit. As stated in my question, when I comment out the perform code line (to add the background colour to selected text) everything works as I'd like (apart from the background colour being set of course). The problem seems to occur because of me applying the background colour. Something seems to prevent the font change from updating on my screen. – Dom May 16 '19 at 16:04