问题
My Environment: C++ Builder XE4 on Windows7 pro(32bit)
I would like to select two forms automatically just after the user execute the software.
I have two forms as follows.
- FormStart : normally this shows up after program execution
- FormOther : this shows up when user specify run-time-parameter (e.g. /useOther)
When FormOther is shown, FormStart is not necessary to be shown.
I added following code in FormShow() of the TFormStart
TFormStart::FormShow(TObject *Sender)
{
if (useOther) {
FormOther->ShowModal();
this->Close();
}
}
This seems work. When user close the FormOther, FormStart shows up and immediately closes. This behavior is what I expected, so O.K.
What other way can we realize the above function?
I tried the following, and had error ("You cannot change Visible in OnShow or OnHide");
So, I gave up using the following.
TFormStart::FormShow(TObject *Sender)
{
if (userOther) {
FormOther->Show();
this->Hide();
}
}
回答1:
The first form created by Application.CreateForm
is the main form of the application, and when it's closed the application terminates.
To use a different form, you have to do so in the project (.dpr or .bpr) source instead. Use Project->View Source
from the IDE main menu to get to it.
In delphi, it would look like this:
program Project1;
uses
Forms, SysUtils,
StartForm in 'StartForm.pas' {FormStart},
OtherForm in 'OtherForm.pas' {FormOther};
{$R *.res}
begin
Application.Initialize;
Application.MainFormOnTaskbar := True;
if FindCmdLineSwitch('useOther') then
Application.CreateForm(TFormOther, FormOther)
else
Application.CreateForm(TFormStart, FormStart);
Application.Run;
end.
In c++builder, it would look like this:
#include <vcl.h>
#pragma hdrstop
#include <SysUtils.hpp>
#include <tchar.h>
//---------------------------------------------------------------------------
USEFORM("StartForm.cpp", StartForm);
USEFORM("OtherForm.cpp", OtherForm);
//---------------------------------------------------------------------------
WINAPI _tWinMain(HINSTANCE, HINSTANCE, LPTSTR, int)
{
try
{
Application->Initialize();
Application->MainFormOnTaskBar = true;
if (FindCmdLineSwitch("useOther"))
Application->CreateForm(__classid(TFormOther), &FormOther);
else
Application->CreateForm(__classid(TFormStart), &FormStart);
Application->Run();
}
catch (Exception &exception)
{
Application->ShowException(&exception);
}
catch (...)
{
try
{
throw Exception("");
}
catch (Exception &exception)
{
Application->ShowException(&exception);
}
}
return 0;
}
Note that modifying the project source can make maintenance difficult, as the IDE uses this for form information and dependencies. Sometimes changing it manually can cause issues.
来源:https://stackoverflow.com/questions/22187387/selection-of-forms-just-after-program-execution