delphi TRichEdit 设置背景颜色不包括空格

发布于 2024-08-17 22:26:46 字数 694 浏览 9 评论 0原文

我在网上找到了这段代码。这会将背景颜色添加到 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;

但是,我需要的是排除空格字符。有人可以帮助我吗?有什么想法会有帮助吗? 我的想法是选择所有空格字符,然后对其进行格式化,但我不知道如何选择它们。 顺便说一下,我使用的是delphi 2009。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

近箐 2024-08-24 22:26:46

@junmats,使用此代码,您可以选择 richedit 控件中的任何单词。

在 Delphi 2010 和 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; 

如果你想选择除空格之外的所有单词,你可以这样做

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;

@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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文