-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuRichEditWithLinks.pas
128 lines (113 loc) · 3.56 KB
/
uRichEditWithLinks.pas
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
{*******************************************************}
{ }
{ Enchanced RichEdit Delphi Component }
{ }
{ Copyright (c) 2001, Alex Petorv, AuRoom group }
{ Obninsk, Russia }
{ }
{ mailto: [email protected] }
{ http://auroom.obninsk.ru }
{*******************************************************}
unit uRichEditWithLinks;
{
This component is a Rich edit with additional function: hyperlink support.
There are 2 public function for setting and resetting hyperlink attribute
And an event, fires where hyperlink was clicked.
}
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, ComCtrls, RichEdit;
type
TTextRange = TTextRangeA;
TLinkClicked = procedure (Sender: TObject;
cpMin, cpMax: LongInt;
const lpstrText: string
) of object;
TRichEditWithLinks = class(TRichEdit)
private
{ Private declarations }
FLinkClicked: TLinkClicked;
procedure CNNotify(var Message: TWMNotify); message CN_NOTIFY;
protected
{ Protected declarations }
procedure CreateWnd; override;
procedure SetHyperlink(Hyperlink: Boolean; wParam: Integer);
public
{ Public declarations }
// Make current selection a hyperlink
procedure SetSelectionHyperlink(Hyperlink: Boolean);
// Make a word under cursor hyperlink
procedure SetWordHyperlink(Hyperlink: Boolean);
published
{ Published declarations }
property OnHyperlinkClicked: TLinkClicked read FLinkClicked Write FLinkClicked;
end;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('Win32', [TRichEditWithLinks]);
end;
{ TRichEditWithLinks }
procedure TRichEditWithLinks.CNNotify(var Message: TWMNotify);
type
PENLink = ^TENLink;
var
TR: TextRange;
begin
if (Message.NMHdr^.code=EN_LINK) then
begin
if (PENLink(Message.NMHdr).Msg=WM_LBUTTONDOWN) and Assigned(FLinkClicked) then
begin
TR.chrg := PENLink(Message.NMHdr).chrg;
GetMem(TR.lpstrText, TR.chrg.cpMax - TR.chrg.cpMin + 2);
try
SendMessage(Handle, EM_GETTEXTRANGE, 0, Integer(@TR));
FLinkClicked(Self, TR.chrg.cpMin, TR.chrg.cpMax, TR.lpstrText);
finally
FreeMem(TR.lpstrText);
end;
end;
Message.Result := 0;
end
else
inherited;
end;
procedure TRichEditWithLinks.CreateWnd;
begin
inherited;
// Updating Richedit EventMask.
// Adding hyperlinks support
SendMessage (
Handle,
EM_SETEVENTMASK, 0,
SendMessage(Handle, EM_GETEVENTMASK, 0,0) or ENM_LINK
);
end;
procedure TRichEditWithLinks.SetHyperlink(Hyperlink: Boolean;
wParam: Integer);
var
cf: TCharFormat;
begin
FillChar(cf, SizeOf(cf), 0);
cf.cbSize := SizeOf(cf);
cf.dwMask := CFM_LINK or CFM_COLOR or CFM_UNDERLINE;
if Hyperlink then
begin
cf.dwEffects := CFE_LINK or CFE_UNDERLINE;
cf.crTextColor := COLORREF(clBlue);
end
else
cf.crTextColor := Font.Color;
SendMessage(Handle, EM_SETCHARFORMAT, wParam, integer(@cf));
end;
procedure TRichEditWithLinks.SetSelectionHyperlink(Hyperlink: Boolean);
begin
SetHyperlink(Hyperlink, SCF_SELECTION);
end;
procedure TRichEditWithLinks.SetWordHyperlink(Hyperlink: Boolean);
begin
SetHyperlink(Hyperlink, SCF_WORD or SCF_SELECTION);
end;
end.