DPR文件
---------------------------------------
program accntbook;
uses
ExceptionLog,
SysUtils,
Windows,
Forms,
UMainForm in 'UMainForm.pas' {MainForm},
UDataModule in 'UDataModule.pas' {MainDataModule: TDataModule},
UGlobal in 'UGlobal.pas',
UBrowserData in 'UBrowserData.pas' {BrowserData},
UInputForm in 'UInputForm.pas' {InputForm},
UPassword in 'UPassword.pas' {PasswordForm},
UQueryWizard in 'UQueryWizard.pas' {QueryWizard},
UQueryResult in 'UQueryResult.pas' {QueryResult};
{$R *.res}
var
I: Integer;
HMutex, AnotherWindow: HWND;
PasswordRight: Boolean = False;
begin
Application.Initialize;
HMutex := CreateMutex(nil, False, ApplicationMutexName);
try
if GetLastError = ERROR_ALREADY_EXISTS then
begin
MessageBox(Application.Handle, '管理系统正在运行中,请不要重复运行本程序。',
'提示', MB_ICONWARNING);
AnotherWindow := FindWindow('TMainForm',
PChar(TMainForm.GetApplicationName));
if AnotherWindow <> 0 then
begin
SetForegroundWindow(AnotherWindow);
SetActiveWindow(AnotherWindow);
end;
Exit;
end;
for I := 1 to 3 do
case TPasswordForm.InputPassword of
prWrong:
MessageBox(Application.Handle, '您输入的密码不正确!无权登录管理系统!',
'警告', MB_ICONWARNING);
prCancel: Break;
prRight:
begin
PasswordRight := True;
Break;
end;
end;
if not PasswordRight then Exit;
Application.CreateForm(TMainDataModule, MainDataModule);
Application.CreateForm(TMainForm, MainForm);
Application.Run;
finally
ReleaseMutex(HMutex);
CloseHandle(HMutex);
end;
end.
----------------------------------------------------
UNIT文件(主要就是TPasswordForm单元)
----------------------------------------------------
unit UPassword;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtCtrls, StdCtrls, RzCommon;
type
TPasswordAction = (paCheck, paModify);
TPasswordResult = (prRight, prWrong, prModified, prCancel);
TPasswordForm = class(TForm)
pnlPassord: TPanel;
pnlNewPassword: TPanel;
pnlButtons: TPanel;
btnOK: TButton;
btnCancel: TButton;
edtCurrentPassword: TLabeledEdit;
Panel1: TPanel;
Image1: TImage;
edtNewPassword: TLabeledEdit;
IniFile: TRzRegIniFile;
procedure edtNewPasswordChange(Sender: TObject);
private
{ Private declarations }
FAction: TPasswordAction;
procedure SetAction(Value: TPasswordAction);
function CheckPassword: Boolean;
procedure SaveNewPassword;
public
{ Public declarations }
class function InputPassword(const Action: TPasswordAction = paCheck):
TPasswordResult;
property Action: TPasswordAction read FAction write SetAction;
end;
implementation
{$R *.dfm}
uses Des, UGlobal;
{ TPasswordForm }
function TPasswordForm.CheckPassword: Boolean;
var
SavedPassword : string;
begin
// Result := False;
SavedPassword := IniFile.ReadString('Admin',
'Password', '');
if SavedPassword <> '' then
Result := edtCurrentPassword.Text = DESryStrHex(SavedPassword,
ApplicationName)
else
Result := edtCurrentPassword.Text = 'AdminIsMe';
end;
class function TPasswordForm.InputPassword(
const Action: TPasswordAction = paCheck): TPasswordResult;
var
PasswordForm : TPasswordForm;
PasswordResult : Boolean;
begin
Result := prCancel;
PasswordForm := TPasswordForm.Create(nil);
try
PasswordForm.Action := Action;
if PasswordForm.ShowModal = mrOK then
begin
PasswordResult := PasswordForm.CheckPassword;
if not PasswordResult then
Result := prWrong
else if Action = paModify then
begin
PasswordForm.SaveNewPassword;
Result := prModified;
end
else
Result := prRight;
end;
finally
PasswordForm.Free;
end;
end;
procedure TPasswordForm.SaveNewPassword;
begin
IniFile.WriteString('Admin', 'Password',
EncryStrHex(edtNewPassword.Text, ApplicationName));
end;
procedure TPasswordForm.SetAction(Value: TPasswordAction);
begin
FAction := Value;
pnlNewPassword.Visible := Value = paModify;
if not pnlNewPassword.Visible then
begin
Height := Height - pnlNewPassword.Height;
btnOK.Enabled := True;
end
else
begin
//Height := Height + pnlNewPassword.Height;
btnOk.Enabled := False;
end;
end;
procedure TPasswordForm.edtNewPasswordChange(Sender: TObject);
begin
btnOK.Enabled := edtNewPassword.Text <> '';
end;
end.