Main
Contents
Whats New?
Articles & Tutorials
Hints and Tips
Links
Feedback
About



Inform when updated


Search this site!
 
     powered by FreeFind
Articles::Writing a URL Label


A URL Label is like a normal Delphi Label, but is written so that it looks and acts like a link in a HTML page. It looks like one due to its default color being blue and its also underlined. Whilst it acts like one by launching the users default web browser pointing to the address supplied in the label. A useful and common use for this is to provide one in an about box of an application so that your users can go straight to your web page. It can also be used to launch to the default mail program, enableing you to incorporate a simple mechanism for you users to provide feedback.
Getting Started with the new component
As we are creating a simple Label  component with an additional feature, we do not need all of the power that is provided in the standard TLabel component. This means if we descend from TCustomLabel we can easily hide any properties and methods that are not needed, but are provided in TLabel. We do not actually hide any of them but just choose not to show them. So to get started first we need to create a new component. This is done by selecting
New Component... for the Component Menu. In the dialog box enter a relevant name for the component  (I used TDCURLLabel) and set its ancestor type to TCustomLabel. Now create a new unit for the component and follow the rest of this tutorial

New Properties to be Added
The only new property that we need is the one that contains the URL address.

URL

This is the published interface to the string containing the URL to follow when the label is clicked upon.

fURL

Stores the string value in the private section of our class, this enables us to perform any needed operations when the text is changed.

We now need to define our new class.

The Definition of the New Class

  TDCURLLabel = class(TCustomLabel)
  private
    { Private declarations }
    fURL : String;
    procedure SetURL(value : string);
    procedure CMTextChanged(var Message: TMessage);Message CM_TextChanged;

  protected
    { Protected declarations }
  public
    { Public declarations }
    constructor Create (AOwner : TComponent); override;
    procedure Click; override;
  published
    { Published declarations }
    Property URL : String Read fURL write setURL;
    Property Align;
    Property Alignment;
    Property AutoSize;
    Property Color;
    Property Cursor;
    Property Caption;
    Property Font;
    Property ParentFont;
  end;
 

To summarize what we are doing here:

The private section contains the new properties and procedures needed for implementing the new
component. The create procedure is overriden in the public section, this allows us to set certain properties to their Default values when a new instance of our component is created. In the published section we have provided a new property to access and set the private URL string field. The other properties listed have already been implemented in the TCustomLabel class (or one of its ancestors) but have been hidden, by declaring them again here makes them available as they are now like any other public (or published) property. I have not redeclared all of the other possibilites due to them not being needed. If you find a use for them, just re-declare them as shown.

The Procedures

procedure
TDCURLLabel.Click;
begin
  ShellExecute(Handle, 'open', PChar(fURL), nil, nil, SW_SHOWNORMAL);
  inherited;
end;

All we are doing here is using the ShellExecute command to launch the application that is associated with the filename (which can be a URL) in our fURL string. We can also use this component to launch Notepad to view a text file, or the users mail program to send an email. I will explain how to use it in the
using the component section.

procedure TDCURLLabel.SetURL(value : string);
begin
  if fURL <> Value then
  begin
    if (Caption = '') or (Caption = fURL) then
      Caption := Value;
    fURL := Value;
  end;
end;

This procedure is used to check that the new URL is different to the one that is already set. If so the caption is changed if it is shows the URL and not another value. The internal fURL setting is then updated.


procedure TDCURLLabel.CMTextChanged(var Message: TMessage);
begin
  inherited;
  if Caption = '' then
     Caption:= fURL;
end;

By responding to the CM_TextChanged message, we are informed when ever the caption property changes. This allows us to make the Caption of the label equal to the URL string if the caption string is empty.

procedure Register;
begin
  RegisterComponents('Tutorials', [TDCURLLabel]);
end;

By registering our new component it is added to the component pallette.


The Construtor

constructor
TDCURLLabel.Create(AOwner: TComponent);
begin
  inherited ;
  Font.Color := clBlue;
  Font.Style := [fsUnderline];
  Cursor     := crHandPoint;
end;

The constructor inherits the create method for a normal TCustomLabel, and sets all the default values so that the label looks like a link, even the cursor changes to that of ahand when its passed over the link..

Installing The Component

Delphi 2

From the component menu select Install, this brings up the Install Components Dialog Button box, which shows you what is presently installed and allows you to add new components or remove them. We want to add a new component so click on Add. It now asks you for the module name, this is just the pas file that we have just created, so browse to the correct file. Click OK and you'll notice that the path to the new component has been added to the search path and that its name is in the Installed Units list box. Now click on OK to rebuild the library. You will now have a new tab on the component palette called Tutorials, with your newly created button control on it, which you can now use as you can all the other components.

Delphi 3

As Delphi 3 now supports packages (a different way of storing components which I will go into more detail on in a later article) I thought it would be a good idea to create a new package and add the new button control to it. One of the ways of doing this is to create a new package by selecting New... off the File menu, add from the dialog box select Package. After you enter a name and description for the new package (use your imagination here) it brings up the Package Editor, which shows you what the package contains and allows you to Add and Remove Components, Compile the Package and Install it. The first thing we need to do is Add our new component, so click on Add and Browse to your newly created pas file, click on OK and you will see your unit has been add in the Contains tab, all you need to do now is Compile it, and then Install it, by clicking the relevant buttons. You will find that you now have a new tab on the component palette called Tutorials, with your newly created button control on it, which you can now use as you can all the other components.
 

Using The Component

Once you have developed and installed your new compoent its time to put it to use. So either use one of your existing forms or create a new one and place the component on to the form. Now set the URL property to http://www.innotts.co.uk/~zephyr (or a URL of your choice) when the application is run and the URLLabel clicked upon you should find the default web browser is launched and it begins to load the welcome page of this site. If you wish to use the new label as a means to provide email back to you, set the URL property to: mailto:zephyr@innotts.co.uk (obviously changing the address to yours).


[Welcome] [Contents ] [Whats New?] [Articles & Tutorials][Hints & Tips] [Feedback] [Links]

Copyright 1997 Allan Carlton. Delphi is a registered trademark of Inprise Corporation. Inprise Corporation in no way endorses or is affiliated with Delphi Central. All other trademarks are the sole property of their respective owners.