如何在Delphi表单启动期间执行操作
我有一个表单,我想在完整表单打开之前显示一个文件打开对话框 我已经发现我不能在FormShow中做与UI相关的事情,但似乎我可以在FormActivate中做(我保护它不被第二次调用…) 但是,如果用户取消了“文件打开”对话框,我想不继续关闭表单 但是,激活事件处理程序中的表单关闭会生成一个错误,我无法更改表单的可见性 那么,如何在表单启动期间执行一些与UI相关的操作,然后可能中止表单(或者我正在尝试将一个函数填充到表单中,而该函数应该是另一个表单?)如何在Delphi表单启动期间执行操作,delphi,Delphi,我有一个表单,我想在完整表单打开之前显示一个文件打开对话框 我已经发现我不能在FormShow中做与UI相关的事情,但似乎我可以在FormActivate中做(我保护它不被第二次调用…) 但是,如果用户取消了“文件打开”对话框,我想不继续关闭表单 但是,激活事件处理程序中的表单关闭会生成一个错误,我无法更改表单的可见性 那么,如何在表单启动期间执行一些与UI相关的操作,然后可能中止表单(或者我正在尝试将一个函数填充到表单中,而该函数应该是另一个表单?) TIA如果您想在表单中保持开头逻辑的独立性
TIA如果您想在表单中保持开头逻辑的独立性,您可以在表单中放置TopEndDialog,并在OnShow事件中使用如下代码:
procedure TForm2.FormShow(Sender: TObject);
begin
if OpenDialog1.Execute(Handle) then
Color := clBlue
else
PostMessage(Handle, WM_CLOSE, 0, 0); // NB: to avoid any visual glitch use AlpaBlend
end;
如果您不需要这种封装,一个更好的选择是在尝试显示表单之前检查条件,例如通过将Form2.show
调用嵌入一个先测试所有必需条件的函数中。这将是最好的选择(我认为)在创建和显示表单之前显示“文件打开”对话框。如果希望将所有代码放在一起,可以添加一个公共类过程OpenForm()或其他内容:
class procedure TForm1.OpenForm( ... );
var
O: TOpenDialog;
F: TForm1;
begin
O := TOpenDialog.Create();
try
// set O properties.
if not O.Execute then Exit
F := TForm1.Create( nil );
try
F.Filename := O.FIlename;
F.ShowModal();
finally
F.Free();
end;
finally
O.Free();
end;
end;
将变量设置为opendialog的条件,如果未正确设置标志,则在formshow事件中关闭窗体
procedure TForm1.FormCreate(Sender: TObject);
begin
ToClose := not OpenDialog1.Execute;
end;
procedure TForm1.FormShow(Sender: TObject);
begin
if ToClose then Close();
end;
或者更简单
procedure TForm1.FormShow(Sender: TObject);
begin
if not OpenDialog1.Execute then Close();
end;
两种方式。。。。
1.使用oncreate和onactivate
创建一个全局标志,甚至2个
vara初始化:布尔 在oncreate处理程序中将标志设置为false。 a初始化:=假//我们还没有执行我们的特殊代码 在onActivate里面有这样的东西
if not aInitialized then
begin
//our one time init code. special stuff or whatever
If successful
then set aInitialized := true
else aInitialized := false
end;
如何在不显示任何内容的情况下关闭它,只需将终止添加到formshow即可。当然,您需要出于某种原因进行测试才能关闭……)
在您的DPR中,您需要添加闪屏类型效果。在我的例子中,我在应用程序启动时显示进度。您也可以只显示表单并获取一些数据
来自splash.pas的代码
Procedure tsplashform.bumpit(str: string);
Begin
label2.Caption := str;
gauge1.progress := gauge1.progress + trunc(100 / items);
update;
If gauge1.progress >= items * (trunc(100 / items)) Then Close;
End;
Program Billing;
uses
Forms,
main in 'main.pas' {maindlg},
Splash in 'splash.pas' {splashform};
{$R *.RES}
Begin
Application.Initialize;
Application.Title := 'Billing Manager';
SplashForm := TSplashForm.Create(Application);
SplashForm.Show;
SplashForm.Update;
splash.items := 5;
SplashForm.bumpit('Loading Main...');
Application.CreateForm(Tmaindlg, maindlg);
SplashForm.bumpit('Loading Datamodule...');
Application.CreateForm(TfrmSingleWorkorder, frmSingleWorkorder);
SplashForm.bumpit('Loading SQL Builder...');
Application.CreateForm(TDm, Dm);
SplashForm.bumpit('Loading Security...');
Application.CreateForm(TSQLForm, SQLForm);
SplashForm.bumpit('Loading Reports...');
Application.CreateForm(Tpickrptdlg, pickrptdlg);
Application.Run;
End.
请参阅“在Delphi/C++Builder中取消/中止创建新表单?”Steve,正如OP正确指出的那样,您不能简单地从OnShow事件中调用Close。该代码是从我刚刚创建的一个小应用程序复制来测试它的。为我工作。。。德尔菲2006@François Close不会立即“发生”,因此它会起作用。
Procedure tsplashform.bumpit(str: string);
Begin
label2.Caption := str;
gauge1.progress := gauge1.progress + trunc(100 / items);
update;
If gauge1.progress >= items * (trunc(100 / items)) Then Close;
End;
Program Billing;
uses
Forms,
main in 'main.pas' {maindlg},
Splash in 'splash.pas' {splashform};
{$R *.RES}
Begin
Application.Initialize;
Application.Title := 'Billing Manager';
SplashForm := TSplashForm.Create(Application);
SplashForm.Show;
SplashForm.Update;
splash.items := 5;
SplashForm.bumpit('Loading Main...');
Application.CreateForm(Tmaindlg, maindlg);
SplashForm.bumpit('Loading Datamodule...');
Application.CreateForm(TfrmSingleWorkorder, frmSingleWorkorder);
SplashForm.bumpit('Loading SQL Builder...');
Application.CreateForm(TDm, Dm);
SplashForm.bumpit('Loading Security...');
Application.CreateForm(TSQLForm, SQLForm);
SplashForm.bumpit('Loading Reports...');
Application.CreateForm(Tpickrptdlg, pickrptdlg);
Application.Run;
End.