this slowpoke moves

RichEdit Helper

Unit RichEditHelperCls.pas
unit RichEditHelperCls;

interface

uses
  ComCtrls, Classes, Graphics;

type
  TWordDelimiter = set of Char;

var
  WordDelimiter: TWordDelimiter = [#1..#64, #91..#96, #123..#127];

type
  TRichEditHelper = class(TObject)
  private
    FRichEdit: TRichEdit;
    function GetWords: Integer;
  public
    constructor Create(RichEdit: TRichEdit);
    property CountWords: Integer read GetWords;
    function Find(StartPos: Integer; const SubStr: string; WholeWord: Boolean):
      Integer;
    function HighLightWords(Words: TStrings; FontStyle: TFontStyles; Color:
      TColor): Cardinal;
  end;

implementation

constructor TRichEditHelper.Create(RichEdit: TRichEdit);
begin
  inherited Create;
  FRichEdit := RichEdit;
end;

function TRichEditHelper.GetWords: Integer;
var
  p: PChar;
  CntWords: Cardinal;
begin
  CntWords := 0;
  p := PChar(FRichEdit.Text);
  while p^ <> #0 do
  begin
    if p^ in WordDelimiter then
      Inc(p)
    else
    begin
      while not (p^ in WordDelimiter) do
        Inc(p);
      Inc(CntWords);
    end;
  end;
  result := CntWords;
end;

function TRichEditHelper.Find(StartPos: Integer; const SubStr: string;
  WholeWord:
  Boolean): Integer;
begin
  if WholeWord then
    Result := FRichEdit.FindText(SubStr, StartPos, Length(FRichedit.Text) -
      StartPos, [stWholeWord])
  else
    Result := FRichEdit.FindText(SubStr, StartPos, Length(FRichedit.Text) -
      StartPos, []);
end;

function TRichEditHelper.HighlightWords(Words: TStrings; FontStyle:
  TFontStyles;
  Color: TColor): Cardinal;
var
  i: Integer;
  LastWordPos: Integer;
  HighLightedWords: Cardinal;
begin
  LastWordPos := 0;
  HighLightedWords := 0;
  FRichEdit.Lines.BeginUpdate;
  for i := 0 to Words.Count - 1 do
  begin
    while LastWordPos <> 1 do
    begin
      LastWordPos := Find(LastWordPos, Words[i], True);
      FRichEdit.SelStart := LastWordPos;
      FRichEdit.SelLength := length(Words[i]);
      FRichEdit.SelAttributes.Color := Color;
      FRichEdit.SelAttributes.Style := FontStyle;
      LastWordPos := LastWordPos + 2;
      Inc(HighLightedWords);
    end;
    LastWordPos := 0;
  end;
  FRichEdit.Lines.EndUpdate;
  result := HighLightedWords;
end;

end.


Unit1:
uses ComCtrls, RichEditHelperCls

//

// Strings zählen
procedure TForm1.Button1Click(Sender: TObject);
var
  reh: TRichEditHelper;
begin
  reh := TRichEditHelper.Create(RichEdit1);
  try
    ShowMessage(IntToStr(reh.CountWords));
  finally
    FreeAndNil(reh);
  end;
end;

// Strings suchen
procedure TForm1.Button2Click(Sender: TObject);
const
  MYWORD = 'and';
var
  reh: TRichEditHelper;
begin
  reh := TRichEditHelper.Create(RichEdit1);
  try
    RichEdit1.SelStart := reh.Find(84, MYWORD, True);
    RichEdit1.SelLength := length(MYWORD);
  finally
    FreeAndNil(reh);
  end;
end;

// Strings Highlighten
procedure TForm1.Button3Click(Sender: TObject);
var
  reh: TRichEditHelper;
  sl: TStringList;
begin
  reh := TRichEditHelper.Create(RichEdit1);
  sl := TStringList.Create;
  try
    sl.Add('and');
    ShowMessage(IntToStr(reh.HighlightWords(sl, [fsBold, fsUnderline],
      clRed)) + ' Wörter markiert');
    RichEdit1.SelStart := 0;
  finally
    FreeAndNil(sl);
    FreeAndNil(reh);
  end;
end;

Keine Kommentare:

Kommentar veröffentlichen

Beliebte Posts

Translate