简体   繁体   English

在为所选文本设置背景颜色后,将字体分配给TRichEdit

[英]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. 我的Delphi表单上有一个TRichEdit控件,我使用perform方法为控件发送一个窗口消息,为文本的某些部分指定背景颜色。 (Text is selected using SelStart and SelLength before calling this code). (在调用此代码之前,使用SelStart和SelLength选择文本)。

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. 我也希望用户可以更改字体(整个字体,它不像背景突出显示那样),所以当他们想要修改编辑框的字体时,我向用户展示TFon​​tDialog ,我将字体分配给TRichEdit控件的字体。

RichEdit.Font.Assign(SelectedFont);

However, using a windows message seems to stop the font from updating. 但是,使用Windows消息似乎会阻止字体更新。 When I comment out the perform method, everything works fine, but when I uncomment the line, the font doesn't update. 当我注释掉perform方法时,一切正常,但是当我取消注释该行时,字体不会更新。

I'm new to windows messages, please explain why this is happening. 我是Windows消息的新手,请解释为什么会发生这种情况。

The Font property you are setting will set the font for the entire RichEdit as a whole, not the current text selection. 您正在设置的Font属性将整个RichEdit的字体设置为整体,而不是当前文本选择。 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 . 如果要设置每个选择字体,则需要使用通过EM_SETCHARFORMAT传递的CHARFORMAT记录的szFaceNameyHeightbCharset字段。 There is no way to assign a TFont object, or even an HFONT handle, on a per-selection basis. 无法在每个选择的基础上分配TFont对象,甚至是HFONT句柄。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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