程序在退出后继续运行

时间:2012-08-02 23:26:43

标签: delphi delphi-4

如果单击表单右上角的X,我的程序将继续运行。这也发生在Delphi 4中,然后我被迫进行程序重置,因为如果不这样做,它将不会重新发生。 主表单代码:

unit Unit3;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, ExtCtrls;

type
  TMainForm = class(TForm)
    NewButton: TButton;
    Button2: TButton;
    Button3: TButton;
    Button4: TButton;
    Button5: TButton;
    Button6: TButton;
    ExitButton: TButton;
    LockButton: TButton;
    SettingsButton: TButton;
    Label1: TLabel;
    TimeLabel: TLabel;
    Timer1: TTimer;
    procedure ExitButtonClick(Sender: TObject);
    procedure LockButtonClick(Sender: TObject);
    procedure SettingsButtonClick(Sender: TObject);
    procedure Timer1Timer(Sender: TObject);
    procedure NewButtonClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  MainForm: TMainForm;

implementation

uses Unit2, Unit1, Unit4;

{$R *.DFM}


procedure TMainForm.ExitButtonClick(Sender: TObject);
begin
if MessageBox(0, 'Are you sure you want to quit?', 'Exit Program?', +mb_YesNo +mb_ICONWARNING) = 6 then
Application.Terminate
else
end;

procedure TMainForm.LockButtonClick(Sender: TObject);
begin
MainForm.Hide;
Login.Show;
Login.LockLabel.Visible := true;
end;

procedure TMainForm.SettingsButtonClick(Sender: TObject);
begin
MainForm.Hide;
Settings.Show;
end;

procedure TMainForm.Timer1Timer(Sender: TObject);
begin
TimeLabel.Caption := TimeToStr(time);
end;

procedure TMainForm.NewButtonClick(Sender: TObject);
begin
TransForm.Show;
MainForm.Hide;
end;

end.

登录表格代码:

unit Unit2;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, Mask, inifiles, Unit1;

type
  TLogin = class(TForm)
    PassEdit: TMaskEdit;
    LoginButton: TButton;
    PassLabel: TLabel;
    InvisiButton: TButton;
    LockLabel: TLabel;
    procedure PassEditClick(Sender: TObject);
    procedure LoginButtonClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Login: TLogin;
  IniFile : TIniFile;
  appINI : TIniFile;
  Password : string;

implementation

uses Unit3;



{$R *.DFM}

procedure TLogin.PassEditClick(Sender: TObject);
begin
PassEdit.Text := '';
end;

procedure TLogin.LoginButtonClick(Sender: TObject);
begin
if Password = PassEdit.Text then begin
Login.Hide;
MainForm.Show;
LockLabel.Visible := false;
end
else
showmessage('Incorrect Password!')
end;
procedure TLogin.FormCreate(Sender: TObject);
begin
appINI := TIniFile.Create(ChangeFileExt(Application.ExeName,'.ini'));
Password := appINI.ReadString('Login','Password','');
appINI.Free;
end;

end.

设置表格代码

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, inifiles;

type

  TSettings = class(TForm)
    SaveButton: TButton;
    AEditA: TEdit;
    AEditB: TEdit;
    SEditB: TEdit;
    PEditB: TEdit;
    PLabelA: TLabel;
    SLabelA: TLabel;
    ALabelA: TLabel;
    PEditA: TEdit;
    SEditA: TEdit;
    BackButton: TButton;
    SettingsLabel: TLabel;
    ALabelB: TLabel;
    SLabelB: TLabel;
    PLabelB: TLabel;
    AReserveLabel: TLabel;
    BReserveLabel: TLabel;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    Label6: TLabel;
    Label8: TLabel;
    Label7: TLabel;
    procedure SaveButtonClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure BackButtonClick(Sender: TObject);
    procedure AEditAKeyPress(Sender: TObject; var Key: Char);
    procedure AEditBKeyPress(Sender: TObject; var Key: Char);
    procedure SEditAKeyPress(Sender: TObject; var Key: Char);
    procedure SEditBKeyPress(Sender: TObject; var Key: Char);
    procedure PEditAKeyPress(Sender: TObject; var Key: Char);
    procedure PEditBKeyPress(Sender: TObject; var Key: Char);
    procedure Button1Click(Sender: TObject);

  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Settings: TSettings;
  IniFile : TIniFile;
  appINI : TIniFile;
  APriceA : String;
  SPriceA : String;
  PPriceA : String;
  APriceB : String;
  SPriceB : String;
  PPriceB : String;
  change : boolean;

