a814123
814123
2018-01-04 15:05
采纳率: 96.3%
浏览 1.2k
已采纳

delphi richedit里比较文字?

比如:richedit1里有“DELPHI”。
richedit2里有“DELPHI是一门开发语言”。
那么单击按钮richedit2里的"DELPHI"就以红色加组字体显示,其他的不变。

麻烦老师给个思路或者实例参考!

///////
就是要这样的效果呢?
图片说明

RichEdit2里的文字,R和我显示红色。

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

5条回答 默认 最新

  • hskill
    堕落恶魔_Delphi 2018-01-05 08:15
    已采纳

    新代码, 通过空格分割要高亮的内容, 你把你图片里的顿号改成空格就行了

     procedure TForm1.Button1Click(Sender: TObject);
    
      procedure _HighLightText(AStr: string);
      var
        nPos, nStrLength, nAllLength: Integer;
      begin
        nPos := 0;
        nAllLength := RichEdit2.GetTextLen;
        nStrLength := Length(AStr);
        nPos := RichEdit2.FindText(AStr, nPos, nAllLength, [stMatchCase]); //如果大小写不敏感就用[]
        while nPos > -1 do //如果只想高亮第一个就不用循环
        begin
          RichEdit2.SelStart := nPos;
          RichEdit2.SelLength := nStrLength;
          RichEdit2.SelAttributes.Color := clRed;
          nPos := nPos + nStrLength;
          nPos := RichEdit2.FindText(AStr, nPos, nAllLength, [stMatchCase]);
        end;
      end;
    
    var
      nStr: string;
      i: Integer;
    begin
      RichEdit2.SelectAll;
      RichEdit2.SelAttributes := RichEdit2.DefAttributes;
    
      with TStringList.Create do
      try
        StrictDelimiter := True;
        Delimiter := ' '; //这里规定要高亮的内容用空格分隔
        DelimitedText := RichEdit1.Text;
        for i := 0 to Count - 1 do
        begin
          nStr := Strings[i].Trim;
          if nStr <> '' then
            _HighLightText(nStr);
        end;
      finally
        Free;
      end;
    end;
    
    
    点赞 评论
  • caozhy

    参考:http://blog.csdn.net/gloria__zhang/article/details/7213801
    keybuffer数组存入delphi等你要找的字即可。

    点赞 评论
  • caozhy

    如果huida回答满意,麻烦采纳下好么?之前回答了很多问题一直没有采纳,不知道是忘记了还是我的回答还有什么不足的地方。谢谢。

    点赞 评论
  • hskill
    堕落恶魔_Delphi 2018-01-05 03:46

    不能修改真麻烦, 重新发一遍代码格式的

     procedure TForm1.Button1Click(Sender: TObject);
    var
      nStr: string;
      nPos, nStrLength: Integer;
    begin
      nStr := RichEdit1.SelText;
    
      RichEdit2.SelectAll;
      RichEdit2.SelAttributes := RichEdit2.DefAttributes;
      if nStr = '' then
        Exit;
    
      nPos := 0;
      nStrLength := Length(nStr);
      nPos := RichEdit2.FindText(nStr, nPos, RichEdit2.GetTextLen, [stMatchCase]); //如果大小写不敏感就用[]
      while nPos > -1 do //如果只想高亮第一个就不用循环
      begin
        RichEdit2.SelStart := nPos;
        RichEdit2.SelLength := nStrLength;
        RichEdit2.SelAttributes.Color := clRed;
        nPos := nPos + nStrLength;
        nPos := RichEdit2.FindText(nStr, nPos, RichEdit2.GetTextLen, [stMatchCase]);
      end;
    end;
    
    
    点赞 评论
  • hskill
    堕落恶魔_Delphi 2018-01-06 08:57
     procedure TForm1.Button1Click(Sender: TObject);
    
      function _HighLightText(AStr: string): Integer;
      var
        nPos, nStrLength, nAllLength: Integer;
      begin
        Result := 0;
        nPos := 0;
        nAllLength := RichEdit2.GetTextLen;
        nStrLength := Length(AStr);
        nPos := RichEdit2.FindText(AStr, nPos, nAllLength, [stMatchCase]); //如果大小写不敏感就用[]
        while nPos > -1 do //如果只想高亮第一个就不用循环
        begin
          RichEdit2.SelStart := nPos;
          RichEdit2.SelLength := nStrLength;
          RichEdit2.SelAttributes.Color := clRed;
          Inc(Result);
          nPos := nPos + nStrLength;
          nPos := RichEdit2.FindText(AStr, nPos, nAllLength, [stMatchCase]);
        end;
      end;
    
    var
      nStr: string;
      i, nCount: Integer;
    begin
      RichEdit2.SelectAll;
      RichEdit2.SelAttributes := RichEdit2.DefAttributes;
    
      nCount := 0;
      with TStringList.Create do
      try
        StrictDelimiter := True;
        Delimiter := ' '; //这里规定要高亮的内容用空格分隔
        DelimitedText := RichEdit1.Text;
        for i := 0 to Count - 1 do
        begin
          nStr := Strings[i].Trim;
          if nStr <> '' then
            nCount := nCount + _HighLightText(nStr); //这里做数量累加, 如果想获取单独的, 就是 _HighLightText(nStr)的返回值
        end;
        Caption := '共' + nCount.ToString + '个';
      finally
        Free;
      end;
    end;
    
    
    点赞 评论

相关推荐