Einzelnen Beitrag anzeigen

A-M-X

Registriert seit: 29. Sep 2005
Ort: Karlsruhe
152 Beiträge
 
#1

Bestimmte Wörter anders zeigen

  Alt 17. Jan 2006, 18:45
Huhu!

Habe vor einiger Zeit von einem Kumpel einen Code gekriegt (habe ihn gefragt wie das geht ^^):

"Wie drucke ich bestimmte Wörter in einem RichEdit anderst?"

Er hat mir einen sehr guten Code gegeben:

Delphi-Quellcode:

procedure TForm1.RichEdit1Change(Sender: TObject);
const
  AMarkedWords : array [1 .. 106] of String = ('unit', 'interface', 'uses', 'type', 'class', 'procedure', 'private', 'public', 'end;', 'end', 'end.', 'var', 'implementation', 'array', 'of', 'string', 'to', 'goto', 'with', 'do', 'for', 'while', 'mod', 'if', 'then', 'begin', 'const', 'else', 'repeat', 'xor', 'label', 'try', 'finally', 'except', 'overload', 'stdcall', 'inherited', 'div', 'interface', 'not', 'stored', 'default', 'read', 'write', 'absolute', 'assembler', 'and', 'as', 'asm', 'at', 'case', 'cdecl', 'downto', 'exports', 'external', 'far', 'file', 'finally', 'forward', 'goto', 'in', 'index', 'inline', 'library', 'name', 'mear', 'nodefault', 'nil', 'not', 'of', 'or', 'on', 'program', 'raise', 'record', 'resident', 'set', 'shl', 'shr', 'finalization', 'packed', 'protected', 'resourcestring', 'threadvar', 'initialization', 'abstract', 'class', 'constructor', 'destructor', 'dynamic', 'inherited', 'is', 'message', 'object', 'property', 'override', 'publishes', 'read', 'stored', 'virtual', 'dispinterace', 'packed', 'protected', 'resourcestring', 'threadvar', 'until');
var
  BufferPosition : LongInt;
  BufferText : String;
  Index : LongInt;
  MySelLength : LongInt;
  MySelStart : LongInt;
begin
  MySelStart := RichEdit1.SelStart;
  MySelLength := RichEdit1.SelLength;

  RichEdit1.SelStart := 0;
  RichEdit1.SelLength := Length(RichEdit1.Text);

  RichEdit1.SelAttributes.Color := clBlack;
  RichEdit1.SelAttributes.Style := [];

  RichEdit1.SelStart := 0;
  RichEdit1.SelLength := 0;
 
  for Index := Low(AMarkedWords) to High(AMarkedWords) do
  begin
    BufferPosition := 0;
    BufferText := RichEdit1.Text;
 
    while (Pos(Trim(LowerCase(AMarkedWords[Index])), LowerCase(BufferText)) > 0) do
    begin
      RichEdit1.SelStart := BufferPosition + Pred(Pos(Trim(LowerCase(AMarkedWords[Index])), LowerCase(BufferText)));
      RichEdit1.SelLength := Length(Trim(AMarkedWords[Index]));
 
      RichEdit1.SelAttributes.Color := clNavy;
      RichEdit1.SelAttributes.Style := [FsBold];
 
      BufferPosition := BufferPosition + Pred(Pos(Trim(LowerCase(AMarkedWords[Index])), LowerCase(BufferText))) + Length(Trim(AMarkedWords[Index]));
      BufferText := Copy(BufferText, Pos(Trim(LowerCase(AMarkedWords[Index])), LowerCase(RichEdit1.Text)) + Length(Trim(AMarkedWords[Index])), Succ(Length(BufferText) - (Pos(Trim(LowerCase(AMarkedWords[Index])), LowerCase(RichEdit1.Text)) + Length(Trim(AMarkedWords[Index])))));
    end;
  end;

  RichEdit1.SelStart := MySelStart;
  RichEdit1.SelLength := MySelLength;

  RichEdit1.SelAttributes.Color := clBlack;
  RichEdit1.SelAttributes.Style := [];

end;
NUR:

wenn ich z.B. NUR "in" fett gedruckt haben will, druckt er bei "string" folgendermaßen:

String

Also er druckt das "in" in "strINg" auch fett. Wie behebe ich das? ^^

Edit: und wie behebe ich dieses Flackern beim schreiben. wenn ich "hallo" eingebe flacktert es und dauert ewig lang bis es da steht -> habe einen code eingefügt und will am ende noch was schreiben, wenn es leer ist und ich schreibe, flackerts und hängts ned, wenn ich aber wiederum einen langen code einfüge und dann am ende schreibe flackerts und hängts total

[edit=sakura] L E E R Z E I C H E N eingefüt. Mfg, 桜[/edit]
  Mit Zitat antworten Zitat