Get Your Delphi Apps off to a Fast Start by Clay Shannon

By: Clay Shannon

Abstract: Explains how and why you should create a template project. This can save you a lot of time whenever you begin a new Delphi project, as the normal "setup work" you do will already be done--setting properties, adding forms and units, etc.

How many times have you started a new project in Delphi, and the first thing you did was set a few properties on the main form-to the values you always want those properties set and you wish were the default values, then quickly added some event handler code by memory that is the same (tried and true) in all your apps? You might even add an About box and a Splash screen, resizing them, dropping on a component or two.

Isn't this tedious? Of course. Is there a better way? Of course. There's no need to reinvent the project and form settings every time you begin a new Delphi application. You can go from RAD to radder (raddest is an exercise left for the reader) by spending a few minutes in advance to create a skeleton project with all the settings as you like them, and adding code that is common to all, or at least most, of your projects. Alternatively, you can download this file from CodeCentral: http://codecentral.borland.com/codecentral/ccWeb.exe/listing?id=19710 and save yourself even more time. Although the exact steps and code will differ, users of other tools can also adapt this technique to their own environment.

DISCLAIMER/CAVEAT/CYA NOTICE

Yes, I realize that Delphi comes factory-equipped with an Application Wizard, but although it's nice for certain situations, it doesn't quite set the project up to my satisfaction (see below for details on how I like an application to begin its life). Moreover, creating your own (or using my) project template is even faster than using the Application Wizard, once you've got it set up (which only takes a few minutes).

0-90 IN FOUR MOUSE CLICKS

There are certain properties that I like to have set different than the default, and certain code that I always (or almost always) want to have added to my projects and forms. So I created a project and saved it in my Project Repository with these settings set and this code coded.

To be specific, these are the things that I like to have in my Delphi projects that I used to have to add manually each time at the beginning of each project:

  • Change the form's Position property from poDesigned to poScreenCenter.
  • Assign values to the Constraint properties so the user can't resize the form (shrinking it to Rhode-Island scale makes it useless, and stretching it to Texas-sized is just plain ugly). To accomplish this, I set MinHeight and MaxHeight := Height, and MinWidth and MaxWidth := Width.
  • Use a color other than boring old clGray, so I add an OnPaint event handler to take care of this if the user's machine is displaying more than 256 colors, using one of the primary colors in a gradient effect.
  • Add menu items that practically every app needs, such as File | Exit, Help | Help, and Help | About. I also add the corresponding OnClick code for these menu items (close the main form, which closes the app; create and show the About box; open the help file).
  • Release the Splash form in the main form's OnShow event (after creating it in the Splash form's own initialization unit).
  • Create a log file to write error messages to a text file. This can be a useful debugging tool, both pre- and post-release.
  • Add code to make the forms size appropriately regardless of the resolution the user may be using (it's also necessary to have your development machine set to use Small Fonts).
  • A <AppName>Const unit, in which to place shared constants, such as resourcestrings.

Realizing, though, that not all coders will want the same settings that I find handy, I added Boolean properties that you can set in the main form's OnCreate event based on what your preferences are. These properties allow "one-stop shopping". Simply navigate your way to the main form's OnCreate event (2-clicking the form is the easiest way), and change the properties to your heart's content. The Form1Create event looks like this:

procedure TForm1.FormCreate(Sender: TObject);
begin
 { set it to False if you don't want a log file }
  Cre8LogFile := True;
  { set it to True if you want to allow the user to resize the form }
  AllowResizing := False;
  { If you want the user to see the Splash screen regardless of how fast your
    form loads, set this to True. You can adjust how long it will show by
    modifying the argument passed to Sleep() in the FormShow event. }
  ShowSplashRegardless := True;
 { For the sake of simplicity in the FormPaint event handling code, you can
    only assign clRed, clGreen, clBlue, clPurple, clTeal, and clOlive to the
    MainFormColor property. If you assign something else (it must be a valid
    color, though, or a hex value), the form's color will be a gradient gray. }
  MainFormColor := clTeal;
  { Replace this with the name of your own help file }
  HelpFile := 'windows.hlp';
  
  InitializeForm;
end;

MAKE IT AVAILABLE FOR LATER

If you have downloaded the "Starting Point" project from Code Central, or if you have created your own default Delphi project template, you need to add it to Delphi's Object Repository. Doing this is about as simple as could be: With the project loaded into Delphi, select Project | Add to Repository... The following dialog appears:

Give it a Title, Description, select the Page on which you want it to appear (such as "Projects"), enter whatever you want for "Author" (Clay Shannon, W. Somerset Maugham, or perhaps your own name), and select an icon that will represent the project. The dialog box will now appear something like this:

This information will allow you to recognize this project for what it is when you subsequently select File | New | Other | <The Page You Designated>). Make sure the Copy radio button is selected if you don't want subsequent changes to the project template to propagate to your current project. If you do want such changes to propagate, select the Inherit radio button.

If you later realize there are other properties you want to set or add, code you want to include or modify, select the Use radio button. This will change the template itself. Be sure to check it back into the Object Repository (close the project, and then open it via the Object Repository again, this time using the Copy option) before adding any project-specific code.

CONCLUSION

With all the time you will save using this technique, you will finally have the opportunity to do the things you've always wanted to do: learn to play the piano, walk the Pacific Crest Trail, get in touch with your inner...tube?, or simply move on more quickly to the next item on your to-do list.

 

Clay Shannon is a Borland and PDA-certified Delphi 5 developer and the author of "Tomes of Delphi: Developer's Guide to Troubleshooting" (Wordware, 2001) as well as the novel he claims is the strangest one ever written, "the Wacky Misadventures of Warble McGorkle" (see http://www.winsite.com/bin/Info?12500000036639 for more information on the 4 Novels application, which contains this and three other novels he has penned).

You can find out more about Clay at: http://hometown.aol.com/bclayshannon/myhomepage/index.html
You can look into Clay's shareware and determine his current availability at:
http://hometown.aol.com/bclayshannon/myhomepage/business.html
You can contact him at:
BClayShannon@aol.com


Server Response from: BDN10A

 
Copyright© 1994 - 2008 Embarcadero Technologies, Inc. All rights reserved. Contact Us   Site Map   Legal Notices   Privacy Policy   Report Software Piracy