Delphi中的事情如何形成启动

时间:2010-01-16 01:26:22

标签: delphi

我有一个表单,我希望在完整表单打开之前显示一个文件打开对话框。

我已经发现我不能在FormShow中做UI相关的东西,但似乎我可以在FormActivate中(我保护它不被第二次调用......)

但是,如果用户取消文件打开对话框,我想关闭表单而不继续。

但是,激活事件处理程序中的表单关闭会生成一个错误,我无法更改表单的可见性。

那么如何在表单启动时执行一些与UI相关的操作,然后可能会中止表单(或者我是否尝试将函数填充到应该采用另一种形式的表单中?)

TIA

4 个答案:

答案 0 :(得分:3)

最好(我认为)在创建和显示表单之前显示文件打开对话框。如果要将所有代码保持在一起,可以添加公共类过程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;

答案 1 :(得分:1)

将变量设置为opendialog的条件,如果未正确设置标志,则关闭formhow事件上的表单。

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;

答案 2 :(得分:0)

如果你想让逻辑条件保持在表单中自包含的开头,你可以在你的表单中放置一个TOpenDialog并在你的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;

如果您不需要此封装,更好的选择是检查之前尝试显示表单的条件,例如嵌入{{1调用一个首先测试所有必需条件的函数。

答案 3 :(得分:0)

两种方式...... 1.使用oncreate和onactivate

创建一个全球旗帜甚至2  VAR
   aInitialized:布尔;

在oncreate处理程序中将标志设置为false。 aInitialized:= false; //我们还没有执行特殊代码。

在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;

如何关闭它而不显示任何内容只是将你的终止添加到formhow。当然你需要为某种原因进行测试才能关闭.. :)

Procedure Tmaindlg.FormShow(Sender: TObject);
Begin
  If (shareware1.Sharestatus = ssExpired) or (shareware1.Sharestatus = ssTampered) Then
    application.Terminate;
End;

在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.