Author: Attila T.P.
URL highlighting in TRichEdit
Answer:
This functionality is implemented in RichEdit from Microsoft (and MS Outlook use
this feature, for example) and
only Borland's developers didn't publish it for us.)
1 protected
2
3 procedure WndProc(var message: TMessage); override;
4
5 {....}
6
7 uses Richedit, ShellApi;
8
9 procedure TForm1.FormCreate(Sender: TObject);
10 var
11 mask: Word;
12 begin
13 mask := SendMessage(RichEdit1.Handle, EM_GETEVENTMASK, 0, 0);
14 SendMessage(RichEdit1.Handle, EM_SETEVENTMASK, 0, mask or ENM_LINK);
15 SendMessage(RichEdit1.Handle, EM_AUTOURLDETECT, Integer(True), 0);
16
17 //Some text in RichEdit
18 RichEdit1.Text := 'Scalabium Software'#13#10 +
19 ' Site is located at www.scalabium.com. Welcome to our site.';
20 end;
21
22 procedure TForm1.WndProc(var message: TMessage);
23 var
24 p: TENLink;
25 strURL: string;
26 begin
27 if (message.Msg = WM_NOTIFY) then
28 begin
29 if (PNMHDR(message.lParam).code = EN_LINK) then
30 begin
31 p := TENLink(Pointer(TWMNotify(message).NMHdr)^);
32 if (p.Msg = WM_LBUTTONDOWN) then
33 begin
34 SendMessage(RichEdit1.Handle, EM_EXSETSEL, 0, Longint(@(p.chrg)));
35 strURL := RichEdit1.SelText;
36 ShellExecute(Handle, 'open', PChar(strURL), 0, 0, SW_SHOWNORMAL);
37 end
38 end
39 end;
40
41 inherited;
42 end;
|