再開啟第二個 form,name 改為 frmLogin,存成 loginform.pas.
此時點功能表的 Project → View Source,會看到程式碼如下:
program Project1; uses Forms, mainform in 'mainform.pas' {frmMain}, loginform in 'loginform.pas' {frmLogin}; {$R *.res} begin Application.Initialize; Application.CreateForm(TfrmMain, frmMain); Application.CreateForm(TfrmLogin, frmLogin); // 直接刪除這一行, 意即程式啟動時, 不要自動產生 frmLogin Application.Run; end.
在 TfrmLogin 的 public 加入
class function Execute : boolean;
然後在 implementation 加入對應的程式碼, 還有輸入密碼後按下 button1 檢查密碼是否輸入正確的程式碼, loginform.pas 的程式碼如下:
unit loginform; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; type TfrmLogin = class(TForm) Edit1: TEdit; Label1: TLabel; Button1: TButton; procedure Button1Click(Sender: TObject); private { Private declarations } public { Public declarations } class function Execute : boolean; // 加入這一行 end; var frmLogin: TfrmLogin; implementation {$R *.dfm} // 加入這一段 class function TfrmLogin.Execute: boolean; begin with TfrmLogin.Create(nil) do try Result := ShowModal = mrOk; finally Free; end; end; procedure TfrmLogin.Button1Click(Sender: TObject); begin if Edit1.Text = 'admin' then ModalResult := mrOK else ModalResult := mrAbort; end; end.
再回頭修改 project 的 source 如下:
program Project1; uses Forms, mainform in 'mainform.pas' {frmMain}, loginform in 'loginform.pas' {frmLogin}; {$R *.res} begin if TfrmLogin.Execute then begin Application.Initialize; Application.CreateForm(TfrmMain, frmMain); Application.Run; end else begin Application.MessageBox('密碼錯誤! 正確的密碼是 admin', '密碼錯誤'); end; end.
沒有留言:
張貼留言