Articles   Members Online: 3
-Article/Tip Search
-News Group Search over 21 Million news group articles.
Member Area
-Account Center
-Top 10 NEW!!
-Submit Article/Tip
-Forums Upgraded!!
-My Articles
-Edit Information
-Become a Member
-Why sign up!
-Chat Online!
-Indexes NEW!!
-Build your resume
-Find a job
-Post a job
-Resume Search
-Link to us
Visit Embarcadero
Embarcadero Community
Standard RichEdit component and URL highlighting/navigation Turn on/off line numbers in source code. Switch to Orginial background IDE or DSP color Comment or reply to this aritlce/tip for discussion. Bookmark this article to my favorite article(s). Print this article
Reporting /Printing
Delphi 2.x
User Rating
No Votes
# Votes
DSP, Administrator
Reference URL:
			Author: Mike Shkolnik

How can I highlight URLs in RichEdit and how can I detect a mouse click in text 
where URL is?


Very popular question in delphi forums is: how can I highlight URLs in RichEdit and 
how can I detect a mouse click in text where URL is... And everytime I see the 
answers like "go to XXXX site and use this superb XXX product instead RichEdit". 

Today I want to show how to implement URL highlighting and URL navigation without 
any third-party components. This functionality is implemented in RichEdit from 
Microsoft (and MS Outlook use this feature, for example) and  only Borland's 
developers didn't publish it for us. 

So what we need: 

1. drop on your form a RichEdit component from win32 page of component palette 
2. in OnCreate event of your form write the next code: 

1   var
2     mask: Word;
3   begin
4     mask := SendMessage(RichEdit1.Handle, EM_GETEVENTMASK, 0, 0);
5     SendMessage(RichEdit1.Handle, EM_SETEVENTMASK, 0, mask or ENM_LINK);
6     SendMessage(RichEdit1.Handle, EM_AUTOURLDETECT, Integer(True), 0);
8     RichEdit1.Text := 'Scalabium Software'#13#10 +
9       ' Site is located at Welcome to our site.';
10  end;

After that your Richedit will convert automatically any URLs in highlighted (blue 
color and underlined). Even if you'll start to enter any text directly in Richedit, 
any begings for URL will be converted too (not only existing text string but new 

3. now we must detect mouse clicks in URL range. For this task we must override 
WndProc method of our form: 

11  type
12    TForm1 = class(TForm)
13    protected
14      procedure WndProc(var message: TMessage); override;
15    end;
16    {... }
18  procedure TForm1.WndProc(var message: TMessage);
19  var
20    p: TENLink;
21    strURL: string;
22  begin
23    if (message.Msg = WM_NOTIFY) then
24    begin
25      if (PNMHDR(message.LParam).code = EN_LINK) then
26      begin
27        p := TENLink(Pointer(TWMNotify(message).NMHdr)^);
28        if (p.msg = WM_LBUTTONDOWN) then
29        begin
30          SendMessage(RichEdit1.Handle, EM_EXSETSEL, 0, LongInt(@(p.chrg)));
31          strURL := RichEdit1.SelText;
32          ShellExecute(Handle, 'open', PChar(strURL), 0, 0, SW_SHOWNORMAL);
33        end
34      end
35    end;
37    inherited;
38  end;

Now you can compile your project (don't forget to include Richedit and ShellAPI 
units in uses clause) and your RichEdit component will work like a sharm. 

Of course, you can modify a code and process this parsed strURL as you like instead implemented navigation in browser as I did... 

Vote: How useful do you find this Article/Tip?
Bad Excellent
1 2 3 4 5 6 7 8 9 10


Share this page
Download from Google

Copyright © Mendozi Enterprises LLC