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

zhushow 2006-05-02 03:19:20
做一个软件的登录,Form1就是填用户名和密码,若正确则显示Form2,若错误则关闭。请达人指教……
...全文
142 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
BlueDeepOcean 2006-05-03
  • 打赏
  • 举报
回复
针对新手而言,最简单、最直接的引领就是采用直观的办法:

设计两个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及时程序的主要功能所在的窗体。
stevenjscn 2006-05-02
  • 打赏
  • 举报
回复
//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();
}
stevenjscn 2006-05-02
  • 打赏
  • 举报
回复
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;
//---------------------------------------------------------------------------
#endif
stevenjscn 2006-05-02
  • 打赏
  • 举报
回复
在工程文件的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;
}

13,873

社区成员

发帖
与我相关
我的任务
社区描述
C++ Builder相关内容讨论区
社区管理员
  • 基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