I have a TRichEdit control containing source code. I want to set the background color of a single line.

I know how to set the text color, but isn't there a way to set the background color as well? I'm not talking about the entire background colour for the whole control, only how to change one single line.

Do I really have to write a custom control to do this?

有帮助吗?

解决方案

There is no TRichEdit property for setting the background color of individual characters/lines. However, you can use the Win32 API SendMessage() function, or the TRichEdit's own Perform() method, to send it an EM_SETCHARFORMAT message, specifying a CHARFORMAT2 structure whose crBackColor field is set to the desired color. You can apply formatting to existing characters by highlighting them first, or you can apply formatting to the current caret position if there is no selection.

其他提示

The MSDN on RTF specifies \cbN, where N is the color index.

Other searches suggest this is not supported by a lot of applications (OS X's native RTF viewer, Microsoft Word) so maybe you should look for a custom solution.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top