Author: Jonas Bilinkevicius
How to search for a certain font style in a TRichEdit
Answer:
Finding all bold-faced words in a TRichEdit control:
1 unit Unit1;
2
3 interface
4
5 uses
6 Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
7 StdCtrls,
8 ComCtrls;
9
10 type
11 TForm1 = class(TForm)
12 RichEdit1: TRichEdit;
13 ListBox1: TListBox;
14 Button1: TButton;
15 procedure Button1Click(Sender: TObject);
16 private
17 { Private declarations }
18 public
19 { Public declarations }
20 end;
21
22 var
23 Form1: TForm1;
24
25 implementation
26
27 {$R *.DFM}
28
29 procedure TForm1.Button1Click(Sender: TObject);
30 var
31 S: string;
32 wordstart, wordend: Integer;
33 begin
34 listbox1.clear;
35 listbox1.setfocus;
36 S := richedit1.text;
37 wordstart := 0;
38 repeat
39 {find start of next word}
40 repeat
41 Inc(wordstart);
42 until
43 (wordstart > Length(S)) or IsCharAlpha(S[wordstart]);
44 if wordstart <= Length(S) then
45 begin
46 {find end of word}
47 wordend := wordstart;
48 repeat
49 Inc(wordend);
50 until
51 (wordend > Length(S)) or not IsCharAlpha(S[wordend]);
52 {we have a word, select it in the rich edit}
53 with richedit1 do
54 begin
55 selstart := wordstart - 1; {character index is 0 based!}
56 sellength := wordend - wordstart;
57 {check the attributes}
58 if (fsBold in SelAttributes.Style) and (caBold in
59 SelAttributes.ConsistentAttributes) then
60 {we have a winna, add it to the listbox}
61 listbox1.items.add(Copy(S, wordstart, wordend - wordstart));
62 end;
63 wordstart := wordend;
64 end;
65 until
66 wordstart >= Length(S);
67 end;
68
69 end.
|