检测点击RichEdit中的URL [英] Detect click on URL in RichEdit
问题描述
我正在尝试更新RichEdit,以便它检测到URL,并允许点击它在浏览器中打开。检测网址很简单,只需使用 http://www.scalabium.com/faq中的以下代码/dct0146.htm
I am trying to update RichEdit so that it detects URL and enables clicking on it to open in the browser. Detecting URL is easy, I just use the following code from http://www.scalabium.com/faq/dct0146.htm
mask := SendMessage(MNote.Handle, EM_GETEVENTMASK, 0, 0);
SendMessage(MNote.Handle, EM_SETEVENTMASK, 0, mask or ENM_LINK);
SendMessage(MNote.Handle, EM_AUTOURLDETECT, Integer(True), 0);
但第二部分对我来说无效。他们提供以下代码来捕获EN_LINK消息并处理它:
but the second part doesn't work for me. They give the following code to capture EN_LINK message and processing it:
type
TForm1 = class(TForm)
protected
procedure WndProc(var Message: TMessage); override;
end;
...
procedure TForm1.WndProc(var Message: TMessage);
var
p: TENLink;
strURL: string;
begin
if (Message.Msg = WM_NOTIFY) then
begin
if (PNMHDR(Message.LParam).code = EN_LINK) then
begin
p := TENLink(Pointer(TWMNotify(Message).NMHdr)^);
if (p.msg = WM_LBUTTONDOWN) then
begin
SendMessage(RichEdit1.Handle, EM_EXSETSEL, 0, LongInt(@(p.chrg)));
strURL := RichEdit1.SelText;
ShellExecute(Handle, 'open', PChar(strURL), 0, 0, SW_SHOWNORMAL);
end
end
end;
inherited;
end;
当我运行程序时,检测到URL,但点击它不会做任何事情。使用调试我发现当我点击URL时,Message.Msg = WM_NOTIFY不正确。然后我试图覆盖TRichEdit的WndProc,但结果是一样的。任何建议?
When I run the program, URL is detected, but clicking on it doesn't do anything. Using debug I found out that Message.Msg = WM_NOTIFY is not true when I click on URL. I then tried to override TRichEdit's WndProc, but result is the same. Any suggestions?
推荐答案
对RichEdit的WindowProc属性进行子类化,查找CN_NOTIFY消息,例如:
Subclass the RichEdit's WindowProc property and look for the CN_NOTIFY message, for example:
type
TForm1 = class(TForm)
RichEdit1: TRichEdit;
procedure FormCreate(Sender: TObject);
private
PrevRichEditWndProc: TWndMethod;
procedure RichEditWndProc(var Message: TMessage);
procedure SetRichEditMasks;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
PrevRichEditWndProc := RichEdit1.WindowProc;
RichEdit1.WindowProc := RichEditWndProc;
SetRichEditMasks;
end;
procedure TForm1.SetRichEditMasks;
var
mask: Longint;
begin
mask := SendMessage(RichEdit1.Handle, EM_GETEVENTMASK, 0, 0);
SendMessage(RichEdit1.Handle, EM_SETEVENTMASK, 0, mask or ENM_LINK);
SendMessage(RichEdit1.Handle, EM_AUTOURLDETECT, 1, 0);
end;
procedure TForm1.RichEditWndProc(var Message: TMessage);
begin
PrevRichEditWndProc(Message);
case Message.Msg of
CN_NOTIFY:
begin
if (TWMNotify(Message).NMHdr^.code = EN_LINK) then
begin
with PENLink(Message.LParam)^ do
begin
if (msg = WM_LBUTTONDOWN) then
begin
SendMessage(RichEdit1.Handle, EM_EXSETSEL, 0, LongInt(@chrg));
ShellExecute(Handle, 'open', PChar(RichEdit1.SelText), 0, 0, SW_SHOWNORMAL);
end;
end;
end;
end;
CM_RECREATEWND:
begin
SetRichEditMasks;
end;
end;
end;
这篇关于检测点击RichEdit中的URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!