TRichEdit和URL高亮问题

发布于 2024-09-15 11:18:58 字数 1137 浏览 5 评论 0原文

我正在使用当前代码突出显示 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

鼻尖触碰 2024-09-22 11:18:58

您的代码中有一个错误。替换

mask := SendMessage(Handle, EM_GETEVENTMASK, 0, 0);

mask := SendMessage(RichEdit1.Handle, EM_GETEVENTMASK, 0, 0);

由于此错误,mask 将不包含 Rich Edit 控件的默认事件位,因此当您 EM_SETEVENTMASK; 时,Rich Edit 控件会丢失这些事件标志。特别是,它将缺少 ENM_CHANGE 位。

更新

Sertac Akyuz 发现了另一个令人停止的错误:mask 需要是一个整数(这确实是 SendMessage 的结果类型)。

There is a bug in your code. Replace

mask := SendMessage(Handle, EM_GETEVENTMASK, 0, 0);

with

mask := SendMessage(RichEdit1.Handle, EM_GETEVENTMASK, 0, 0);

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.

Update

Sertac Akyuz found yet another show-stopping bug: mask needs to be an integer (which indeed is the result type of SendMessage).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文