CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
不看会后悔的Windows XP之经验谈 简单快捷DIY实用家庭影院
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  C++ Builder >  基础类

请教怎么制作程序的登录框

楼主zhushow(小酷猪)2006-05-02 15:19:20 在 C++ Builder / 基础类 提问

做一个软件的登录,Form1就是填用户名和密码,若正确则显示Form2,若错误则关闭。请达人指教…… 问题点数:20、回复次数:4Top

1 楼stevenjscn(小胖)回复于 2006-05-02 15:44:28 得分 10

在工程文件的CPP中写入  
  #include   <vcl.h>  
  #include   "Login.h"  
  #pragma   hdrstop  
  //---------------------------------------------------------------------------  
  USEFORM("Main.cpp",   MainForm);  
  USEFORM("BIOCLOCKDM.cpp",   dmBC);   /*   TDataModule:   File   Type   */  
  USEFORM("Login.cpp",   LoginForm);  
   
  //---------------------------------------------------------------------------  
  WINAPI   WinMain(HINSTANCE,   HINSTANCE,   LPSTR,   int)  
  {  
          TLoginForm   *frm=new   TLoginForm(Application);  
   
          try  
          {  
                    Application->Initialize();  
                    Application->CreateForm(__classid(TdmBC),   &dmBC);  
                    frm->ShowModal();  
                    if(frm->iCancel==0)  
                    {  
                          delete   frm;  
                          Application->Run();  
                          return   0;  
                    }  
                    delete   frm;  
                    Application->CreateForm(__classid(TMainForm),   &MainForm);  
   
                    Application->Run();  
          }  
          catch   (Exception   &exception)  
          {  
                    Application->ShowException(&exception);  
          }  
          catch   (...)  
          {  
                    try  
                    {  
                            throw   Exception("");  
                    }  
                    catch   (Exception   &exception)  
                    {  
                            Application->ShowException(&exception);  
                    }  
          }  
          return   0;  
  }  
  Top

2 楼stevenjscn(小胖)回复于 2006-05-02 15:46:35 得分 0

LOGINFORM的头文件:  
  #ifndef   LoginH  
  #define   LoginH  
  //---------------------------------------------------------------------------  
  #include   <Classes.hpp>  
  #include   <Controls.hpp>  
  #include   <StdCtrls.hpp>  
  #include   <Forms.hpp>  
  #include   <Buttons.hpp>  
  #include   <ExtCtrls.hpp>  
  #include   <Graphics.hpp>  
  #include   <Dialogs.hpp>  
  #include   <ADODB.hpp>  
  #include   <DB.hpp>  
  #include   <ComCtrls.hpp>  
  //---------------------------------------------------------------------------  
  extern   "C"   __declspec(dllimport)   long   GetCPUID();  
  extern   "C"   __declspec(dllimport)   AnsiString   GetRegCode(AnsiString   sCPUID);  
   
  class   TLoginForm   :   public   TForm  
  {  
  __published: //   IDE-managed   Components  
          TPanel   *Panel1;  
          TImage   *Image1;  
          TBitBtn   *btnOK;  
          TBitBtn   *btnCancel;  
          TEdit   *edtUser;  
          TEdit   *edtPswd;  
          TEdit   *edtDBLocation;  
          TButton   *btnBrowse;  
          TLabel   *Label1;  
          TLabel   *Label2;  
          TLabel   *Label3;  
          TOpenDialog   *odDialog;  
          TGroupBox   *GroupBox1;  
          TLabel   *Label4;  
          TDateTimePicker   *dtDateFrom;  
          TLabel   *Label5;  
          TDateTimePicker   *dtDateTo;  
          TBitBtn   *btnOptions;  
          void   __fastcall   FormActivate(TObject   *Sender);  
          void   __fastcall   FormCreate(TObject   *Sender);  
          void   __fastcall   edtUserKeyPress(TObject   *Sender,   char   &Key);  
          void   __fastcall   edtPswdKeyPress(TObject   *Sender,   char   &Key);  
          void   __fastcall   btnCancelClick(TObject   *Sender);  
          void   __fastcall   btnOKClick(TObject   *Sender);  
          void   __fastcall   FormClose(TObject   *Sender,   TCloseAction   &Action);  
          void   __fastcall   btnBrowseClick(TObject   *Sender);  
          void   __fastcall   btnOptionsClick(TObject   *Sender);  
  private: //   User   declarations  
  public: //   User   declarations  
          __fastcall   TLoginForm(TComponent*   Owner);  
          int   iCancel;  
  };  
  //---------------------------------------------------------------------------  
  extern   PACKAGE   TLoginForm   *LoginForm;  
  //---------------------------------------------------------------------------  
  #endifTop

