Autor Beitrag
ZO_orki
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 42



BeitragVerfasst: Do 08.06.06 19:34 
Hallo, mein Problem ist folgendes:

Ich möchte in einer Zeile einen bestimmte Länge von Text hervorheben, indem ich ihn fett mache. Das klappt leider nur insofern, dass ich den gesamten Text der Komponente fett mache.
Ich schaffe es auch irgendwie nicht, die Einstellung wieder rückgängig zu machen.

Ich würde gerne Quellcode posten, das macht aber keinen sinn, da ich bislang nur ausprobiert habe.

Wäre super, wenn ihr mir sagen könntet, wie ich meinen Wunsch umsetzte, oder mir zumindestens Denkanstöße gebt.

MfG ZO_orki
Lannes
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 2352
Erhaltene Danke: 4

Win XP, 95, 3.11, IE6
D3 Prof, D4 Standard, D2005 PE, TurboDelphi, Lazarus, D2010
BeitragVerfasst: Do 08.06.06 19:59 
Hallo,

wenn Du es mit SelAttributes gecodet hast, dann poste mal Deinen Code.
Ist das nicht der Fall, dann such mal hier im Forum danach, da findest Du bestimmt was Passendes.

_________________
MfG Lannes
(Nichts ist nicht Nichts) and ('' <> nil ) and (Pointer('') = nil ) and (@('') <> nil )
JayK
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 1013



BeitragVerfasst: Do 08.06.06 20:27 
Stichwort SelAtrributes, der markierte Text wird über diese Eigenschaft formatiert. Damit du deine bestimmte Länge Text also fett bekommst, markierst (Properties: SelStart, SelLength) du den Text und dann musst du dir SelAttributes angucken, wie das Ding zu benutzen is, weiß ich nicht ausm Kopf :angel: da is glaube ich ein Set Style drin, bin mir aber nich sicher, einfach nachschauen.
ZO_orki Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 42



BeitragVerfasst: Do 08.06.06 21:04 
Ich machs im Moment folgendermaßen:

Ich "weiß" breits beim einfügen des Textes, ob er Fett oder nicht Fett werden soll.
Daher:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
VAR Pos: Integer;
begin
  Pos := Length(Memo.Text) + 1; // Ab hier soll markiert werden, als ab da, wo
  // der neue Text eingefügt wird

  Memo.Lines.Add(string1); // Der Text wird eingefügt
  Memo.SelStart := Pos; // Ab hier wird markiert
  Memo.SelLength := Length(Memo.Text) - Pos; // uns zwar bis zum Ende
  Memo.SelAttributes.Style := [fsBold]; // Das ganze wird fett.

  // Jetzt soll ans Ende der Zeile im Prinzip ein nicht mehr fetter Text angehängt werden
  Memo.Lines.Strings[Memo.Lines.Count-1] := Lines.Strings[chat_fenster.Lines.Count-1] + string2;
  
  Memo.SelStart := Length(Memo.Text) - Length(string2); // markiert den neu eingefügten
  Memo.SelLength := Length(string2); // Bereich
  Memo.SelAttributes.Style := []; // Und hier habe ich ein Problem. Wie setzte ich die Eigenschaften
 //  wieder auf "Standart?" Mit [] geht es nicht. [fsNone] oder [fsNormal] haut auch nicht hin
  
end;


Und das ist letztendlich mein Problem, dass ich nicht weiß, wie ich die einstellungen zurücksetzte...
JayK
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 1013



BeitragVerfasst: Do 08.06.06 21:12 
Hmm das wundert mich.. ich hätte es wohl auch so gemacht.
Ich merk mir solche Sachen immer nicht: wie sieht das Subtrahieren bei Sets aus?
Bei C# mit Flags hätte ich da schon ne Idee, aber hier...
Lannes
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 2352
Erhaltene Danke: 4

Win XP, 95, 3.11, IE6
D3 Prof, D4 Standard, D2005 PE, TurboDelphi, Lazarus, D2010
BeitragVerfasst: Do 08.06.06 22:09 
Hallo,

