views:

763

answers:

1

Hi! I found this code over the net. This puts background color to the selected texts on Trichedit:

uses
 RichEdit;

procedure RE_SetSelBgColor(RichEdit: TRichEdit; AColor: TColor);
var
  Format: CHARFORMAT2;
begin
  FillChar(Format, SizeOf(Format), 0);
  with Format do
  begin
    cbSize := SizeOf(Format);
    dwMask := CFM_BACKCOLOR;
    crBackColor := AColor;
    Richedit.Perform(EM_SETCHARFORMAT, SCF_SELECTION, Longint(@Format));
  end;
end;

// Example: Set clYellow background color for the selected text.
procedure TForm1.Button1Click(Sender: TObject);
begin
  RE_SetSelBgColor(RichEdit1, clYellow);
end;

However, what I need is to exclude space characters. Can someone help me? Any idea would be helpful? My idea would be to select all space characters and then format it but then I don't know how to select them. By the way, I am using delphi 2009.

+2  A: 

@junmats, with this code you can select any word in a richedit control.

tested in Delphi 2010 and windows 7

uses
  RichEdit;

procedure SetWordBackGroundColor(RichEdit : TRichEdit; aWord : String;AColor: TColor);
var
  Format: CHARFORMAT2;
  Index : Integer;
  Len   : Integer;
begin
          FillChar(Format, SizeOf(Format), 0);
          Format.cbSize := SizeOf(Format);
          Format.dwMask := CFM_BACKCOLOR;
          Format.crBackColor := AColor;

          Index := 0;
          Len := Length(RichEdit.Lines.Text) ;
          Index := RichEdit.FindText(aWord, Index, Len, []);

          while Index <> -1 do
          begin
                RichEdit.SelStart  := Index;
                RichEdit.SelLength := Length(aWord) ;
                RichEdit.Perform(EM_SETCHARFORMAT, SCF_SELECTION, Longint(@Format));
                Index := RichEdit.FindText(aWord,Index + Length(aWord),Len, []) ;
          end;
end;


procedure TForm1.Button1Click(Sender: TObject);
begin
          SetWordBackGroundColor(RichEdit1,' ',clYellow);// will mark all spaces
end; 

if you wanna select all words except the spaces, you can do something like this

Procedure GetListofWords(Text : String; var ListofWords : TStringList);
var
  DummyStr  : String;
  FoundWord : String;
begin
  DummyStr := Text;
  FoundWord := '';
  if (Length(Text) = 0) then exit;

  while (Pos(' ', DummyStr) > 0) do
  begin
    FoundWord := Copy(DummyStr, 1, Pos(' ', DummyStr) - 1);
    ListofWords.Add(FoundWord);
    DummyStr := Copy(DummyStr, Pos(' ', DummyStr) + 1,  Length(DummyStr) - Length(FoundWord) + 1);
  end;

  if (Length(DummyStr) > 0) then
    ListofWords.Add(DummyStr);

end;



procedure TForm1.Button1Click(Sender: TObject);
var
ListofWords : TStringList;
i           : integer;
begin
          ListofWords:=TStringList.Create;
          try
           GetListofWords(RichEdit1.Lines.Text,ListofWords);
           if ListofWords.Count>0 then
            for i:=0  to ListofWords.Count - 1 do
             SetWordBackGroundColor(RichEdit1,ListofWords[i],clYellow);
          finally
          ListofWords.Clear;
          ListofWords.Free;
          end;
end;
RRUZ
looks like this is a word by word searching.. Using this would make my program very slow as I have a very large chunk of string.. But I guess this is the best solution for my answer.. Thanks a lot :-)
junmats