3 楼stevenjscn(小胖)回复于 2006-05-02 15:52:25 得分 0

//cpp---------------------------------------------------------------------------  
   
  void   __fastcall   TLoginForm::btnOKClick(TObject   *Sender)  
  {  
          if(edtUser->Text.IsEmpty()   )  
          {  
                  MessageDlg("Please   input   the   legal   user   name",  
                  mtInformation,TMsgDlgButtons()<<mbOK,0);  
                  edtUser->SetFocus();  
                  return;  
          }  
          if(edtDBLocation->Text.IsEmpty())  
          {  
                  MessageDlg("Please   configure   the   database   location",  
                  mtInformation,TMsgDlgButtons()<<mbOK,0);  
                  btnBrowse->SetFocus();  
                  return;  
          }  
          AnsiString   sCn,   sDriver,   sDBQ,sDateFrom,sDateTo,sFilter;  
          TRegistry   *Reg=new   TRegistry();  
   
          Reg->RootKey=HKEY_LOCAL_MACHINE;  
          Reg->OpenKey(ODBC_DRIVER,true);  
          sDriver=Reg->ReadString("Driver");  
          Reg->CloseKey();  
   
          sDBQ=edtDBLocation->Text;  
   
          Reg->RootKey=HKEY_CURRENT_USER;  
          Reg->OpenKey(ODBC_KEY,true);  
          Reg->WriteString("Driver",sDriver);  
          Reg->WriteString("DBQ",sDBQ);  
          Reg->WriteInteger("DriverID",25);  
          Reg->WriteString("FIL","MS   Access");  
          Reg->WriteInteger("SafeTransactions",0);  
          Reg->WriteString("UID","");  
   
          sCn="Provider=MSDASQL.1;Password=751216steven;";  
          sCn+="Persist   Security   Info=True;Data   Source=BIOCLOCK   DSN;";  
   
          sDateFrom=DateTimeToStr(dtDateFrom->Date).SubString(1,10);  
          sDateTo=DateTimeToStr(dtDateTo->Date).SubString(1,10);  
          sFilter="[Date]>=#"   +   sDateFrom     +   "#   AND   [Date]<=#"   +   sDateTo   +   "#";  
   
          Reg->OpenKey(REG_KEY,true);  
          Reg->WriteString("CN",sCn);  
          Reg->WriteString("Filter",sFilter);  
          Reg->WriteString("DateFrom",sDateFrom);  
          Reg->WriteString("DateTo",sDateTo);  
   
          try  
          {  
                  dmBC->cnBC->Close();  
                  dmBC->cnBC->ConnectionString=sCn;  
                  dmBC->cnBC->Open();  
          }  
          catch   (Exception   &exception)  
          {  
                    Application->ShowException(&exception);  
          }  
          catch   (...)  
          {  
                    try  
                    {  
                            throw   Exception("");  
                    }  
                    catch   (Exception   &exception)  
                    {  
                            Application->ShowException(&exception);  
                            return;  
                    }  
          }  
   
          AnsiString   sUser,sPswd,sSQL;  
          TADOQuery   *qryTemp;  
          sUser=edtUser->Text;  
          sSQL="Select   UserName,Password   From   Users   Where   UserName='"   +   sUser   +"'";  
          qryTemp=new   TADOQuery(this);  
          qryTemp->ConnectionString=sCn;  
          qryTemp->SQL->Clear();  
          qryTemp->SQL->Add(sSQL);  
          qryTemp->Open();  
          if(qryTemp->Eof)  
          {  
                  MessageDlg("Illegal   User!\nPlease   contact   administrator   for   useers   configure.",  
                  mtError,TMsgDlgButtons()<<mbOK,0);  
                  qryTemp->Close();  
                  delete   qryTemp;  
                  edtUser->SetFocus();  
                  return;  
          }  
          else  
          {  
                  sPswd=qryTemp->FieldByName("Password")->AsString;  
          }  
          qryTemp->Close();  
          delete   qryTemp;  
          if(sPswd!=edtPswd->Text)  
          {  
                  MessageDlg("Wrong   Password!\nPlease   try   again.",  
                  mtInformation,TMsgDlgButtons()<<mbOK,0);  
                  edtPswd->SetFocus();  
                  return;  
          }  
   
          AnsiString   sRegCode,sCPUID;  
          int   iRunTimes;  
   
          Reg->RootKey=HKEY_CURRENT_USER;  
          Reg->OpenKey(REG_KEY,true);  
          Reg->WriteString("User",sUser);  
          sRegCode=Reg->ReadString("RegCode");  
          Reg->CloseKey();  
   
          sCPUID=IntToStr(GetCPUID());  
          if(!sRegCode.IsEmpty()   &&   sRegCode!=GetRegCode(sCPUID))  
          {  
                  MessageDlg("Wrong   Registration!\nPlease   contact   the   author",  
                          mtError,TMsgDlgButtons()<<mbOK,0);  
                          delete   Reg;  
                          iCancel=0;  
                          Close();  
                          return;  
          }  
          else   if(sRegCode.IsEmpty())  
          {  
                  Reg->RootKey=HKEY_USERS;  
                  Reg->OpenKey(RUNTIME_KEY,true);  
                  iRunTimes=Reg->ReadInteger("RunTimes");  
                  iRunTimes+=1;  
                  if(iRunTimes>50)  
                  {  
                          MessageDlg("The   system   has   been   over   the   registration   limitation",  
                          mtError,TMsgDlgButtons()<<mbOK,0);  
                          delete   Reg;  
                          iCancel=0;  
                          Close();  
                          return;  
                  }  
                  else  
                  {  
                          MessageDlg("The   system   can   be   run   "   +   IntToStr(50-iRunTimes)   +   "   Times!",  
                          mtInformation,TMsgDlgButtons()<<mbOK,0);  
                          Reg->WriteInteger("RunTimes",iRunTimes);  
                  }  
          }  
   
          delete   Reg;  
          iCancel=1;  
          Close();  
  }  
  Top