user profile iconZO_orki hat folgendes geschrieben:

ausblenden Delphi-Quelltext
1:
  Memo.Lines.Strings[Memo.Lines.Count-1] := Lines.Strings[chat_fenster.Lines.Count-1] + string2;					
Lines von welcher Komponente?

user profile iconZO_orki hat folgendes geschrieben:
Und das ist letztendlich mein Problem, dass ich nicht weiß, wie ich die einstellungen zurücksetzte...
welche Einstellung willst Du denn zurücksetzen? Du hast doch nur den mit SelStart und SelLength festgelegten Teil formatiert.
Kannst doch direkt danach mit Add weiterarbeiten:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
VAR Pos: Integer;  
begin  
  Pos := Length(Memo.Text) + 1; // Ab hier soll markiert werden, als ab da, wo  
  // der neue Text eingefügt wird  
 
  Memo.Lines.Add('string1 in fsBold'); // Der Text wird eingefügt  
  Memo.SelStart := Pos; // Ab hier wird markiert  
  Memo.SelLength := Length(Memo.Text) - Pos; // uns zwar bis zum Ende  
  Memo.SelAttributes.Style := [fsBold]; // Das ganze wird fett.
  Memo.Lines.Add('String in Normal-Formatierung');

_________________
MfG Lannes
(Nichts ist nicht Nichts) and ('' <> nil ) and (Pointer('') = nil ) and (@('') <> nil )
ZO_orki Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 42



BeitragVerfasst: Do 08.06.06 22:32 
Ja, dein Beispiel funktioniert prima, ich möchte nun aber in einer Zeile fetten und normalen Text haben. Sozusagen einzelne Wörter hervorheben. Wie mache ich das?

PS: Oben im Code habe ich ein Memo. vergessen. Das Lines bezieht sich aufs TRichedit


Edit: Hat sich gegessen, ich bin drauf gekommen:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
  start := Length(Memo.Text);
  Memo.Lines.Add(string1);
  ende := Length(Memo.Text) - start - 3; // Die 3 kann ich mir nicht erklären, aber ohne sie gehts nicht...

  Memo.Lines.Strings[Memo.Lines.Count-1] := Memo.Lines.Strings[Memo.Lines.Count-1] + string2;
  Memo.SelStart := start;
  Memo.SelLength := ende;
  Memo.SelAttributes.Style := [fsBold];
Lannes
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 2352
Erhaltene Danke: 4

Win XP, 95, 3.11, IE6
D3 Prof, D4 Standard, D2005 PE, TurboDelphi, Lazarus, D2010
BeitragVerfasst: Do 08.06.06 22:53 
Hallo,

damit ist es variabler/einfacher anzuwenden, SelLength wird nicht benötigt:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
var Pos: Integer;
begin
  Pos := Length(RichEdit1.Text) + 1;
  RichEdit1.SelStart := Pos;
  RichEdit1.SelText := 'normal ';

  Pos := Pos+Length('normal ');
  RichEdit1.SelStart := Pos;
  RichEdit1.SelAttributes.Style := [fsBold];
  RichEdit1.SelText := 'fett ';

  Pos := Pos+Length('fett ');
  RichEdit1.SelStart := Pos;
  RichEdit1.SelAttributes.Style := [];
  RichEdit1.SelText := 'wieder normal '+#13#10;//neue Zeile
  //...

und das dann in entsprechende Funktionen verpacken.

_________________
MfG Lannes
(Nichts ist nicht Nichts) and ('' <> nil ) and (Pointer('') = nil ) and (@('') <> nil )
DaK
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 16

Win 2000, Windows XP
Delphi 2007 Professional
BeitragVerfasst: Mi 06.09.06 23:59 
Wegen des selben "Problems" habe ich eben eine kleine procedure geschrieben, die zugegeben leider nicht richtig funktioniert hat. :shock: Mit dieser hier (aus dem Post über mir entstanden) geht es aber wunderbar:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
    procedure AddStyledText(s:string; style:TFontStyles; var aRtf:TRichEdit);
    begin
      artf.SelStart := strlen(pchar(artf.text));
      artf.SelAttributes.Style := style;
      artf.SelText := s;
    end;


