合理应用用户登录界面用户登录时不必创建其他窗体

ffflll60

ffflll60

2016-02-19 13:20

下面是个合理应用用户登录界面用户登录时不必创建其他窗体教程,撑握了其技术要点,学起来就简单多了。赶紧跟着图老师小编一起来看看吧!

  /////////////////////  (一)项目文件  test.dpr //////////////////////
  program SerialGet;

  uses
    Forms,
    UMain in 'UMain.pas' {frmMain},
    ULogin in 'ULogin.pas' {frmLogin},
    UDataModule in 'UDataModule.pas' {DataModule1: TDataModule},

  {$R *.res}

  begin
    Application.Initialize;

    if CreateMutex then                   begin
      //调用全局函数,创建并显示登陆界面
      if doLogin then                       begin
        Application.CreateForm(TfrmMain, frmMain);
        //数据模块文件不须在这儿创建,因为 ULogin.pas 中已创建
        //Application.CreateForm(TDataModule1, DataModule1);
        Application.Run;
      end else                              begin
        try
          DataModule1.free;
          Application.terminate;
        except
        end;
      end;
    end else
    begin
      DestroyMutex;                     //释放句柄
    end;
  end.

  ////////////////  (二)登陆窗体 ULogin.pas  ULogin.dfm //////////////////
  unit ULogin;

  interface
  uses ......
  type
    ... ... ...
    private
      function checkPsw:integer;
    public
    end;

(本文来源于图老师网站,更多请访问https://m.tulaoshi.com/bianchengyuyan/)

  var
    frmLogin: TfrmLogin;

    function doLogIn:boolean;          // 全项目公用函数
    function CreateMutex: Boolean;     // 全项目公用函数
    procedure DestroyMutex;            // 全项目公用函数

  implementation
  uses UDataModule;  //引用数据模块
  var Mutex: hWnd;

(本文来源于图老师网站,更多请访问https://m.tulaoshi.com/bianchengyuyan/)

  {$R *.dfm}

  function doLogIn:boolean;                 //由项目文件调用此函数
  begin
    with TfrmLogin.create(application) do   //创建并显示登陆界面
    begin
      //窗体的ShowModal属性
      if ShowModal = mrok then result := true else result := false;
      free;
    end;
  end;

  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:数据库不对  2:没有此用户  3:合法
  function TfrmLogin.checkPsw:integer;
  var name,sPsw,SQL,sValue:string;
  begin
    Application.CreateForm(TDataModule1, DataModule1);   if not DataModule1.ConnOK then
    begin result := 1;   exit;  end;

    name := lowercase(editName.text);  //文本框
    sPsw := lowercase(editPass.text);  //文本框
    sql := 'select * from maker where name="'+name+'"';
    if openSQL(SQL,DataModule1.dsDataSet) =0 then
    begin result := 2; exit;  end;

    DataModule1.dsDataSet.First ;
    sValue := lowercase(DataModule1.dsDataSet.fieldbyName('loginPsw').asString);
    if sValuesPsw then result := -1 else result := 3;
  end;
  
  /////////////////////  (三)数据模块 UDataModule.pas //////////////////////
  
... ... ... ...
  type
    public
      ConnOK:boolean;
    end;
  var
    DataModule1: TDataModule1;
    function OpenSQL(s: string;query:TADODataSet):integer;
    function DoSQL(s: string;query:TADOQuery):boolean;
   
  implementation

  {$R *.dfm}

  procedure TDataModule1.DataModuleCreate(Sender: TObject); //连接ADOConnection
  var 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; //查询SQL
  var old_Cursor:TCursor;
  begin
    old_Cursor:=screen.cursor;
    screen.cursor:=crSQLWait;
    try
      try
        with query do
        begin
          close; commandtext:=s; open;
          result:=query.recordcount;       //返回结果集记录数
        end;
      except
       result:=0;
      end;
    finally
      screen.cursor:=old_Cursor;
    end;
  end;

  function DoSQL(s: string;query:TADOQuery):boolean;  //运行 SQL
  var 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
        result:=false;
      end;
    finally
      screen.cursor:=old_Cursor;
    end;
  end;

展开更多 50%)
分享

猜你喜欢

合理应用用户登录界面用户登录时不必创建其他窗体

编程语言 网络编程
合理应用用户登录界面用户登录时不必创建其他窗体

Photoshop设计简洁灰色用户登录界面

电脑网络
Photoshop设计简洁灰色用户登录界面

s8lol主宰符文怎么配

英雄联盟 网络游戏
s8lol主宰符文怎么配

SharePoint2018 以其他用户登录和修改AD域用户密码

电脑网络
SharePoint2018 以其他用户登录和修改AD域用户密码

网站登录页面怎么设计好?用户登录页面设计原则

交互设计 网页设计 网站设计
网站登录页面怎么设计好?用户登录页面设计原则

lol偷钱流符文搭配推荐

英雄联盟 网络游戏
lol偷钱流符文搭配推荐

如何隐藏Win7登录界面的administrator用户名

电脑入门
如何隐藏Win7登录界面的administrator用户名

JSP如何保存用户上次登录时间

Java JAVA基础
JSP如何保存用户上次登录时间

lolAD刺客新符文搭配推荐

英雄联盟
lolAD刺客新符文搭配推荐

excel2010怎么插入图形

excel2010怎么插入图形

jsp入门:初学者对Spring框架的认识

jsp入门:初学者对Spring框架的认识
下拉加载更多内容 ↓