Reasonable application user login interface, do not have to create other forms when logging in

xiaoxiao2021-03-06  64

/ (1) Project file Test.dpr // Program serialget;

Uses form, umain in 'umain.pas' {frmmain}, ulogin in 'ulogin.pas' {frmlogin}, udatamodule in 'udatamodule.pas' {datamodule1: tdataModule},

{$ R * .res}

Begin Application.initialize;

If createmutex then // Create a handle to determine if this application is running the BEGIN // to call the global function, create and display the login interface if DOLOGIN THEN / / Success Begin Application.createform (tfrmmain, frmmain); // Data module file is not Before you have created here, because Ulogin.Pas has created //application.createform (TDataModule1, DataModule1); Application.Run; END ELSE // Login Undend DataModule1.Free; Application.Terminate; Except End; End; End; Else Begin Destroymutex; // Release the handle end; end.

(2) Landing form ulogin.pas ulogin.dfm // unit ulogin;

Interfaceuses ... Type ... ... private function checkpsw: integer;

Var FRMLOGIN: TFRMLOGIN

Function DOLOGIN: Boolean; // All project utility function createmutex: boolean; // full project utility procedure destroymutex; // full project utility function

ImplementationUses UDataModule; // Reference Data Module Var Mutex: hwnd;

{$ R * .dfm}

Function DOLOGIN: BOOLOLAN; // Call this function based on project file Begin with tfrogin.create (Application) DO // Create and display the ShowModal property of the login interface begin // Form SHOWMODAL = MROK THEN RESULT: = True else result: = False; free;

Procedure Destroymutex; Begin IF Mutex <> 0 Then CloseHandle (MUTEX); END;

function CreateMutex: Boolean; var PrevInstHandle: THandle; AppTitle: PChar; begin AppTitle: = StrAlloc (100); StrPCopy (AppTitle, Application.Title); Result: = True; Mutex: = Windows.CreateMutex (nil, False, AppTitle) ; if (GetLastError = ERROR_ALREADY_EXISTS) or (Mutex = 0) then begin Result: = False; SetWindowText (Application.Handle, ''); PrevInstHandle: = FindWindow (nil, AppTitle); if PrevInstHandle <> 0 then begin if IsIconic ( PrevInstHandle) then ShowWindow (PrevInstHandle, SW_RESTORE) else BringWindowToTop (PrevInstHandle); SetForegroundWindow (PrevInstHandle); end; if Mutex <> 0 then Mutex: = 0; end; StrDispose (AppTitle); end; // -1: 1 wrong password : Database is not 2: No user 3: legal function tfrmlogin.checkpsw: integer; var name, spsw, sql, svalue: string; begin application.createform (tdataModule1, datamodule1); // Create a data module IF not DataModule1 .Connok the beginning: = 1;

Name: = LowerCase (editname.text); // text box spsw: = lowercase (editpass.text); // text box sql: = 'select * from maker where name = "' name '"; if OpenSQL SQL, DATAMODULE1.DSDATASET <= 0 THEN BEGIN RESULT: = 2;

DataModule1.dsdataSet.first; svalue: = LowerCase (DataModule1.dsDataSet.fieldByname ('loginpsw'); asstring); if svalue <> spswim: = -1 else result: = 3; end; / (3) data module UDataModule.Pas // ... ... type public connok: boolean; end; var datamodule1: tdataModule1; function opensql (s: string; query: tadodataset): integer; function dosql (s: string ; query: tadoquery): boolean; importation

{$ R * .dfm}

procedure TDataModule1.DataModuleCreate (Sender: TObject); // connected ADOConnectionvar SQL, pwd: string; begin try pwd: = 'deliSerial'; SQL: = 'Provider = Microsoft.Jet.OLEDB.4.0; Data Source =' extractfilepath ( paramstr (0)) 'SerialInfo.mdb' '; Persist Security Info = False;' 'Jet OLEDB: Database Password = "' pwd '"'; ADOConnection1.Connected: = false; ADOConnection1.ConnectionString: = SQL ; ADOConnection1.Connected: = true; ConnOK: = true; except ConnOK: = false; end; end; function OpenSQL (s: string; query: TADODataSet): integer; // query SQLvar old_Cursor: TCursor; begin old_Cursor: = screen .cursor; screen.cursor: = Crsqlwait; Try with Query Do Begin Close; CommandText: = S; Open; Result: = query.RecordCount; // Return Result Set Number End; Except Result: = 0; End; Finally Screen.cursor: = OLD_CURSOR; END;

function DoSQL (s: string; query: TADOQuery): boolean; // Run SQLvar old_Cursor: TCursor; begin result: = true; old_Cursor: = screen.cursor; screen.cursor: = crSQLWait; try try with query do begin close; Sql.clear; sql.add (s); execSql; end; Except results: = false; end; finally screen.cursor: = OLD_CURSOR; END;

转载请注明原文地址:https://www.9cbs.com/read-109226.html

New Post(0)