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

2016-02-19 13:20 3 1 收藏

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

【 tulaoshi.com - 编程语言 】

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

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

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

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

  {$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;

  var
    frmLogin: TfrmLogin;

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

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

  {$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;

来源:https://www.tulaoshi.com/n/20160219/1603839.html

延伸阅读
手机当当网如何切换用户登录   1.进入手机当当网,点击右下角 2.在我的当当页面点击左上角 3.找到底部的 4.回到我的当当,点击 5.输入要登录的账号密码及验证码,点击即可
Win7纯净版32位系统下开机到登录界面时提示用户界面失败怎么办   在操作win7系统过程中,经常会在开机过程中遇到各种各样的故障,比如有使用win7纯净版32位系统的用户反映说他的电脑开机启动到登录界面的时候,系统突然弹出一个错误窗口,提示用户界面失败,导致无法正常登录系统,遇到这样的问题该如何处理呢?接下来win7之家图老师小...
标签: windows 操作系统
  “在安装了SP2以后,您的XP系统将可以像那些服务器操作系统一样,供多人同时在不同地点登录了,虽然这个多人仅仅等于2人。”以上是SP2还处在2055版本的Beta测试的时候被公开的一个新特性(相关报道:装了SP2吗?XP系统可以多人同时登录了)。然而在2055以后的beta版和微软发布的正式版本的SP2中,这个特性都不见了踪影。是什么原因...
标签: word
创建用户在Word2007中填写的窗体   1、创建窗体 步骤一:对 Word 进行创建窗体的设置 1.单击Office 按钮 ,然后单击Word 选项。 2.单击常用。 3.选中在功能区显示‘开发工具’选项卡复选框,然后单击确定。 步骤二:打开一个模板或文档,以便以此为基础创建窗体 1.单击Office 按钮 ,然后单...
标签: 电脑入门
为了计算机安全,相信使用Windows7系统的朋友大多都为自己的用户账户设置了或简单或复杂的密码,其实对于个人电脑来说,开机登录输入密码有时候会显得累赘,毕竟用户账户密码不仅仅是为了防止他人登录。那么有没有方法既设置了用户密码又可以省掉登录输入密码这一步骤呢?今天就为大家简单介绍几种方法。 第一,魔方在手,优化不愁 打开魔方优...

经验教程

459

收藏

90
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部