1. Technology

MAIL ME LINK ME...
Let us improve standard TLabel component, so that it acts like a link in a web page

Wouldn't it be marvelous if users of your applications could link to your web-site or send you e-mail from within your apps? With just a few modifications to standard TLabel component, we will have everything we need.
I was not too honest to you! I'm not going to improve TLabel component - I will not create a new Label component. Nevertheless, I am going to show you how to easily implement those two nice features.

   How and what?
On Windows 95/98/NT the registry contains settings for default http and mailto handlers, detailing which application should be used to service these requests. To take advantage of these features all that is required is to issue a ShellExecute command, passing your URL or e-mail address as the application name. The default handler will be loaded to service your request. (I won't explain how ShellExecute API call works - this is not necessary to understand).
I think that the most useful place for providing those features is About Box of your app.

Simple URL/email about box

   Prepare.
I will presume that you have something like AboutBox in your app (some form with copyright and stuff). First, we have to put two Label components on the form, one for URL address and another one for e-mail.

Before using ShellExecute API call, we need to fill in some properties of TLabels. The idea is to have whatever-you-like text in caption property, and the real URL or e-mail address in the Hint property.
Set Caption property of URL label to 'Visit my WWW page' and Hint property to 'http://delphi.about.com'. Do the same with e-mail label; Caption: 'Mail me!', Hint: 'mailto:delphi@aboutguide.com'. Obviously, you have to change those addresses to yours.

To accomplish same look and feel like in HTML page, set Cursor property to crHandPoint, Font Color to clRed (clBlue or some other) and Style to fsUnderline. Normally you have to do this both with URL label and e-mail label.

   Let's do it.
Finally, let us do some coding. Of course, don't forget to put ShellApi unit to the Uses clause of the AboutBox.

uses SHELLAPI;
procedure TAboutBox.UrlMailClick(Sender: TObject);
begin
 If (Sender is TLabel) then
  with (Sender as Tlabel) do
 ShellExecute(Application.Handle,
             PChar('open'),
             PChar(Hint), 
             PChar(0),
             nil,
             SW_NORMAL);
 end;

How this works: if Hint property is filled with e-mail address then Windows loads the user's default e-mail package in response to the 'mailto' command, ready to send the e-mail. If, in other hand, Hint is filled with web address this will load the users web browser and attempt to open the URL passed to it.

This code is written so that both Labels could share the same procedure. (Read more about this in Delphi help under "Sharing an event handler")

That's it!

If you find this approach too messy, you could write you own URL Label, or use some of the components listed below:

  • TMMJLink Label component that can display 3D text, gradient fill the background of label, and lounch your default browser in order to navigate to specified Url, or send e-mail to specified e-mail address.
  • ASLink. With this control it's easy to add www-addresses, mail-addresses etc. behind menu-options, buttons etc.
  • ©2014 About.com. All rights reserved.