📄 jvloginform.pas
字号:
begin
FSaveOnRestore := Application.OnRestore;
Application.Minimize;
{$IFDEF VCL}
Application.HookMainWindow(UnlockHook);
{$ENDIF VCL}
FLocked := True;
end;
procedure TJvCustomLogin.TerminateApplication;
begin
with Application do
begin
ShowMainForm := False;
{$IFDEF VCL}
if Handle <> 0 then
ShowOwnedPopups(Handle, False);
{$ENDIF VCL}
Terminate;
end;
CallTerminateProcs;
end;
procedure TJvCustomLogin.UnlockOkClick(Sender: TObject);
var
Ok: Boolean;
begin
with TJvLoginForm(Sender) do
begin
Ok := False;
try
Ok := CheckUnlock(UserNameEdit.Text, PasswordEdit.Text);
except
Application.HandleException(Self);
end;
if Ok then
ModalResult := mrOk
else
ModalResult := mrCancel;
end;
end;
function TJvCustomLogin.CheckUnlock(const UserName, Password: string): Boolean;
begin
Result := True;
if Assigned(FOnUnlockApp) then
FOnUnlockApp(Self, UserName, Password, Result)
else
if Assigned(FOnUnlock) then
Result := FOnUnlock(Password);
end;
function TJvCustomLogin.CreateLoginForm(UnlockMode: Boolean): TJvLoginForm;
begin
Result := TJvLoginForm.Create(Application);
Result.Caption := FCaption;
with Result do
begin
FUnlockMode := UnlockMode;
if FUnlockMode then
begin
FormStyle := fsNormal;
FSelectDatabase := False;
end
else
FormStyle := fsStayOnTop;
if Assigned(Self.FOnIconDblClick) then
begin
with AppIcon do
begin
OnDblClick := DoIconDblClick;
Cursor := crHand;
end;
with KeyImage do
begin
OnDblClick := DoIconDblClick;
Cursor := crHand;
end;
end;
PasswordEdit.MaxLength := FMaxPasswordLen;
{$IFDEF VCL}
PasswordEdit.PasswordChar := PasswordChar;
{$ENDIF VCL}
AttemptNumber := Self.AttemptNumber;
end;
end;
function TJvCustomLogin.DoUnlockDialog: Boolean;
begin
with CreateLoginForm(True) do
try
OnFormShow := nil;
OnOkClick := UnlockOkClick;
with UserNameEdit do
begin
Text := LoggedUser;
ReadOnly := True;
Font.Color := clGrayText;
end;
Result := ShowModal = mrOk;
finally
Free;
end;
end;
{$IFDEF VCL}
function TJvCustomLogin.UnlockHook(var Msg: TMessage): Boolean;
function DoUnlock: Boolean;
var
Popup: HWND;
begin
with Application do
if IsWindowVisible(Handle) and IsWindowEnabled(Handle) then
SetForegroundWindow(Handle);
if FUnlockDlgShowing then
begin
Popup := GetLastActivePopup(Application.Handle);
if (Popup <> 0) and IsWindowVisible(Popup) and
(WindowClassName(Popup) = TJvLoginForm.ClassName) then
SetForegroundWindow(Popup);
Result := False;
Exit;
end;
FUnlockDlgShowing := True;
try
Result := DoUnlockDialog;
finally
FUnlockDlgShowing := False;
end;
if Result then
begin
Application.UnhookMainWindow(UnlockHook);
FLocked := False;
end;
end;
begin
Result := False;
if not FLocked then
Exit;
with Msg do
begin
case Msg of
WM_QUERYOPEN:
UnlockHook := not DoUnlock;
WM_SHOWWINDOW:
if WParam <> 0 then
UnlockHook := not DoUnlock;
WM_SYSCOMMAND:
if ((WParam and $FFF0) = SC_RESTORE) or ((WParam and $FFF0) = SC_ZOOM) then
UnlockHook := not DoUnlock;
end;
end;
end;
{$ENDIF VCL}
//=== { TJvLoginDialog } =====================================================
procedure TJvLoginDialog.Loaded;
var
Loading: Boolean;
begin
Loading := csLoading in ComponentState;
inherited Loaded;
if not (csDesigning in ComponentState) and Loading then
if Active and not Login then
TerminateApplication;
end;
procedure TJvLoginDialog.OkButtonClick(Sender: TObject);
var
SetCursor: Boolean;
begin
with TJvLoginForm(Sender) do
begin
SetCursor := GetCurrentThreadID = MainThreadID;
try
if SetCursor then
Screen.Cursor := crHourGlass;
try
if DoCheckUser(UserNameEdit.Text, PasswordEdit.Text) then
ModalResult := mrOk
else
ModalResult := mrNone;
finally
if SetCursor then
Screen.Cursor := crDefault;
end;
except
Application.HandleException(Self);
end;
end;
end;
function TJvLoginDialog.DoCheckUser(const UserName, Password: string): Boolean;
begin
Result := True;
if Assigned(FOnCheckUser) then
FOnCheckUser(Self, UserName, Password, Result);
end;
procedure TJvLoginDialog.WriteUserName(const UserName: string);
begin
if Assigned(AppStorage) then
AppStorage.WriteString(AppStorage.ConcatPaths([AppStoragePath, RsLastLoginUserName]), UserName);
end;
function TJvLoginDialog.ReadUserName(const UserName: string): string;
begin
if Assigned(AppStorage) then
Result := AppStorage.ReadString(AppStorage.ConcatPaths([AppStoragePath, RsLastLoginUserName]), UserName)
else
Result := UserName;
end;
function TJvLoginDialog.DoLogin(var UserName: string): Boolean;
begin
try
with CreateLoginForm(False) do
try
OnOkClick := Self.OkButtonClick;
UserName := ReadUserName(UserName);
UserNameEdit.Text := UserName;
Result := (ShowModal = mrOk);
if Result then
begin
UserName := UserNameEdit.Text;
WriteUserName(UserName);
end;
finally
Free;
end;
except
Application.HandleException(Self);
Result := False;
end;
end;
//=== { TJvLoginForm } =======================================================
procedure TJvLoginForm.FormCreate(Sender: TObject);
begin
Icon := Application.Icon;
{$IFDEF VCL}
if Icon.Empty then
Icon.Handle := LoadIcon(0, IDI_APPLICATION);
{$ENDIF VCL}
AppIcon.Picture.Assign(Icon);
AppTitleLabel.Caption := Format(RsAppTitleLabel, [Application.Title]);
PasswordLabel.Caption := RsPasswordLabel;
UserNameLabel.Caption := RsUserNameLabel;
OkBtn.Caption := SOKButton;
CancelBtn.Caption := SCancelButton;
end;
procedure TJvLoginForm.OkBtnClick(Sender: TObject);
begin
Inc(FAttempt);
if Assigned(FOnOkClick) then
FOnOkClick(Self)
else
ModalResult := mrOk;
if (ModalResult <> mrOk) and (FAttempt >= AttemptNumber) then
ModalResult := mrCancel;
end;
procedure TJvLoginForm.FormShow(Sender: TObject);
var
I: Integer;
S: string;
begin
if FSelectDatabase then
begin
ClientHeight := CustomCombo.Top + PasswordEdit.Top - UserNameEdit.Top;
S := RsDatabaseName;
I := Pos(':', S);
if I = 0 then
I := Length(S);
CustomLabel.Caption := '&' + Copy(S, 1, I);
end
else
begin
ClientHeight := PasswordEdit.Top + PasswordEdit.Top - UserNameEdit.Top;
CustomLabel.Visible := False;
CustomCombo.Visible := False;
end;
if not FUnlockMode then
begin
HintLabel.Caption := RsHintLabel;
if Caption = '' then
Caption := RsRegistrationCaption;
end
else
begin
HintLabel.Caption := RsUnlockHint;
if Caption = '' then
Caption := RsUnlockCaption;
end;
if (UserNameEdit.Text = '') and not FUnlockMode then
ActiveControl := UserNameEdit
else
ActiveControl := PasswordEdit;
if Assigned(FOnFormShow) then
FOnFormShow(Self);
FAttempt := 0;
end;
function TJvCustomLogin.Execute: Boolean;
begin
Result := Login;
end;
{$IFDEF UNITVERSIONING}
initialization
RegisterUnitVersion(HInstance, UnitVersioning);
finalization
UnregisterUnitVersion(HInstance);
{$ENDIF UNITVERSIONING}
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -