TRichEdit and URL highlighting problems

微笑、不失礼 提交于 2020-01-04 06:19:06

问题


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!


回答1:


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).



来源:https://stackoverflow.com/questions/3559356/trichedit-and-url-highlighting-problems

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!