implementation

uses Unit3, Unit2;


{$R *.DFM}

procedure TSettings.SaveButtonClick(Sender: TObject);
//Save Button
 begin
  appINI := TIniFile.Create(ChangeFileExt(Application.ExeName,'.ini'));
  APriceA := (AEditA.Text);
  SPriceA := (SEditA.Text);
  PPriceA := (PEditA.Text);
  APriceB := (AEditB.Text);
  SPriceB := (SEditB.Text);
  PPriceB := (PEditB.Text);
  appINI.WriteString('PricesA','Adult',APriceA);
  appINI.WriteString('PricesA','Student',SPriceA);
  appINI.WriteString('PricesA','Pensioner',PPriceA);
  appINI.WriteString('PricesB','Adult',APriceB);
  appINI.WriteString('PricesB','Student',SPriceB);
  appINI.WriteString('PricesB','Pensioner',PPriceB);
  appINI.Free;
  ShowMessage('Settings Saved Successfully!');
  change := false;
 end;

procedure TSettings.FormCreate(Sender: TObject);
//Displays values as the form is created
begin
appINI := TIniFile.Create(ChangeFileExt(Application.ExeName,'.ini'));
APriceA := appINI.ReadString('PricesA','Adult','');
SPriceA := appINI.ReadString('PricesA','Student','');
PPriceA := appINI.ReadString('PricesA','Pensioner','');
APriceB := appINI.ReadString('PricesB','Adult','');
SPriceB := appINI.ReadString('PricesB','Student','');
PPriceB := appINI.ReadString('PricesB','Pensioner','');
appINI.Free;
AEditA.Text := (APriceA);
SEditA.Text := (SPriceA);
PEditA.Text := (PPriceA);
AEditB.Text := (APriceB);
SEditB.Text := (SPriceB);
PEditB.Text := (PPriceB);
end;

procedure TSettings.BackButtonClick(Sender: TObject);
//Exit Button
begin
if MessageBox(0, 'Are you sure you want to quit?', 'Exit Program?', +mb_YesNo +mb_ICONWARNING) = 6 then begin
if Change = (true) then
begin
if MessageBox(0, 'Save Changes?', 'Save Changes?', +mb_YesNo +mb_ICONWARNING) = 6 then
  begin
  appINI := TIniFile.Create(ChangeFileExt(Application.ExeName,'.ini'));
  APriceA := (AEditA.Text);
  SPriceA := (SEditA.Text);
  PPriceA := (PEditA.Text);
  APriceB := (AEditB.Text);
  SPriceB := (SEditB.Text);
  PPriceB := (PEditB.Text);
  appINI.WriteString('PricesA','Adult',APriceA);
  appINI.WriteString('PricesA','Student',SPriceA);
  appINI.WriteString('PricesA','Pensioner',PPriceA);
  appINI.WriteString('PricesB','Adult',APriceB);
  appINI.WriteString('PricesB','Student',SPriceB);
  appINI.WriteString('PricesB','Pensioner',PPriceB);
  appINI.Free;
  ShowMessage('Settings Saved Successfully!');
  Settings.Hide;
  MainForm.Show;
  change := false;
  end
else
change := false;
MainForm.Show;
Settings.Hide;
end
else
MainForm.Show;
Settings.Hide;
end
else
end;