4 楼BlueDeepOcean(蓝色·深海)回复于 2006-05-03 16:09:43 得分 10

针对新手而言,最简单、最直接的引领就是采用直观的办法:  
   
  设计两个Form,Form1和Form2。在Form1所在的.cpp文件中包括Form2的单元文件,即:#include"Form2的单元文件.h";  
  之后,我们让Form1作为程序的启动界面。(即是设置Form1无边框,上面存在一幅图片等)  
  在Form1中添加一个TTimer控件,设置其Interval为3000(即3秒)在Timer的OnTime事件中写入:“  
          Form1->Hide();  
          Form2->Show();  
  ”  
  这样,在程序的启动界面停留在屏幕上3秒钟之后,就会自动隐藏,并将Form2显示出来。而Form2及时程序的主要功能所在的窗体。Top

相关问题

关键词

得分解答快速导航

  • 帖主:zhushow
  • stevenjscn
  • BlueDeepOcean

相关链接

  • CSDN Blog
  • 技术文档
  • 代码下载
  • 第二书店
  • 读书频道

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
北京创新乐知广告有限公司 版权所有, 京 ICP 证 070598 号
世纪乐知(北京)网络技术有限公司 提供技术支持
Copyright © 2000-2008, CSDN.NET, All Rights Reserved
GongshangLogo