2012-08-02 130 views
0

如果我单击窗体右上角的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. 

当我关闭应用IT方面运行,你能帮我解决这个问题吗?

+3

登录表单下旨是主要的你的应用程序的形式。因为这是通过调用Application.CreateForm创建的第一个。当主窗体关闭时,应用程序关闭。从来没有发生过。只为您的主表单使用CreateForm。或者致电Login.Close终止您的应用程序。 – 2012-08-02 23:42:47

+1

@大卫:我之前说过[这里](http://stackoverflow.com/a/11751291/62576),但显然马特没有阅读答案。尽管如此,我将重复它作为后代:使用Application.CreateForm创建的第一个表单被认为是应用程序的主要表单**,并且当该表单关闭时,应用程序将关闭。如果Matt提出这样一个问题,比如“我有一个TLoginForm,我希望在我的主表单之前显示,并且如果他们输入了正确的信息,允许我的应用程序运行;如果没有,我希望我的应用程序终止,我该怎么做?“ – 2012-08-03 02:50:02

回答

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

向这将是在一个关闭按钮只用一行代码的最简单的方法:

BtnClose.click 
Begin 
    Application.terminate; 
End; 

希望帮助

+0

我看不出有什么理由使用application.terminate – 2012-08-06 20:04:11

+0

我知道这并不常见,但它是确保应用程序关闭的最简单方法 – 2012-08-07 04:30:02

+0

您甚至可以将它放在最适合的表单的onclose事件处理程序上为你 – 2012-08-07 04:30:44

相关问题