2006. augusztus 10., csütörtök

Set the background colour of a word in a TRichEdit


Problem/Question/Abstract:

I have a RichEdit control where my code outputs text into, formatting certain words or phrases with different font colors. This works fine, but I would also like to set the background of certain words to be different colors. MSWord allows this with the Highlight option, and I can copy and paste highlighted text from MSWord into my RichEdit control, so I have to assume that this is possible to do in code as well. Can anyone provide me with sample code or guidance on doing this in code?

Answer:

The version 1 richedit control introduced with Win95 did not have this ability and this version is what the TRichedit class was wrapped around. So it does not give you access to all the new abilities in the version 3 control you find on WinMe, 2K, and XP. You could delve into the murky depth of the API and send appropriate messages to the control:

uses
  richedit;

procedure TForm1.Button1Click(Sender: TObject);
var
  cf: TCharFormat2;
begin
  fillchar(cf, sizeof(cf), 0);
  cf.cbSize := sizeof(cf);
  cf.dwMask := CFM_BACKCOLOR;
  cf.crBackColor := ColorToRGB(clYellow);
  richedit1.Perform(EM_SETCHARFORMAT, SCF_SELECTION, lparam(@cf));
end;

Nincsenek megjegyzések:

Megjegyzés küldése