TRichEdit和URL突出显示问题 [英] TRichEdit and URL highlighting problems
问题描述
我正在使用当前代码在TRichEdit上突出显示URL:
I am using the current code to highlight URLs on a TRich
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;
procedure TForm1.InitRichEditURLDetection;
var
mask: Word;
begin
mask := SendMessage(Handle, EM_GETEVENTMASK, 0, 0);
SendMessage(RichEdit1.Handle, EM_SETEVENTMASK, 0, mask or ENM_LINK);
SendMessage(RichEdit1.Handle, EM_AUTOURLDETECT, Integer(True), 0);
form1.RichEdit1.OnChange := form1.RichEdit1Change;
end;
它突出显示了URL,但是阻止了我的RichEdit1.OnChange被调用.我尝试从WndProc和其他方法中再次进行设置,但是没有任何效果.在启用URL荧光笔(通过在FormCreate上调用InitRichEditURLDetection)的那一刻,OnChange停止工作.
It highlights the URLs, however it prevent my RichEdit1.OnChange from being called. I trying setting again from within WndProc and other approaches but nothing works. The minute I enable the URL highlighter (by calling InitRichEditURLDetection on FormCreate) OnChange stops working.
这是在Delphi 7上.
This is on Delphi 7.
有什么建议吗? 谢谢!
Any suggestions? thanks!
推荐答案
您的代码中有错误.替换
There is a bug in your code. Replace
mask := SendMessage(Handle, EM_GETEVENTMASK, 0, 0);
使用
mask := SendMessage(RichEdit1.Handle, EM_GETEVENTMASK, 0, 0);
由于此错误,mask
将不包含Rich Edit控件的默认事件位,因此,当您EM_SETEVENTMASK
时,Rich Edit控件会放开这些事件标志.特别是它将缺少ENM_CHANGE
位.
Because of this bug, mask
will not contain the default event bits of the Rich Edit control, so the Rich Edit control looses these event flags when you EM_SETEVENTMASK
; in particular, it will lack the ENM_CHANGE
bit.
Sertac Akyuz发现了另一个令人大跌眼镜的错误:mask
必须是整数(确实是SendMessage
的结果类型).
Sertac Akyuz found yet another show-stopping bug: mask
needs to be an integer (which indeed is the result type of SendMessage
).
这篇关于TRichEdit和URL突出显示问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!