简体   繁体   中英

Assigning a font to a TRichEdit after setting background colour for selected text

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM