Creating a Custom Form


The main goal of this part of the chapter is to show how to save a form to the Object Repository and how to reuse it in other projects. To illustrate this, we are going to create a form that will fade in or slide in when displayed and fade out or slide out when closed. These effects can be easily implemented using the AnimateWindow API function.

First, you should create a new VCL Forms application project and rename the main form AnimatedForm. Then, create the TAnimationType enumeration and the AnimationType public property to enable the users to select whether they want the form to fade or slide:

type   TAnimationType = (atSlide, atFade);   TAnimatedForm = class(TForm)   private     { Private declarations }     FAnimationType: TAnimationType;   public     { Public declarations }     property AnimationType: TAnimationType       read FAnimationType write FAnimationType;   published   end; 

Next, you have to call the AnimateWindow function in the OnShow and OnClose event handlers to run the selected effect when the form is displayed and closed. AnimateWindow is a simple function that accepts three parameters: the handle of the window to be animated, the duration of the animation in milliseconds, and one of the AW_* constants that specifies the animation type:

function AnimateWindow(hWnd: HWND; dwTime: DWORD; dwFlags: DWORD): BOOL;  stdcall;

The AW_* constants that can be used in the dwFlags parameter are declared in the Windows unit. To slide the window from the center outward or to slide inward toward the center, we are going to use the AW_CENTER constant. To fade the window, we're going to use the AW_BLEND constant. To animate the window in the OnClose event when the form is closed, combine (using the or operator) one of the above constants with the AW_HIDE constant.

Since the animation messes up the display of the child controls on the form, we have to do two more things in the OnShow event handler: Call SetFocus to properly display the form in front of other forms (if the form is not the main form) and repaint all child controls. To repaint all child controls, we can use the Controls and ControlCount properties of the form in a loop that will repaint all child controls by calling their Repaint method.

The following listing displays the AnimatedForm's implementation.

Listing 25-1: The AnimatedForm

image from book
unit Unit1; interface uses   Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,   Dialogs; type   TAnimationType = (atSlide, atFade);   TAnimatedForm = class(TForm)     procedure FormClose(Sender: TObject; var Action: TCloseAction);     procedure FormShow(Sender: TObject);   private     { Private declarations }     FAnimationType: TAnimationType;   public     { Public declarations }     property AnimationType: TAnimationType       read FAnimationType write FAnimationType;   end; const   DURATION = 200; { ms } var   AnimatedForm: TAnimatedForm; implementation {$R *.dfm} procedure TAnimatedForm.FormShow(Sender: TObject); var   cnt: Integer; begin   if FAnimationType = atSlide then     AnimateWindow(Handle, DURATION, AW_CENTER)   else     AnimateWindow(Handle, DURATION, AW_BLEND);   { focus the form and repaint the form's child controls }   SetFocus;   for cnt := 0 to Pred(ControlCount) do     if Controls[cnt].Visible then Controls[cnt].Repaint; end; procedure TAnimatedForm.FormClose(Sender: TObject; var Action: TCloseAction); begin   if FAnimationType = atSlide then     AnimateWindow(Handle, DURATION, AW_CENTER or AW_HIDE)   else     AnimateWindow(Handle, DURATION, AW_BLEND or AW_HIDE); end; end.
image from book

Now that the form is finished, you should save it (only the form, using the File ® Save As command) to a dedicated object repository directory. You can either create your own or use Delphi's Object Repository (BDS\4.0\Objrepos\ DelphiWin32). Save the form as image from book Anim.pas.

Saving the form to a custom directory only makes it project independent. To reuse the form in other projects, you still have to add it to the Object Repository by right-clicking the form and selecting Add To Repository on the context menu. The Add to Repository dialog box that appears allows you to define basic repository item properties like category, name, and description.

image from book
Figure 25-1: Adding the form to the Object Repository

You can now close the current project and create a new VCL Forms application project to test the new form. After you've created the new project, select Project ® Remove from Project to display the Remove From Project dialog box, which allows us to remove forms from a project (see Figure 25-2). To remove the Form1 form from the project, select it in the Remove From Project dialog box and click OK. When you click OK to remove the form, Delphi will ask you to confirm the removal, so you'll have to click another OK button to completely remove the form from the project.

image from book
Figure 25-2: Removing a form from the project

The reason we removed the main form should be obvious: to replace it with the animated form we created earlier. To add an instance of the animated form to the project, select File ® New ® Other to display the New Items dialog box. You should be able to see the Animated Form item immediately, if you added the form to the Delphi Files category.

image from book
Figure 25-3: Creating a new animated form

Before creating a new animated form, you have to choose how to use it. There are three ways to use the animated form (and other similar Object Repository items) in a project. You can copy the form, inherit from it, or use it. Notice the three radio buttons that allow you to make this choice at the bottom of the New Items dialog box.

If you select Copy, the original animated form unit will be copied to your project. If you use this option, changes made to the form in this project will not be available in other projects, and changes made to the original animated form won't be available in the current project.

If you select Use, you will be able to use the original animated form. This is something you shouldn't do if you want to use the animated form in other projects. If you use this option, make sure the changes made to the animated form can be successfully reused in other projects.

If you select Inherit, the New Items dialog box will derive a new form from the original animated form (this is known as visual form inheritance). If you inherit the original animated form, future changes made to the animated form will be immediately available in the project (you'll only have to recompile it). This is the best choice most of the time.

So, to use the animated form in your projects, select Inherit to derive a new form from the original animated form and click OK. When you derive a new form from an Object Repository form, Delphi also adds the reference to the original form to your project (see Figure 25-4), so be careful what form you're changing until you get used to using visually inherited forms.

image from book Figure 25-4: Visually inheriting a form

Finally, the following example shows how to animate a form that covers the entire desktop (using the wsMaximized WindowState property doesn't work properly):

procedure TMainForm.FormCreate(Sender: TObject); begin   AnimationType := atFade;   SetBounds(Screen.WorkAreaLeft, Screen.WorkAreaTop,     Screen.WorkAreaWidth, Screen.WorkAreaHeight); end;

Figure 25-5 shows an animated form that uses the above code.

image from book
Figure 25-5: The animated form caught in the middle of the fade effect



Inside Delphi 2006
Inside Delphi 2006 (Wordware Delphi Developers Library)
ISBN: 1598220039
EAN: 2147483647
Year: 2004
Pages: 212
Authors: Ivan Hladni

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net