Visit our Sponsor   Visit our Sponsor
delphi3000.com - the free delphi knowledge platform
delphi3000.com - the free delphi knowledge platform
500 Users Online NOW
Have a look at your member-status

connecting people's knowledge


  - Recent ArticlesRSS feed for Recent Articles on delphi3000.com
  - List of All Articles
  - Top Viewed Articles
  - Articles (+Attachem.)
  - Articles Of Interest
  - Categories
  - Top Uploader
  - Search
  - Index

  - My Home
  - Submit an Article
  - My Articles
  - My Personal Data
  - My Bookmarks
  - Activities
  - Login/Logout

  - Sign Up
  - Why Sign Up
  - Newsletter

  - Press
  - Advertise

  - Contact
  - Feedback





Community
Borland
ClubeDelphi
Dr. Bob
UK-BUG
Delphi Meetings
Planeta Delphi







Startblatt.de






Share this article with friendsShare this article with friends
Rate this articleRate this article - to keep the quality of delphi3000.com !
Comment this article or read through previous comments (3)


standard RichEdit component and URL highlighting/navigationGo to Mike Shkolnik's websiteFormat this article printer-friendly!Bookmark function is only available for registered users!
Product:
Delphi all versions
Category:
Win API
Skill Level:
Scoring:
Last Update:
11/06/2002
Search Keys:
delphi delphi3000 article borland vcl code-snippet richedit richtext url site highlight navigate visit EM_AUTOURLDETECT AUTOURLDETECT event EM_SETEVENTMASK EM_GETEVENTMASK EN_LINK
Times Scored:
3
Visits:
5422
Uploader: Mike Shkolnik
Company: Scalabium Software
Reference: http://www.scalabium.com/faq/dct0146.htm
 
Question/Problem/Abstract:
How can I highlight URLs in RichEdit and how can I detect a mouse click in text where URL is?
Answer:



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:

var
  mask: Word;
begin
  mask := SendMessage(RichEdit1.Handle, EM_GETEVENTMASK, 0, 0);
  SendMessage(RichEdit1.Handle, EM_SETEVENTMASK, 0, mask or ENM_LINK);
  SendMessage(RichEdit1.Handle, EM_AUTOURLDETECT, Integer(True), 0);

  RichEdit1.Text := 'Scalabium Software'#13#10 +
    ' Site is located at www.scalabium.com. Welcome to our site.';
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 too)

3. now we must detect mouse clicks in URL range. For this task we must
override WndProc method of our form:
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;

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





Please rate this article!
Skill level:
BeginnerExpert

Useful:
No!Very!

Overall rating:
PoorExcellent



Comments to this article
Write a new comment
How to make it work with TPanel ot TNotebook
    Jimmy (Feb 24 2004 4:31AM)

Everything is fine if I drop RichEdit component on the form, but if I drop RichEdit component on TPanel, or TNotebook. WndProc method does not detect mouse clicks in URL range.

Please help!

Thanks,
Jimmy.
Respond

RE: How to make it work with TPanel ot TNotebook
Mike Shkolnik (Feb 24 2004 6:56AM)

If you dropped the RichEdit on another parent control (panel or page control etc), you must override the WndProc for this control.

In sample I created on form and overriden the WndProc for form. You dropped on panel, so override the WndProc for your panel (with same functionality in code)
Respond

Terrific article!
    salih guney (Nov 8 2002 12:26PM)

this article is very useful.
I appreciate the author.
Danko
Respond














 
Sign up to consume product discounts for Bronze memberships !

read more


  Visit our Sponsor

 

  Community Ad of
D. Wischnewski
 
   














 







     
  Copyright © 2000 - 2007 delphi3000.com - All rights reserved. Terms of use. || Privacy
delphi3000.com is a service by bluestep.com IT-Services GmbH (Vienna)