|
Kanade's Delphi Stuff
Delphi Tips, Sample code and Tools
Copyright ©
1997-2004, Sanjay Kanade
| |
|
|
|
Delphi Tips: Application utility code
|
Tip 1:
Want to show a dialog on program startup, after the main form is visible?
(modified: 02 Jan 2001)
Tip 48:
Version number on About dialog, why change manually?
(modified: 19 Mar 2001)
Tip 49:
Benefits of using a constants file
(modified: 19 Mar 2001)
Tip 51:
Remembering form positions in registry
(modified: 19 Mar 2001)
Tip 52:
Showing menu hints in a status bar
(modified: 19 Mar 2001)
Tip 53:
Remembering position of the splitter
(modified: 19 Mar 2001)
|
|
|
|
1: Want to show a dialog on program startup, after the main form is visible?
|
Date added/modified: 02 Jan 2001
Suppose, you are developing a trial or shareware version and want to show a trial reminder dialog as soon as the program starts. If you show it in FormCreate of the main form, it's going to spoil the fun as your main form is still not created. What you really want is to show that dialog after your main form is visible but before any other processing is done. What do you do?
I solved this problem by using the onIdle event only once.
- Defined a doIdle event handler in my main form.
procedure DoIdle(Sender: TObject; var Done: Boolean);
- In FormCreate of the main form, set the application's onIdle event to the above procedure.
Application.OnIdle := DoIdle;
- In the beginning of the DoIdle procedure, I removed the onIdle redirection as I didn't want to use it again.
Application.OnIdle := nil;
After that, the trial reminder dialog was shown.
This method can be used for any one time processing which you want to do only once after the main form is created. I could have used OnShow but that may be used more than once in an application if the main form is hidden and shown several times. If you know of another way to do it which I might have missed, please send me a message.
To demonstrate this and many other solutions, I have created a complete sample application in another product, Kanade's Appmodels. For more details, please see the sample code section under this web site.
|
|
|
|
48: Version number on About dialog, why change manually?
|
Date added/modified: 19 Mar 2001
If you are in the habit of manually changing Version number in Project Options and also on the About dialog of your application, consider using a utility routine so that you only change it in one place, in project options. My Application Models freeware has a sample that you can use. There is a getMyFileVersion routine in its Utils unit that is used in About box. You can customize this routine to display the version number the way you want to. The sample code gives some comments.
|
|
|
|
49: Benefits of using a constants file
|
Date added/modified: 19 Mar 2001
My Application Models freeware uses constants in a Params.pas file to keep such stuff as program name, its short description, copyright statement, web site URL, support email, version display prefix, help file name, etc. The about box and other code uses constants from this file to display labels or messages. There are many advantages to this approach:
- All such constants are in one place and makes it easy to change any information.
- When making another application, it is easy to just reuse the code and only change the above constants.
- If you are considering private encryption of certain strings, it becomes easier to keep the encrypted stuff in the same place.
- If you have an evaluation and licensed version, you can use {$ifdef} feature of Delphi to put both types of constants in the same file, one set for licensed version, another for evaluation version.
This is an example of a case where the old programming techniques are still useful. For example, someone might say. "Why not create a component and let these constants be changeable properties?" But, the above approach is easier and gives you more power if you want to use {$ifdefs} for multiple versions in future.
Using the same argument, it also makes sense to put application globals in a single file and include the above file there. My Application Models uses a file commdefs.pas to do that.
|
|
|
|
51: Remembering form positions in registry
|
Date added/modified: 19 Mar 2001
If you want to remember form positions in registry so that you can restore forms next time to the same positions, read this. It is not enough to just remember Left, Top, Width, Height. If the window is maximized, the above properties give maximized values which is not right. What you want to store are actual non-maximized values. My Application Models freeware gives sample code to do that.
|
|
|
|
52: Showing menu hints in a status bar
|
Date added/modified: 19 Mar 2001
You would already know that the menu hints never appear in popup windows as the hints for controls do. Then, where are these hints displayed? They should be displayed in a status bar at the bottom using the OnHint handler. My Application Models freeware shows how to do that.
|
|
|
|
53: Remembering position of the splitter
|
Date added/modified: 19 Mar 2001
If your application uses splitter component and you want to remember user preferences so that next time the user need not readjust the panes, you can do it. However, it is not too easy as you have to account for maximized positions where the splitter position may be unrealistic (not actual). This is similar to the tip "remembering form positions." My Application Models freeware has sample code to show how to do that.
|
|
|
|
Copyright 1995-2004, Sanjay Kanade. All rights reserved. All trademarks and copyrights belong to their respective owners.
|
|
|
|