寻找完美的登陆窗口解决方案!大家来讨论下! ( 积分: 50 )

  • 主题发起人 主题发起人 lsjkiki
  • 开始时间 开始时间
L

lsjkiki

Unregistered / Unconfirmed
GUEST, unregistred user!
login为--Normal登陆窗体(动态创建窗体)
main为--MDI主窗体
就是要先显示登陆 ,登陆成功就显示mian-MDI主窗体。
----------------大家来讨论下吧。
我的错误是:就是从任务栏点两下(先最小化再最大化)它就有错误,或者直接把主窗体也也显示出来了,我还没有登陆呢。
--------错误的解决方案(我的)
1:在program工程文件:
Application.CreateForm(TMain, Main);
Login := TLogin.Create(Application);
Login.ShowModal;
---------错误结果:从任务栏点两下(先最小化再最大化)就直接把主窗体也也显示出来了,我还没有登陆.
2:在main窗体的Create事件里写
Application.CreateForm(TMain, Main);
Login := TLogin.Create(Application);
Login.ShowModal;
------错误结果:因为在uses有login 所以在free时候有错误。hide也有其他问题,也想过用hide,所以不。

=========我看了很多贴子,还是没有找到的好的方案。有的方案可以解决 ,不过还会闪下才关闭,烦哦。大家遇过这个问题吗? !


-----------------写这么做字也好辛苦 ,大家就都阐述下自己看法,最好是贴出解决方法!!感激不尽!
 
在main窗体的onshow事件中写:
application.ShowMainForm := false;
Login := TLogin.Create(self);
Login.ShowModal;
登录成功
application.ShowMainForm := true;
登录失败
application.terminate;
 
在工程文件中写:
Application.Initialize;
if TLogin.Login then begin //用户成功登录,Login为TLogin类的一个类方法
Application.CreateForm(TMain, Main);
Application.Run;
end
else begin
Application.Terminate;
end;
 
可是在main窗口中有我要在logon中用的变量和控件啊~。
我没有做DM。
 
这表示你当初的设计不够好,登录窗体和主窗体的耦合过于紧密,需要调整。
 
program Project1;

uses
Forms,
controls,
Unit1 in 'Unit1.pas' {Form1},
Unit2 in 'Unit2.pas' {Form2};

{$R *.res}
var
LogonForm:TForm2;
begin
Application.Initialize;
LogonForm:=TForm2.Create(application);
if LogonForm.ShowModal<>mrok then halt;
Application.CreateForm(TForm1, Form1);
Application.Run;
end.
 
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Menus, ComCtrls, ToolWin, ImgList, jpeg, ExtCtrls;

type
TForm1 = class(TForm)
MainMenu1: TMainMenu;
N1: TMenuItem;
N2: TMenuItem;
N3: TMenuItem;
N4: TMenuItem;
N5: TMenuItem;
N6: TMenuItem;
N7: TMenuItem;
N8: TMenuItem;
N9: TMenuItem;
CoolBar1: TCoolBar;
ToolBar1: TToolBar;
ToolButton1: TToolButton;
ToolButton2: TToolButton;
ToolButton3: TToolButton;
ToolButton4: TToolButton;
ToolButton5: TToolButton;
ToolButton6: TToolButton;
ToolButton7: TToolButton;
ImageList1: TImageList;
Image1: TImage;
ToolButton8: TToolButton;
ToolButton9: TToolButton;
procedure ToolButton7Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure ToolButton8Click(Sender: TObject);
private
{ Private declarations }
procedure FormInit;
function DoLogin:Integer;
protected
procedure ShowLogin(var Message: TMessage); message WM_USER + 1024;
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation
uses unit2;

{$R *.dfm}

function TForm1.DoLogin:Integer;
var
dlg: Tfrm_login;
begin
dlg := Tfrm_login.Create(Self);
Result:=dlg.ShowModal;
FreeAndNil(dlg);
end;

procedure TForm1.ShowLogin(var Message: TMessage);
begin
Application.ProcessMessages;
DoLogin;
end;


procedure TForm1.FormInit;
begin
end;

procedure TForm1.ToolButton7Click(Sender: TObject);
begin
if Application.MessageBox('您确定要退出系统吗?','信息',mb_IconQuestion+mb_YesNo)=IDYES Then
Application.Terminate;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
FormInit;
//这一句也可以写在Application.Run;之前,更好一些
//像这样 PostMessage(Application.MainFrom.Handle, WM_USER + 1024, 0, 0);
PostMessage(Handle, WM_USER + 1024, 0, 0);
end;

procedure TForm1.ToolButton8Click(Sender: TObject);
begin
if Application.MessageBox('您确定要重新登陆系统吗?','信息',mb_IconQuestion+mb_YesNo)=IDYES Then
begin
PostMessage(Handle, WM_USER + 1024, 0, 0);
end;
end;

end.
 
继续学习,是我当初设计时模块耦合高了!
 
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.
 
program Project1;

uses
Forms,
Controls,
Unit1 in 'Unit1.pas' {frmMain},
Unit2 in 'Unit2.pas' {frmLogin};

{$R *.res}

begin
Application.Initialize;
Application.CreateForm(TfrmMain, frmMain);
Application.CreateForm(TfrmLogin, frmLogin);
While true do
begin
frmLogin.ShowModal ;
case frmLogin.ModalResult of
mrOk:
begin
frmLogin.Free;
frmLogin:=nil;
Application.Run;
end;

mrRetry:;

else
frmLogin.Free ;
frmLogin := nil;
end;

if frmLogin = nil then break;

end;
end.
============================================
在登录窗体中的适当位置调用ModalResult :=mrOK就可以完成登录;
 
我就是用这个的,双击也不会出现MainForm,直接退出也不闪烁,因为没有LogonForm.Free,登录后当然可以用其中的变量了。

program MyPro;

uses
Forms,
Main in 'Main.pas' {MainForm},
Logon in 'Logon.pas' {LogonForm};

{$R *.res}

begin
Application.Initialize;
Application.CreateForm(TMainForm, MainForm);
Application.CreateForm(TLogonForm, LogonForm);
if LogonForm.ShowModal = 1 then
Application.Run;
end.
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
D
回复
0
查看
1K
DelphiTeacher的专栏
D
I
回复
0
查看
620
import
I
后退
顶部