08. august 2003 - 16:11Der er
38 kommentarer og 1 løsning
Farve tekst i en RichEdit1
Som overskriften siger ... Jeg vil kunne farve forskillige ord'er i en RichEdit1 ... hvordan gør jeg det ... jeg skal bruge noget kode. Componenter kan jeg ikke bruge.
Kan du komme lidt nærmere ind på det? Skal programmet automatisk farve nogle ord en speciel farve hver gang de optræder...ligesom Delphi's egen editor ? eller skal brugeren kunne markere noget tekst og give det en farve?
"Speedy - richedit1.SelAttibutes.Color := clGreen; - gør hele Richedtit'en grøn ikke teksten" Prøv med RichEdit1.SelAttibutes.Font.Color := clGreen; :-)
Citat fra SpEeDy "Skal programmet automatisk farve nogle ord en speciel farve hver gang de optræder...ligesom Delphi's egen editor ?" og det er lige det den skal kunne.
for I := 0 to length(text) do if copy(text, I, length(Ord)) = Ord then begin Richedit.SelStart := I - 1; Richedit.SelLength := length(ord); Richedit.SelAttributes.Color := clred; end; end;
procedure TForm1.Button1Click(Sender: TObject); begin Highlightword(RichEdit1, 'hej'); end;
k, takker... Men hvis den nu skal køre og Highlight imens jeg skriver i Richedit1... Den skal også Highlight alle de som jeg har i en liste fx ord ved åbning af dokument.
for I := 0 to length(text) do if copy(text, I, length(Ord)) = Ord then begin Richedit.SelStart := I - 1; Richedit.SelLength := length(ord); Richedit.SelAttributes.Color := clred; end; end;
procedure Highlightword(Richedit: TRichEdit; Ord: string; Color: TColor); var i: integer; text: string; begin text := Richedit.Lines.text; for I := 0 to length(text) do if copy(text, I, length(Ord)) = Ord then begin Richedit.SelStart := I - 1; Richedit.SelLength := length(ord); Richedit.SelAttributes.Color := Color; end; Richedit.SelStart := I; Richedit.SelLength := 0; Richedit.SelAttributes.Color := clBlack; end;
Og læg Highlightword(RichEdit1, 'hej'); i OnChance for RichEdit.
Ord := AnsiLowerCase(Ord); text := AnsiLowerCase(Richedit.Lines.text); for I := 0 to length(text) do if copy(text, I, length(Ord)) = Ord then begin Richedit.SelStart := I - 1; Richedit.SelLength := length(ord); Richedit.SelAttributes.Color := Color; end; Richedit.SelStart := OldSelStart; Richedit.SelLength := OldSelLength; Richedit.SelAttributes.Color := clBlack; end;
procedure TForm1.RichEdit1Change(Sender: TObject); var aRichEdit: TRichEdit; begin aRichEdit := Sender as TRichEdit; Highlightword(aRichEdit, 'hest', clRed); Highlightword(aRichEdit, 'pony', clBlue); end;
Synes godt om
Slettet bruger
08. august 2003 - 17:51#27
procedure Highlightword(Richedit: TRichEdit; Ord: string; Color: TColor); var i: integer; text: string; begin text := Richedit.Lines.text; for I := 0 to length(text) do if copy(text, I, length(Ord)) = Ord then begin Richedit.SelStart := I - 1; Richedit.SelLength := length(ord); Richedit.SelAttributes.Color := Color; end; Richedit.SelStart := I; Richedit.SelLength := 0; Richedit.SelAttributes.Color := clBlack; end;
procedure HighLightListeAfOrd(RichEdit:TRichEdit; Liste: Array of String; Farve: TColor); var I:Integer; begin for I := Low(Liste) to High(Liste) do begin Highlightword(RichEdit,Liste[I],Farve); end; end;
procedure TForm1.RichEdit1Change(Sender: TObject); var OrdListe: array[0..2] of string; I:Integer; begin OrdListe[0] := '1'; OrdListe[1] := '2'; OrdListe[2] := '3'; HighLightListeAfOrd(RichEdit1,OrdListe,clRed); end;
Du skal nok også bruge HighLightListeAfOrd(RichEdit1,OrdListe,clRed); den når du loader filen.
dl -> Den her funktion gør det med en TStrings eller en TStringList:
procedure HighlightList(Richedit: TRichEdit; Liste: TStrings; Color: TColor); var i, j: integer; text: string; OldSelStart, OldSelLength: Integer; begin OldSelStart := Richedit.SelStart; OldSelLength := Richedit.SelLength;
Liste.Text := AnsiLowerCase(Liste.Text); text := AnsiLowerCase(Richedit.Lines.text); for J := 0 to Liste.Count -1 do for I := 1 to length(text) do if copy(text, I, length(Liste[J])) = Liste[J] then begin Richedit.SelStart := I - 1; Richedit.SelLength := length(Liste[J]); Richedit.SelAttributes.Color := Color; end; Richedit.SelStart := OldSelStart; Richedit.SelLength := OldSelLength; Richedit.SelAttributes.Color := clBlack; end;
Prøv den her: procedure HighlightList(Richedit: TRichEdit; Liste: TStrings; Color: TColor); var i, j: integer; text: string; OldSelStart, OldSelLength: Integer; OldFocus: Boolean; begin OldFocus := RichEdit.Focused; if OldFocus then RichEdit.Parent.SetFocus;
Liste.Text := AnsiLowerCase(Liste.Text); text := AnsiLowerCase(Richedit.Lines.text); for J := 0 to Liste.Count -1 do for I := 1 to length(text) do if copy(text, I, length(Liste[J])) = Liste[J] then begin Richedit.SelStart := I - 1; Richedit.SelLength := length(Liste[J]); Richedit.SelAttributes.Color := Color; end; Richedit.SelStart := OldSelStart; Richedit.SelLength := OldSelLength; Richedit.SelAttributes.Color := clBlack; RichEdit.Enabled := true; if OldFocus then RichEdit.SetFocus; end;
Synes godt om
Slettet bruger
08. august 2003 - 18:03#30
Prøv den her istedet for min anden:
procedure HighlightArray(Richedit: TRichEdit; Liste: Array of String; Color: TColor); var i,j: integer; text: string; begin for j := Low(Liste) to High(Liste) do begin text := LowerCase(Richedit.Lines.text); for I := 0 to length(text) do if copy(text, I, length(LowerCase(Liste[J]))) = LowerCase(Liste[J]) then begin Richedit.SelStart := I - 1; Richedit.SelLength := length(Liste[J]); Richedit.SelAttributes.Color := Color; end; Richedit.SelStart := I; Richedit.SelLength := 0; Richedit.SelAttributes.Color := clBlack; end; end;
procedure HighlightList(Richedit: TRichEdit; Liste: TStrings; Color: TColor); var i, j: integer; text: string; OldSelStart, OldSelLength: Integer; begin OldSelStart := Richedit.SelStart; OldSelLength := Richedit.SelLength;
Liste.Text := AnsiLowerCase(Liste.Text); text := AnsiLowerCase(Richedit.Lines.text); for J := 0 to Liste.Count -1 do for I := 1 to length(text) do if copy(text, I, length(Liste[J])) = Liste[J] then begin Richedit.SelStart := I - 1; Richedit.SelLength := length(Liste[J]); Richedit.SelAttributes.Color := Color; RichEdit.SelAttributes.Style := [fsBold];
Hvordan for jeg den til at uppercase ord'et fra den list...
Synes godt om
Slettet bruger
08. august 2003 - 18:24#32
procedure HighlightList(Richedit: TRichEdit; Liste: TStrings; Color: TColor); var i, j: integer; text: string; OldSelStart, OldSelLength: Integer; begin OldSelStart := Richedit.SelStart; OldSelLength := Richedit.SelLength;
Liste.Text := AnsiLowerCase(Liste.Text); text := AnsiLowerCase(Richedit.Lines.text); for J := 0 to Liste.Count -1 do for I := 1 to length(text) do if copy(text, I, length(Liste[J])) = Liste[J] then begin Richedit.SelStart := I - 1; Richedit.SelLength := length(Liste[J]); Richedit.SelAttributes.Color := Color; RichEdit.SelAttributes.Style := [fsBold]; Richedit.SelText := AnsiUpperCase(Richedit.SelText); end; Richedit.SelStart := OldSelStart; Richedit.SelLength := OldSelLength; Richedit.SelAttributes.Color := clBlack; end;
Lige en ting til ... og så kan du få 70 point ... Kan du ikke lige skrive en lille text, hvad den gør ved vær linie ...
fx ... text := AnsiLowerCase(Richedit.Lines.text); // alle bogstaver bliver små
procedure HighlightList(Richedit: TRichEdit; Liste: TStrings; Color: TColor); var i, j: integer; text: string; OldSelStart, OldSelLength: Integer; begin OldSelStart := Richedit.SelStart; OldSelLength := Richedit.SelLength;
Liste.Text := AnsiLowerCase(Liste.Text); text := AnsiLowerCase(Richedit.Lines.text); for J := 0 to Liste.Count -1 do for I := 1 to length(text) do if copy(text, I, length(Liste[J])) = Liste[J] then begin Richedit.SelStart := I - 1; Richedit.SelLength := length(Liste[J]); Richedit.SelAttributes.Color := Color; RichEdit.SelAttributes.Style := [fsBold]; Richedit.SelText := AnsiUpperCase(Richedit.SelText); end; RichEdit.SelAttributes.Style := []; Richedit.SelStart := OldSelStart; Richedit.SelLength := OldSelLength; Richedit.SelAttributes.Color := clBlack; end;
Lige en ting til ... og så kan du få 70 point ... Kan du ikke lige skrive en lille text, hvad den gør ved vær linie ...
fx ... text := AnsiLowerCase(Richedit.Lines.text); // alle bogstaver bliver små
procedure HighlightList(Richedit: TRichEdit; Liste: TStrings; Color: TColor); var i, j: integer; text: string; OldSelStart, OldSelLength: Integer; begin OldSelStart := Richedit.SelStart; OldSelLength := Richedit.SelLength;
Liste.Text := AnsiLowerCase(Liste.Text); text := AnsiLowerCase(Richedit.Lines.text); for J := 0 to Liste.Count -1 do for I := 1 to length(text) do if copy(text, I, length(Liste[J])) = Liste[J] then begin Richedit.SelStart := I - 1; Richedit.SelLength := length(Liste[J]); Richedit.SelAttributes.Color := Color; RichEdit.SelAttributes.Style := [fsBold]; Richedit.SelText := AnsiUpperCase(Richedit.SelText); end; RichEdit.SelAttributes.Style := []; Richedit.SelStart := OldSelStart; Richedit.SelLength := OldSelLength; Richedit.SelAttributes.Color := clBlack; end;
Prøv den her, den er mere avanceret: procedure HighlightWordsExp(aRichedit: TRichEdit; aLists: Array of TStrings; aColors: Array of TColor; aFontStyles: Array of TFontStyles; aUpCase: Array of Boolean); var i, J, K: integer; text: string; OldSelStart, OldSelLength: Integer; OldFocus: Boolean; begin OldFocus := aRichEdit.Focused; if OldFocus then aRichEdit.Parent.SetFocus;
text := AnsiLowerCase(aRichedit.Lines.text); for K := 0 to Length(aLists) -1 do begin aLists[K].Text := AnsiLowerCase(aLists[K].Text); for J := 0 to aLists[K].Count -1 do for I := 1 to length(text) do if copy(text, I, length(aLists[K][J])) = aLists[K][J] then begin aRichedit.SelStart := I - 1; aRichedit.SelLength := length(aLists[K][J]); if K < Length(aColors) then aRichedit.SelAttributes.Color := aColors[K]; if K < Length(aFontStyles) then aRichEdit.SelAttributes.Style := aFontStyles[K]; if K < Length(aUpCase) then if aUpCase[K] then aRichEdit.SelText := AnsiUpperCase(aRichEdit.SelText); end; end; aRichedit.SelStart := OldSelStart; aRichedit.SelLength := OldSelLength; aRichedit.SelAttributes.Color := clBlack; aRichEdit.Enabled := true; if OldFocus then aRichEdit.SetFocus; end;
Synes godt om
Slettet bruger
08. august 2003 - 19:30#36
procedure HighlightList(Richedit: TRichEdit; Liste: TStrings; Color: TColor); var i, j: integer; text: string; OldSelStart, OldSelLength: Integer; begin OldSelStart := Richedit.SelStart; OldSelLength := Richedit.SelLength;
Liste.Text := AnsiLowerCase(Liste.Text); //Gør ordene små text := AnsiLowerCase(Richedit.Lines.text); //Gør bogstaverne i richedit'en små for J := 0 to Liste.Count -1 do //Looper indtil den har highlightet alle ordene for I := 1 to length(text) do //Looper gennem alle bogstaverne i ordet if copy(text, I, length(Liste[J])) = Liste[J] then //Checker om bogstavet i text(richedit'en) er det samme som bogstavet i dit ord begin Richedit.SelStart := I - 1; //Begynder med at markere ved starten af ordet i richedit'en Richedit.SelLength := length(Liste[J]);//Og slutter markeringen efter ordet er slut Richedit.SelAttributes.Color := Color; //Sætter farven RichEdit.SelAttributes.Style := [fsBold];//Gør ordet fed Richedit.SelText := AnsiUpperCase(Richedit.SelText);//Ændre bogstaverne i ordet til store bogstaver end; Richedit.SelStart := OldSelStart; //Sætter coursoren tilbage på sin gamle plads Richedit.SelLength := 0; //Tror bare den skal være 0, fordi ingen bogstaver skal være markeret Richedit.SelAttributes.Color := clBlack;//Sætter farven til sort igen end;
Den her er noget langsom, men så er den til gengæld bedre egnet til f.eks. Delphi-kode:
type CommentType = record ctStart: String; ctEnd: String; ctOneLine: Boolean; ctFontStyle: TFontStyles; ctFontColor: TColor; end;
procedure HighlightWordsExp(aRichedit: TRichEdit; aLists: Array of TStrings; aColors: Array of TColor; aFontStyles: Array of TFontStyles; aUpCase: Array of Boolean; aComments: Array of CommentType); var i, J, K: integer; text: string; OldSelStart, OldSelLength: Integer; OldFocus: Boolean; IComment: Integer; begin OldFocus := aRichEdit.Focused; if OldFocus then aRichEdit.Parent.SetFocus;
for I := 1 to length(text) do begin for K := 0 to Length(aLists) -1 do begin aLists[K].Text := AnsiLowerCase(aLists[K].Text); for J := 0 to aLists[K].Count -1 do if (copy(text, I, length(aLists[K][J])) = aLists[K][J]) then begin aRichedit.SelStart := I - 1; aRichedit.SelLength := length(aLists[K][J]); if IComment = -1 then begin if K < Length(aColors) then aRichedit.SelAttributes.Color := aColors[K]; if K < Length(aFontStyles) then aRichEdit.SelAttributes.Style := aFontStyles[K]; end; if K < Length(aUpCase) then if aUpCase[K] then aRichEdit.SelText := AnsiUpperCase(aRichEdit.SelText); end; end; for J := 0 to Length(aComments) - 1 do begin if (IComment <> -1) and (IComment <> J) then Continue; if IComment = J then begin if aComments[J].ctOneLine then begin if Pos(copy(text, I, 1), #10 + #13) > 0 then IComment := -1; end else if copy(text, I, length(aComments[J].ctEnd)) = aComments[J].ctEnd then begin aRichEdit.SelStart := I - 1; aRichEdit.SelLength := length(aComments[J].ctEnd); aRichedit.SelAttributes.Color := aComments[J].ctFontColor; aRichedit.SelAttributes.Style := aComments[J].ctFontStyle; IComment := -1; end; end else if copy(text, I, length(aComments[J].ctStart)) = aComments[J].ctStart then IComment := J; if IComment <> -1 then begin aRichEdit.SelStart := I - 1; aRichEdit.SelLength := 1; aRichedit.SelAttributes.Color := aComments[J].ctFontColor; aRichedit.SelAttributes.Style := aComments[J].ctFontStyle; end; end; end; aRichedit.SelStart := OldSelStart; aRichedit.SelLength := OldSelLength; aRichedit.SelAttributes.Color := clBlack; aRichEdit.SelAttributes.Style := []; aRichEdit.Enabled := true; if OldFocus then aRichEdit.SetFocus; end;
procedure TForm1.RichEdit1Change(Sender: TObject); var aRichEdit: TRichEdit; aCommentType: Array of CommentType; I: Integer; begin aRichEdit := Sender as TRichEdit; SetLength(aCommentType, 3); aCommentType[0].ctStart := '//'; aCommentType[0].ctEnd := ''; aCommentType[0].ctOneLine := true; aCommentType[1].ctStart := '{'; aCommentType[1].ctEnd := '}'; aCommentType[1].ctOneLine := false; aCommentType[2].ctStart := '(*'; aCommentType[2].ctEnd := '*)'; aCommentType[2].ctOneLine := false; for I := 0 to 2 do begin aCommentType[I].ctFontColor := clBlue; aCommentType[I].ctFontStyle := [fsItalic]; end; HighlightWordsExp(aRichEdit, [Memo1.Lines, Memo2.Lines], [clGreen, clBlue], [[fsBold], []], [false, true], aCommentType); end;
Synes godt om
Slettet bruger
08. august 2003 - 23:20#39
Takker for point :-)
Synes godt om
Ny brugerNybegynder
Din løsning...
Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.