Thema: Delphi Nur ganze Wörter finden

Einzelnen Beitrag anzeigen

Benutzerbild von Luckie
Luckie

Registriert seit: 29. Mai 2002
37.621 Beiträge
 
Delphi 2006 Professional
 
#13

Re: Nur ganze Wörter finden

  Alt 9. Aug 2004, 19:42
So, hier mal die ganze Klasse:
Delphi-Quellcode:
{************************************************************}
{                                                            }
{             RichEditHelperCls Unit - Version 1.1           }
{                                                            }
{               Copyright (c) 2004 Michael Puff              }
{                                                            }
{  When I die I want 'Hello, world' carved on my headstone.  }
{                                                            }
{************************************************************}

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

////////////////////////////////////////////////////////////////////////////////
//
// TRichEditHelper.Create
//
// Author : Michael Puff
// Date : 2004-08-09
// Purpose: Constructor
//

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

////////////////////////////////////////////////////////////////////////////////
//
// TRichEditHelper.CountWords
//
// Author : Michael Puff
// Date : 2004-08-08
// Purpose: Ermittelt die Anzahl der Wörter im RichEdit
//

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;

////////////////////////////////////////////////////////////////////////////////
//
// TRichEditHelper.Find
//
// Author : Michael Puff
// Date : 2004-08-08
// Purpose: Findet eine Zeichenfolge im Text des RichEdit
//

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;

////////////////////////////////////////////////////////////////////////////////
//
// TRichEditHelper.HighlightWords
//
// Author : Michael Puff
// Date : 2004-08-08
// Purpose: Hebt Wörter aus einer List in einem RichEdit hervor
//

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.
Eventuell fallen euch ja noch ein paar nützliche sachen ein, die mit dem RichEdit zutun haben und da rein können. Aber mehr brauche ich im Moment selber nicht.
Michael
Ein Teil meines Codes würde euch verunsichern.
  Mit Zitat antworten Zitat