Autor Beitrag
Timelesk
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 82


Delphi 2006 Prof
BeitragVerfasst: So 18.10.09 20:58 
Hallo,

nach viel Sucherei und dennoch ständigem Scheitern möchte ich hier "mal wieder" eine Frage zur Formatierung im RichEdit stellen.

Ich möchte in einer Zeile mehrere Formatierungen einfügen.
Mein Problem dabei ist, dass mir die letzte Formatierung immer flöten geht, sobald ich die nächste Formatierung einfüge.

Diese Code funktioniert einwandfrei:
ausblenden volle Höhe Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
function TMain.AddText(const Text: string; const Format: string): boolean;
begin
  if Format = 'normal' then
    begin
      with Editor.SelAttributes do
        begin
          Color := clWindowText;
          Size  := 10;
          Style := [];
        end;
    end
  else if Format = 'h1' then
    begin
      with Editor.SelAttributes do
        begin
          Color := $00555555;
          Size  := 12;
          Style := [fsBold];
        end;
    end
  else if Format = 'h2' then
    begin
      with Editor.SelAttributes do
        begin
          Color := $00555555;
          Size  := 10;
          Style := [fsItalic, fsBold];
        end;
    end;

  Editor.Lines.Add(Text);
end;


Jedoch muss ich hierfür immer eine neue Zeile beginnen.


Nun habe ich folgenden Code hier gefunden:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
  for i := 0 to Length(Editor.text) -1 do
  begin
    Editor.SelStart := i;
    Editor.SelLength := 1;

    if i mod 2 = 0 then
    begin
      Editor.SelAttributes.Color := clBlue;
    end else
    begin
      Editor.SelAttributes.Color := clRed;
    end;
  end;

und dachte: Ok, dann setzen wir die Formatierung eben nachdem der nächste Text eingefügt wurde und habe mir hierfür die Positionen gemerkt und anschließend mit SelStart und SelLength den Text rausgesucht. Hat aber auch nicht funktioniert, ich erhielt wieder nur die Formatierung des ersten Eintrags.

Wie schaffe ich es, dass ich folgende Zeile bekomme:

Datum: 18.10.2009

Und zwar wenn ich den Text einfüge.
Ich muss ja danach noch weiteren Text einfügen und wieder mit anderen Formatierungen.


Vielen Dank für eure Hilfe.

gruß
Timelesk
jaenicke
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 19346
Erhaltene Danke: 1754

W11 x64 (Chrome, Edge)
Delphi 12 Pro, C# (VS 2022), JS/HTML, Java (NB), PHP, Lazarus
BeitragVerfasst: So 18.10.09 22:59 
Wie wäre es so in etwa? (ungetestet, habe gerade kein Delphi da)
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
function TMain.AddText(AEditor: TRichEdit; const AText, AFormat: string): boolean;
const
  FormatNames: array[0..2] of string = ('normal', 'h1', 'h2');
  FormatColors: array[0..2] of TColor = (clWindowText, $00555555, $00555555);
  FormatSizes: array[0..2] of Integer = (10, 12, 10);
  FormatStyles: array[0..2] of TFontStyles = ([], [fsBold], [fsItalic, fsBold]);
var
  i, TextStart: Integer;
  SelTextAttr: TTextAttributes;
begin
  Result := False;
  TextStart := AEditor.SelStart;
  AEditor.SelText := AText;
  AEditor.SelStart := TextStart;
  AEditor.SelLength := Length(AText);
  SelTextAttr := AEditor.SelAttributes;
  for i := Low(FormatNames) to High(FormatNames) do
    if AFormat = FormatNames[i] then
    begin
      SelTextAttr.Color := FormatColors[i];
      SelTextAttr.Size := FormatSizes[i];
      SelTextAttr.Style := FormatStyles[i];
      Result := True;
      Break;
    end;
  AEditor.SelStart := AEditor.SelStart + AEditor.SelLength;
end;
Timelesk Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 82


Delphi 2006 Prof
BeitragVerfasst: Mo 19.10.09 14:28 
Hi,

vielen Dank für deine Hilfe.
Es funktioniert bisher tadellos!

Den einzigen Unterschied (außer der Optimierung), den ich entdecken konnte war, dass du dich von Anfang an über die Länge des Markierten Textes und setzen der SelStart-Position durch den Inhalt hangelst.

Komische Sache.
Ich versteh's zwar nicht ganz, aber ich werd's mir merken.

Herzlichen Dank nochmal.

gruß
Timelesk