Delphi Clinic C++Builder Gate Training & Consultancy Delphi Notes Weblog Dr.Bob's Webshop
Dr.Bob's Delphi Notes Dr.Bob's Delphi Clinics Dr.Bob's Delphi Courseware Manuals
 Dr.Bob Examines... #4
See Also: other Dr.Bob Examines columns or Delphi articles

Delphi Control Panel Applets
As a frequent user of DIL - Delphi Information Library - I've written many add-on DIL-supporting tools, such as the Wizard to start a DIL Search from within the Delphi Code Editor. This time, I'd like to tell you about one of my latest DIL-related tools: a Control Panel Applet that implements some of the items found in the DIL Options Dialog.

There are a number of "special" applications. NT Services, Tray Icon appliations and Control Panel Applets. The later contains special applications to configure your hardware and software (other applications, such as Internet Explorer). As such, a nice example to illustrate the Delphi 5 support for Control Panel Applets would be an application that can be used to configure DIL - in other words, that shows (part of) the DIL Options dialog...

Control Panel Applets
Two new Delphi 5 wizards help us to generate the skeletons of control panel applets (both contained in the Borland Control Panel Applet Package). Just do File | New and select the Control Panel Application or Control Panel Module. The latter can be used to add additional modules to an open control panel application (i.e. one you're working on right now), so we need the former. To create our DIL Control Panel Application, we need to do File | New and select the Control Panel Application from the Object Repository:

This results in a TAppletApplication with the first AppletModule inside. TAppletApplication is actually a container (singleton class) for AppletModules. A global Application variable of type TAppletApplication is declared in the CtlPanel unit. TAppletModule encapsulates the functionality of a single Control Panel Applet, and since we only need one, we'll continue with this single AppletModule.
Let's first take a look at the properties and events that we see right away:

Properties
The properties of an AppletModule are divided into two sorts: "normal" or "resource" properties. The normal properties are AppletIcon, Caption and Help. AppletIcon specifies the icon associated with the current Applet Module that will be displayed in the Control Panel. Caption is the string that will be displayed in the Control Panel under the Applet icon. Help is the string that will be shown in the status bar of the Control Panel when the Applet is selected.
ResidIcon (mutually exclusive with AppletIcon) is the resource ID of the Applet icon. ResidInfo (mutually exclusive with Help) is the resource ID of the help string. ResidName (mutually exclusive with Caption) is the resource ID of the caption.
For the DIL Control Panel Applet, I decided to keep things as easy as possible, so I'm only using the "normal" properties here: AppletIcon (the DIL icon itself), Caption (DIL Options) and Help (UK-BUG Developer Information Library):

Events
As soon as we take a look at the events, to implement the specific behaviour of our DIL Control Panel Applet, we notice that there is no on-line help available for the events of TAppletModule. At least, not if you check out the on-line help for TAppletModule - but if you go to an individual event handler and hit Ctrl+F1 you'll see the on-line help for each event.
Obviously, the OnCreate and OnDestroy are fired when the Control Panel Applet is created. Note that these events are also fired when you open up and close the Control Panel. The actual work is done in response to the OnActivate event handler, which is fired when we double-click on the Control Panel Applet's icon in the Control Panel. OnActive has a Sender argument, which points to the Applet Module which was clicked (so we can share OnActivate handlers among multiple Applet Modules) and the Data argument is a value that can be set in the Inquire events (see on-line help for more details - we won't be needing those).
OnActivate is the event we need to listen to, and where we should create our DIL Options Dialog:

  procedure TAppletModule1.AppletModuleActivate(Sender: TObject; Data: Integer);
  begin
    with TDILOptions.Create(nil) do
    try
      ShowModal
    finally
      Free
    end
  end;
