As David Heffernan has already said, itโs impossible to change the basic form of an already running application. This is a limitation of the windows themselves.
What you can do is cheat and never change the basic shape, but just make it look like you. How do you achieve this?
Step 1: Add code to the second form to create your own taskbar button
procedure TWorkForm.CreateParams(var Params: TCreateParams); begin inherited; Params.ExStyle := Params.ExStyle or WS_EX_APPWINDOW; end;
Step 2: Dynamically create a second form just before switching it. After creating it, the previously added code will create a new taskbar button for your second form.
Step 3: Now hide the real base shape. Hiding will also hide his taskbar button. That way, you still show one taskbar button, and it belongs to your second form.
Step 4:. To have your second form terminate your application when it closes, call the Close your true main form method from your second Forms OnClose or OnFormCloseQuery event.
If you want to return to the true call to the main form, select the "My main form" method instead of the "Close" method.
This approach allows us to quickly change forms, so only the sharpest users will notice a short animation of the "Taskbar" button.
NOTE. If your second option is complex, and because of this it takes some time to create, you can create it hidden, and then, as soon as its creation process is completed, show it and exchange it. Otherwise, you may end up showing two taskbar buttons that I think you want to avoid.
Here is a quick example:
- LoginForm is the real basic form created when the application was launched - WorkForm is the form by which the user will spend most of the time after logging in, and this file is created during the login process.
Login Form Code:
unit ULoginForm; interface uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls; type TLoginForm = class(TForm) BLogIn: TButton; procedure BLogInClick(Sender: TObject); private { Private declarations } public { Public declarations } end; var LoginForm: TLoginForm; //Global variable to tell us if we are only logging out or closing our program LoggingOut: Boolean; implementation uses Unit2; {$R *.dfm} procedure TLoginForm.BLogInClick(Sender: TObject); begin //Create second Form Application.CreateForm(TWorkForm, WorkForm); //Hide Main Form Self.Hide; //Don't forget to clear login fields end; end.
Work form code:
unit UWorkForm; interface uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls; type TWorkForm = class(TForm) BLogOut: TButton; //Used in overriding forms creating parameters so we can add its own Taskbar button procedure CreateParams(var Params: TCreateParams); override; procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean); procedure BLogOutClick(Sender: TObject); private { Private declarations } public { Public declarations } end; var WorkForm: TWorkForm; implementation uses Unit1; {$R *.dfm} procedure TWorkForm.BLogOutClick(Sender: TObject); begin //Set to true so we know we are in the process of simply logging out LoggingOut := True; //Call close method to begin closing the current Form Close; end; procedure TWorkForm.CreateParams(var Params: TCreateParams); begin inherited; Params.ExStyle := Params.ExStyle or WS_EX_APPWINDOW; end; procedure TWorkForm.FormCloseQuery(Sender: TObject; var CanClose: Boolean); begin //Check to see if we are in the process of simply logging out if not LoggingOut then begin //If we are not in the process of logging out close the Main Form LoginForm.Close; //and then allow closing of current form CanClose := True; end else begin //But if we are in the process of simply logging out show the Main Form LoginForm.Show; //Reset the LoggingOut to false LoggingOut := False; //and then alow closing of current form CanClose := True; end; end; end.