procedure TSettings.AEditAKeyPress(Sender: TObject; var Key: Char);
var s:string;
begin
  change := true;
  s := ('1234567890.'#8); //Add chars you want to allow
  if pos(key,s) =0 then begin
    Key:=#0;
    showmessage('Only Numbers are allowed. Include cents!');
  end;
end;

procedure TSettings.AEditBKeyPress(Sender: TObject; var Key: Char);
var s:string;
begin
change := true;
  s := ('1234567890.'#8); //Add chars you want to allow
  if pos(key,s) =0 then begin
    Key:=#0;
    showmessage('Only Numbers are allowed. Include cents!');
  end;
end;

procedure TSettings.SEditAKeyPress(Sender: TObject; var Key: Char);
var s:string;
begin
change := true;
  s := ('1234567890.'#8); //Add chars you want to allow
  if pos(key,s) =0 then begin
    Key:=#0;
    showmessage('Only Numbers are allowed. Include cents!');
  end;
end;

procedure TSettings.SEditBKeyPress(Sender: TObject; var Key: Char);
var s:string;
begin
change := true;
  s := ('1234567890.'#8); //Add chars you want to allow
  if pos(key,s) =0 then begin
    Key:=#0;
    showmessage('Only Numbers are allowed. Include cents!');
  end;
end;

procedure TSettings.PEditAKeyPress(Sender: TObject; var Key: Char);
var s:string;
begin
change := true;
  s := ('1234567890.'#8); //Add chars you want to allow
  if pos(key,s) =0 then begin
    Key:=#0;
    showmessage('Only Numbers are allowed. Include cents!');
  end;
end;

procedure TSettings.PEditBKeyPress(Sender: TObject; var Key: Char);
var s:string;
begin
change := true;
  s := ('1234567890.'#8); //Add chars you want to allow
  if pos(key,s) =0 then begin
    Key:=#0;
    showmessage('Only Numbers are allowed. Include cents!');
  end;
end;
//End of Settings
procedure TSettings.Button1Click(Sender: TObject);
begin

Settings.hide;
end;

end.

项目数据:

program Project1;

uses
  Forms,
  Unit1 in 'Unit1.pas' {Settings},
  Unit2 in 'Unit2.pas' {Login},
  Unit3 in 'Unit3.pas' {MainForm},
  Unit4 in '..\Write to ini\Unit4.pas' {TransForm};


{$R *.RES}

begin
  Application.Initialize;
  Application.CreateForm(TLogin, Login);
  Application.CreateForm(TMainForm, MainForm);
  Application.CreateForm(TSettings, Settings);
  Application.Run;
end.

当我关闭它继续运行的应用程序时,你能帮我解决这个问题吗?

2 个答案:

答案 0 :(得分:8)

正如David所说,您的TLogin表单被设置为Application.MainForm,因为它是由Application.CreateForm()创建的第一个表单。您只是隐藏TLogin表单,而不是关闭它,这就是您的应用程序无法完全退出的原因。当您关闭TMainForm表单时,TLogin表单仍在运行。

根据您显示的代码,您的TMainForm表单应该是使用Application.CreateForm()创建的唯一表单。所有其他表格都应该根据需要创建。

您还编写了Unit1,Unit2和Unit3(Unit4是什么?),当它们不需要时,它们彼此相互依赖,因此您也应该删除该依赖性。 TLoginTSettings单位应为独立单位。

尝试更像这样的东西:

主要形式:

unit Unit3; 

interface 

uses 
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, 
  StdCtrls, ExtCtrls; 

type 
  TMainForm = class(TForm) 
    NewButton: TButton; 
    Button2: TButton; 
    Button3: TButton; 
    Button4: TButton; 
    Button5: TButton; 
    Button6: TButton; 
    ExitButton: TButton; 
    LockButton: TButton; 
    SettingsButton: TButton; 
    Label1: TLabel; 
    TimeLabel: TLabel; 
    Timer1: TTimer; 
    procedure FormCreate(Sender: TObject);
    procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean); 
    procedure ExitButtonClick(Sender: TObject); 
    procedure LockButtonClick(Sender: TObject); 
    procedure SettingsButtonClick(Sender: TObject); 
    procedure Timer1Timer(Sender: TObject); 
    procedure NewButtonClick(Sender: TObject); 
  protected
    procedure WndProc(var Message: TMessage); override;
  private 
    { Private declarations } 
  public 
    { Public declarations } 
  end; 

var 
  MainForm: TMainForm; 

implementation 

uses
  Unit2, Unit1, Unit4; 

{$R *.DFM} 

const
  WM_LOCK = WM_USER + 100;

procedure TMainForm.FormCreate(Sender: TObject);
begin 
  PostMessage(Handle, WM_LOCK, 0, 0);
end;

procedure TMainForm.FormCloseQuery(Sender: TObject; var CanClose: Boolean); 
begin
  if Application.MessageBox('Are you sure you want to quit?', 'Exit Program?', MB_YESNO or MB_ICONWARNING) <> IDYES then
    CanClose := False;
end;

procedure TMainForm.WndProc(var Message: TMessage);
begin
  if Message.Msg = WM_LOCK then
    LockButtonClick(nil)
  else
    inherited; 
end;

procedure TMainForm.ExitButtonClick(Sender: TObject); 
begin 
  Close;
end; 

procedure TMainForm.LockButtonClick(Sender: TObject); 
var
  Login: TLogin;
begin 
  Login := TLogin.Create(nil);
  try
    Hide; 
    Login.LockLabel.Visible := True; 
    if Login.ShowModal = mrOk then 
      Show
    else
      Application.Terminate; 
  finally
    Login.Free; 
  end;
end; 

procedure TMainForm.SettingsButtonClick(Sender: TObject); 
var
  Settings: TSettings;
begin 
  Settings := TSettings.Create(nil);
  try 
    Settings.ShowModal; 
  finally
    Settings.Free; 
  end;
end; 

procedure TMainForm.Timer1Timer(Sender: TObject); 
begin 
  TimeLabel.Caption := TimeToStr(time); 
end; 

procedure TMainForm.NewButtonClick(Sender: TObject); 
begin 
  TransForm.Show; 
  Hide; 
end; 

end. 

登录表单:

unit Unit2; 

interface 

uses 
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, 
  StdCtrls, Mask; 

type 
  TLogin = class(TForm) 
    PassEdit: TMaskEdit; 
    LoginButton: TButton; 
    PassLabel: TLabel; 
    InvisiButton: TButton; 
    LockLabel: TLabel; 
    procedure PassEditClick(Sender: TObject); 
    procedure LoginButtonClick(Sender: TObject); 
    procedure FormCreate(Sender: TObject); 
  private 
    { Private declarations } 
  public 
    { Public declarations } 
  end; 

implementation 

uses
  inifiles; 

var 
  Password : string; 

{$R *.DFM} 

procedure TLogin.FormCreate(Sender: TObject); 
var
  appINI : TIniFile; 
begin 
  appINI := TIniFile.Create(ChangeFileExt(Application.ExeName,'.ini')); 
  try
    Password := appINI.ReadString('Login','Password',''); 
  finally
    appINI.Free; 
  end;
end; 

procedure TLogin.PassEditClick(Sender: TObject); 
begin 
  PassEdit.Text := ''; 
end; 

procedure TLogin.LoginButtonClick(Sender: TObject); 
begin 
  if Password <> PassEdit.Text then
  begin 
    ShowMessage('Incorrect Password!') 
    Exit;
  end;
  LockLabel.Visible := False; 
  ModalResult = mrOk;
end; 

end. 

设置表单:

unit Unit1; 

interface 

uses 
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, 
  StdCtrls; 

type 
  TSettings = class(TForm) 
    SaveButton: TButton; 
    AEditA: TEdit; 
    AEditB: TEdit; 
    SEditB: TEdit; 
    PEditB: TEdit; 
    PLabelA: TLabel; 
    SLabelA: TLabel; 
    ALabelA: TLabel; 
    PEditA: TEdit; 
    SEditA: TEdit; 
    BackButton: TButton; 
    SettingsLabel: TLabel; 
    ALabelB: TLabel; 
    SLabelB: TLabel; 
    PLabelB: TLabel; 
    AReserveLabel: TLabel; 
    BReserveLabel: TLabel; 
    Label1: TLabel; 
    Label2: TLabel; 
    Label3: TLabel; 
    Label4: TLabel; 
    Label5: TLabel; 
    Label6: TLabel; 
    Label8: TLabel; 
    Label7: TLabel; 
    procedure SaveButtonClick(Sender: TObject); 
    procedure FormCreate(Sender: TObject); 
    procedure BackButtonClick(Sender: TObject); 
    procedure Button1Click(Sender: TObject); 
  private 
    { Private declarations } 
    function Changed: Boolean;
    function SaveSettings: Boolean;
  public 
    { Public declarations } 
  end; 

var 
  APriceA : String; 
  SPriceA : String; 
  PPriceA : String; 
  APriceB : String; 
  SPriceB : String; 
  PPriceB : String; 

implementation 

uses
  inifiles; 

{$R *.DFM} 

procedure LoadSettings;
var
  appINI: TIniFile; 
begin
  appINI := TIniFile.Create(ChangeFileExt(Application.ExeName,'.ini')); 
  try
    APriceA := appINI.ReadString('PricesA','Adult',''); 
    SPriceA := appINI.ReadString('PricesA','Student',''); 
    PPriceA := appINI.ReadString('PricesA','Pensioner',''); 
    APriceB := appINI.ReadString('PricesB','Adult',''); 
    SPriceB := appINI.ReadString('PricesB','Student',''); 
    PPriceB := appINI.ReadString('PricesB','Pensioner',''); 
  finally
    appINI.Free; 
  end;
end;

procedure TSettings.FormCreate(Sender: TObject); 
begin 
  AEditA.Text := APriceA; 
  AEditA.Modified := False; 
  SEditA.Text := SPriceA; 
  SEditA.Modified := False; 
  PEditA.Text := PPriceA; 
  PEditA.Modified := False; 
  AEditB.Text := APriceB; 
  AEditB.Modified := False; 
  SEditB.Text := SPriceB; 
  SEditB.Modified := False; 
  PEditB.Text := PPriceB; 
  PEditB.Modified := False; 
end; 

function TSettings.Changed: Boolean;
begin
  Result := AEditA.Modified or 
            SEditA.Modified or
            PEditA.Modified or
            AEditB.Modified or
            SEditB.Modified or
            PEditB.Modified; 
end;

function TSettings.SaveSettings: Boolean;
var
  dbl: Double;
begin
  Result := TryStrToFloat(AEditA.Text, dbl) and
            TryStrToFloat(SEditA.Text, dbl) and
            TryStrToFloat(PEditA.Text, dbl) and 
            TryStrToFloat(AEditB.Text, dbl) and 
            TryStrToFloat(SEditB.Text, dbl) and 
            TryStrToFloat(PEditB.Text, dbl);

  if not Result then
  begin
    ShowMessage('Only Numbers are allowed. Include cents!'); 
    Exit;
  end;

  APriceA := AEditA.Text; 
  SPriceA := SEditA.Text;
  PPriceA := PEditA.Text; 
  APriceB := AEditB.Text; 
  SPriceB := SEditB.Text; 
  PPriceB := PEditB.Text; 

  appINI := TIniFile.Create(ChangeFileExt(Application.ExeName,'.ini')); 
  try
    appINI.WriteString('PricesA','Adult',APriceA); 
    appINI.WriteString('PricesA','Student',SPriceA); 
    appINI.WriteString('PricesA','Pensioner',PPriceA); 
    appINI.WriteString('PricesB','Adult',APriceB); 
    appINI.WriteString('PricesB','Student',SPriceB); 
    appINI.WriteString('PricesB','Pensioner',PPriceB); 
  finally
    appINI.Free; 
  end;

  AEditA.Modified := False; 
  SEditA.Modified := False;
  PEditA.Modified := False; 
  AEditB.Modified := False; 
  SEditB.Modified := False; 
  PEditB.Modified := False; 

  ShowMessage('Settings Saved Successfully!');
  Result := True;
end;

procedure TSettings.SaveButtonClick(Sender: TObject); 
begin 
  SaveSettings;
end; 

procedure TSettings.BackButtonClick(Sender: TObject); 
begin 
  if Changed then 
  begin 
    if Application.MessageBox('Save Changes?', 'Save Changes?', MB_YESNO or MB_ICONWARNING) = IDYES then
    begin
      if not SaveSettings then
        Exit;
    end;
  end;
  ModalResult = mrOk; 
end; 

procedure TSettings.Button1Click(Sender: TObject); 
begin 
  Close; 
end; 

initialization
  LoadSettings;

end. 

项目:

program Project1; 

uses 
  Forms, 
  Unit1 in 'Unit1.pas' {Settings}, 
  Unit2 in 'Unit2.pas' {Login}, 
  Unit3 in 'Unit3.pas' {MainForm}, 
  Unit4 in '..\Write to ini\Unit4.pas' {TransForm}; 


{$R *.RES} 

begin 
  Application.Initialize; 
  Application.CreateForm(TMainForm, MainForm); 
  Application.ShowMainForm := False;
  Application.Run; 
end. 

答案 1 :(得分:0)

最简单的方法是使用一行代码关闭按钮:

BtnClose.click
Begin
  Application.terminate;
End;

希望有所帮助