Detect click on URL in RichEdit
Asked Answered
S

3

7

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

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;

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?

Sigma answered 19/3, 2010 at 19:51 Comment(1)
M
9

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;
Manchukuo answered 20/3, 2010 at 4:0 Comment(0)
A
1

For me, it only works if the displayed text is the same text as the underlying hyperlink.

I think my problem is that the underlying hyperlink has the attribute CFE_HIDDEN, and so can't be selected by EM_EXSETSEL.

As an example, if I create (in WORD) a link with the url http://www.rubbish.com, but which displays the text RUBBISH, although the chrg of the selected text is 11-33 which is 22 characters - the same length as the URL, the actual text returned by the method is RUBBISH.

However, I've discovered that if I use WM_GETTEXT, the whole of the link is returned:

HYPERLINK "http://www.rubbish.com" RUBBISH

From which I can extract the URL based on the chrg.

It feels a little clumsy... but it works. :-)

Amman answered 30/3, 2011 at 14:14 Comment(2)
Since RichEdit mixes use of the CFE_LINK and CFE_HIDDEN effects for Friendly Name URLs (see blogs.msdn.com/b/murrays/archive/2009/09/24/…), perhaps you can try using EM_GETCHARFORMAT to scan the characters around the specified charrange to detect where the hidden URL is actually located.Manchukuo
For me WM_GETTEXT returns text with CRLF line-endings. While the ENLink.chgr seems to use indexes in text with singe-character (LF) line endings. So I had to convert the text to LF before extracting the URL using ENLink.chgr. Also note that the EM_EXSETSEL method work on hidden texts on Windows 10 (but not on Windows 7 and earlier). For details, see my answer at How to implement the mouse click for URLs at rich edit control.Ario
C
-1

Have you tried it with a stripped down application to make sure it isn't something else in your program causing problems? I followed the steps from that website in Delphi 2009 and clicking URLs worked just fine.

Cartie answered 20/3, 2010 at 3:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.