The DILOptions dialog is a Form with two buttons (one to get the current DIL Registry settings from HKEY_LOCAL_MACHINE\Software\Developer Information Library, and one to update the registry with some new values for certain keys), a memo field (to display nine registry settings) and a number of controls to be able to manipulate a subset of the DIL options. I've chosen a subset of which is was relatively easy to see what they meant (there are more DIL registry settings, and I'm sure one could in fact re-build the entire DIL Options Dialog as Control Panel Applet - I'm just showing the basics here as an example).

The implementation starts with two string consts. The first one specifies the location in the registry, the second one is an array of nine strings that hold the name of the sub-keys we're interested in:

  const
    DIL = '\Software\Developer Information Library';
  const
    RegStr: Array[1..9] of String =
      ('InstallDir',
       'MaxHistoryList',
       'MaxResults',
       'eMailClient', { 0/1 }
       'CDDILPath',
       'CDComponentPath',
       'CDComponentPath2',
       'BitMapsPath',
       'CDProductPath');
In order to read the above specified registry values, we need to create an instance of TRegIniFile (found in the Registry unit) and use ReadString to obtain the string values:
  procedure TDILOptions.Button1Click(Sender: TObject);
  var
    i: Integer;
  begin
    with TRegIniFile.Create('') do
    try
      RootKey := HKEY_LOCAL_MACHINE;
      for i:=1 to 9 do
      begin
        Memo1.Lines.Add(RegStr[i]+'='+ReadString(DIL,Regstr[i],''));
        case i of
          1: InstallDir.Text := ReadString(DIL,Regstr[i],'');
          2: MaxHistoryList.Text := ReadString(DIL,Regstr[i],'');
          3: MaxResults.Text := ReadString(DIL,Regstr[i],'');
          4: eMailClient.Checked := ReadString(DIL,Regstr[i],'') = '1';
          5: CDDILPath.Text := ReadString(DIL,Regstr[i],'');
          6: CDComponentPath.Text := ReadString(DIL,Regstr[i],'');
          7: CDComponentPath2.Text := ReadString(DIL,Regstr[i],'');
          8: BitMapsPath.Text := ReadString(DIL,Regstr[i],'');
          9: CDProductPath.Text := ReadString(DIL,Regstr[i],'');
        end
      end
    finally
      Free
    end
  end;
Note that eMailClient is stored as "1" (for True) or "0" (for False), so we need a bit extra code here, as well as in the event handler for the other button (when writing the values back to the registry):
  procedure TDILOptions.Button2Click(Sender: TObject);
  var
    i: Integer;
  begin
    with TRegIniFile.Create('') do
    try
      RootKey := HKEY_LOCAL_MACHINE;
      for i:=1 to 9 do
      begin
        case i of
          1: WriteString(DIL,Regstr[i],InstallDir.Text);
          2: WriteString(DIL,Regstr[i],MaxHistoryList.Text);
          3: WriteString(DIL,Regstr[i],MaxResults.Text);
          4: if eMailClient.Checked then
               WriteString(DIL,Regstr[i],'1')
             else WriteString(DIL,Regstr[i],'0');
          5: WriteString(DIL,Regstr[i],CDDILPath.Text);
          6: WriteString(DIL,Regstr[i],CDComponentPath.Text);
          7: WriteString(DIL,Regstr[i],CDComponentPath2.Text);
          8: WriteString(DIL,Regstr[i],BitMapsPath.Text);
          9: WriteString(DIL,Regstr[i],CDProductPath.Text)
        end;
        Memo1.Lines.Add(RegStr[i]+'='+ReadString(DIL,Regstr[i],''))
      end
    finally
      Free
    end
  end;
Although it's only a simple example, I've actually been using this Control Panel Applet a lot the past few weeks. The best thing about it is that you can set DIL Options without having to load DIL (read: without the need for DIL to be in your CD-player). Furthermore, once you move DIL from your CD player to the network or a shared CD player, you can still use it if you can set these options. And try doing that without loading DIL (you'd have to hack the registry by hand - which makes the DIL Options Control Panel Applet a whole lot easier to handle).


This webpage © 2000-2010 by Bob Swart (aka Dr.Bob - www.drbob42.com). All Rights Reserved.