I am using Delphi 2010 to build a Win32 GUI application running on Windows XP/Vista and Windows 7.
Basically, the Application.MainForm is a read only property and can't be changed at runtime once the first form is created via Application.CreateForm:
begin
Application.Initialize;
Application.MainFormOnTaskbar := True;
Application.CreateForm(TForm1, Form1);
Application.CreateForm(TForm2, Form2);
Application.Run;
end.
The above example will make Form1 as application's mainform. It will show on Windows 7 taskbar's thumbnail preview.
Setting Application.MainFormOnTaskBar to true at runtime allow us to enjoy Windows aero theme features.
I need to switch the application's mainform at runtime. For example, set Form2 as main form. I use following code to make it work:
procedure SetAsMainForm(aForm:TForm);
var
P:Pointer;
begin
Application.MainFormOnTaskBar := False;
try
P := @Application.Mainform;
Pointer(P^) := aForm;
aForm.Show;
finally
Application.MainFormOnTaskBar := True;
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
SetAsMainForm(Form2);
end;
Execute Button1.Click will make Form2 as mainform and update the Windows' taskbar thumbnail preview. However, the Taskbar may flicker on the switching.
My questions are:
The main form isn't something you're allowed to change in Delphi. You managed to find a way that appears to work half-way, but it's a hack. Stop doing that.
An application has only one main form, and it's the first form that finishes being created via CreateForm
. If you need two radically different behaviors for the main form, then you can try a couple of techniques:
Define your two main forms as frames. Put all your functionality there. Then define a single form that will act as the parent of one of the two frames. Instead of switching the main form, simply destroy the frame and replace it with a new one.
Similar to the frame solution, but use forms instead. Create the forms, and set the Parent
property to be the "real" main form. This will probably have a lower initial cost because you already have the two forms, but in my experience, re-parenting forms is more fragile than frames, which were designed for being child controls, so prefer the frame technique.
The flicker on the taskbar comes from one form disappearing and another appearing. With either technique above, there's always just one form, never two, so there's nothing to flicker.
Another option to consider is to set MainFormOnTaskbar=False
, then create a hidden MainForm for the lifetime of the process, and have Form1
and Form2
be secondary forms that you create and free dynamically when needed, and give them their own taskbar buttons by overriding the TForms.CreateParams()
method, eg:
procedure TForm1.CreateParams(var Params: TCreateParams);
begin
inherited;
Params.ExStyle := Params.ExStyle or WS_EX_APPWINDOW;
Params.WndParent := GetDesktopWindow;
end;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With