das selbe in grün für Colors:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
    procedure AddColoredText(s:string; c:TColor; var aRtf:TRichEdit);
    begin
      artf.SelStart := strlen(pchar(artf.text));
      artf.SelAttributes.Color := c;
      artf.SelText := s;
    end;


Die Anwendung könnte z.B. so aussehen:

ausblenden Delphi-Quelltext
1:
2:
3:
    AddStyledText('normal,',[],Form1.Richedit1);
    AddStyledText('fett,',[fsbold],Form1.Richedit1);
    AddStyledText('kursiv',[fsItalic],Form1.Richedit1);


Die Procedure hängt den in s angegebenen String mit den style/color-Angaben an den richtext an.
sonic159
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 76

Windows XP
BDS 2006 Professional
BeitragVerfasst: Do 07.09.06 02:52 
Da wir bzw. ihr gerade bei dem Thema "RichEdit gestalten" seid,
könntet ihr mir eventuell helfen indem ihr beichtet wie man ein wort im RichEdit sucht und es unterstreicht oder farbig macht?

Danke!
DaK
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 16

Win 2000, Windows XP
Delphi 2007 Professional
BeitragVerfasst: Do 07.09.06 15:11 
@sonic159 Versuch es mal hiermit:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
procedure UnderlineText(s:string; var aRtf:TRichedit);
  function WhereStrInStr(s:string;z:string):integer;
  var i:integer;
  begin
    WhereStrInStr:=0;
    for i:=1 to StrLen(PChar(s)) do
    begin
      if z=MidStr(s,i,StrLen(PChar(z))) then
      begin
        WhereStrInStr:=i;
        Exit;
      end;
    end;
  end;
begin
  aRtf.SelStart:=WhereStrInStr(aRtf.Text,s)-1;
  aRtf.SelLength:=StrLen(PChar(s));
  aRtf.SelAttributes.Style := [fsUnderline];
end;


Und dann einfach:

ausblenden Delphi-Quelltext
1:
2:
Richedit1.Text:='Hallo ich bin ein unterstrichener Text';
UnderlineText('unterstrichener', Richedit1);


Für Farben müsstest du einfach das Style-Attribut durch die gewünschte Farbe ersetzen.
sonic159
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 76

Windows XP
BDS 2006 Professional
BeitragVerfasst: Fr 08.09.06 03:41 
Hi!

Also mein Delphi meint das "MidStr" nicht dekläriert ist...
Lannes
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 2352
Erhaltene Danke: 4

Win XP, 95, 3.11, IE6
D3 Prof, D4 Standard, D2005 PE, TurboDelphi, Lazarus, D2010
BeitragVerfasst: Fr 08.09.06 06:55 
Hallo,

"MidStr" kennt Delphi 3 auch nicht, wird auch nicht benötigt, da Pos völlig ausreicht.

@user profile iconDaK:
Die Deklaration als var-Parameter (var aRtf:TRichedit) ist nicht erforderlich, da mit aRtf:TRichedit ein Zeiger auf das Richedit übergeben wird.
Desweiteren ist die PChar-Umwandlung überflüssig.

Die kurze Variante:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
procedure RTFUnderLine(s: String;RE: TRichEdit);
begin
  with RE do
    begin
    SelStart := Pos(s,Text)-1;
    SelLength := Length(s);
    with SelAttributes do
      Style := Style + [fsUnderline];
    end;
end;

_________________
MfG Lannes
(Nichts ist nicht Nichts) and ('' <> nil ) and (Pointer('') = nil ) and (@('') <> nil )
sonic159
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 76

Windows XP
BDS 2006 Professional
BeitragVerfasst: Fr 08.09.06 20:45 
Vielen Dank Lannes.
Auch Danke an dich DaK!

:)

MfG: sonic