Delphi Programming

  1. Home
  2. Computing & Technology
  3. Delphi Programming
TRichEditURL - hyperlink aware RichEdit
Full source code of the TRichEditURL Delphi component, an extension to the standard RichEdit component. The TRichEditURL automatically recognizes URLs. Whenever the text in a RichEditURL matches the format of a URL, the control will display it as a hyperlink - when the link is clicked an event is raised enabling you to, for example, open a browser or send an email. The TRichEditURL works correctly event when placed on a Panel or any other container control.
 Win prizes by sharing code!
Do you have some Delphi code you want to share? Are you interested in winning a prize for your work?
Delphi Programming Quickies Contest
 More of this Feature
• Download TRichEditURL's SOURCE code
 Join the Discussion
"Post your views, comments, questions and doubts to this article."
Discuss!
 Related Resources
• Running applications from Delphi
• Building Delphi components - tutorials
• Free source Delphi components

URL aware RichEdit
In the "Adding URL hyperlink functionality to RichEdit" article, you can find the code to implement URL highlighting in a TRichEdit. Inside the article, you'll find code to add URL hyperlink functionality to a TRichEdit component - whenever the text in a RichEdit matches the format of a URL, the control will display it as a hyperlink.

The code provided in the above article, assumes that a RichEdit control is dropped directly on a TForm object. To provide URL highlighting, the WndProc method of the TForm object is overridden in order to trap a specific message, the EN_LINK, sent from RichEdit to its parent (Form).

Does not work on a Panel?
Hyperlink aware TRichEdit Now, if the RichEdit is placed on a Panel component (for example) the code will not work?! The problem is that the message is being sent to the Panel, not the Form. Specifically, the message is sent to the RichEdit's parent. Since we are trying to catch the message from within the form (and not the RichEdit's Parent: the Panel), the message is never seen.

One solution to the problem is to subclass the Panel's WndProc method and catch the message instead of using the form's WndProc. Now, this might seem reasonable, but what if you move the RichEdit from the Panel to a GroupBox .. now you would need to mess with the GroupBox's WndProc ...

It does work on ANY parent!
The only true solution is to create a new component from TRichEdit and intercept the CN_NOTIFY message instead of WM_NOTIFY. The VCL internally translates WM_NOTIFY into CN_NOTIFY and forwards the message to the destination control so it can handle the message directly. That way we don't have to worry about which Parent is used at all.
Let's not stop here. Once we catch the message saying "a link is clicked" why not raise an event like "OnURLClick" - and send the URL string to the user ... let him decide what to do ....

TRichEditURL
The TRichEditURL (click to download full source code) component has all the functionality we are looking for built in!

Here's a sample code that creates an instance of the TRichEdit at run time and handles the OnURLClick event:

The Form's OnCreate event is used to create an instance of the TRichEditURL component and to assign the handler for the OnURLClick event:

procedure TForm2.FormCreate(Sender: TObject);
var
  s: string;
begin
  RichEditURL := TRichEditURL.Create(self);

  RichEditURL.Parent := Self; //Form1
  (*
  RichEditURL.Parent := Panel1; //another Parent
  *)
  RichEditURL.OnURLClick := RichEditUrlClick;

  s:= 'This Rich Edit is "URL smart"!' + #13#10 +
      'It event raises an event when ' +
      'a link is clicked. Try it: ' +
      ' http://www.delphi.about.com.';
  RichEditURL.Text := s
end; (*FormCreate*)

In the OnUrlClick event handling procedure we simply execute the default application - a browser if the hyperlink is an URL or a default mail client if the hyperlink specifies the "mailto:" prefix.

procedure TForm2.RichEditUrlClick
  (Sender: TObject; const URL: string);
begin
  ShellExecute(Handle, 
               'open', 
               PChar(URL), 
               nil, 
               nil, 
               SW_SHOWNORMAL);
end; (*RichEditUrlClick*)

Note: the Start from Delphi article provides many more samples of executing and running applications and files from Delphi.

Installing into a Component palette
First, download the component. The TRichEditURL comes as a single unit file (.pas extension). You'll need to add the component into an existing package. Here's "How to Install Custom Component in Delphi (into Existing Package)"

Questions? Comments? Extensions? Exceptions?!
That's it. If you find this component practical and if you extend it by adding more properties, please send your source and make it available to other developers.

Here are some ideas: RichEdit to the MAX - RichEdit tips and tricks.
As always if there are any questions or comments please post them on the Delphi Programming Forum.

Explore Delphi Programming

About.com Special Features

Delphi Programming

  1. Home
  2. Computing & Technology
  3. Delphi Programming

©2009 About.com, a part of The New York Times Company.

All rights reserved.