TRichEdit和URL高亮问题
我正在使用当前代码突出显示 TRichEdit 上的 URL:
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 停止工作。
这是在 Delphi 7 上的。
有什么建议吗? 谢谢!
I am using the current code to highlight URLs on a TRichEdit:
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;
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.
This is on Delphi 7.
Any suggestions?
thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的代码中有一个错误。替换
为
由于此错误,
mask
将不包含 Rich Edit 控件的默认事件位,因此当您EM_SETEVENTMASK
; 时,Rich Edit 控件会丢失这些事件标志。特别是,它将缺少ENM_CHANGE
位。更新
Sertac Akyuz 发现了另一个令人停止的错误:
mask
需要是一个整数(这确实是SendMessage
的结果类型)。There is a bug in your code. Replace
with
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 youEM_SETEVENTMASK
; in particular, it will lack theENM_CHANGE
bit.Update
Sertac Akyuz found yet another show-stopping bug:
mask
needs to be an integer (which indeed is the result type ofSendMessage
).