选择显示字体大小

用注册表给应用程序加口令

几乎每个应用系统都有相应的权限管理功能。对于大型多用户应用系统,一般是将权限设置保存在数据库中。但是对于小型的单用户应用系统和非数据库应用系统,就没有必要将权限设置保存在数据库中,但是用文件来保存既不方便又不安全,因为文件很可能被删除或毁坏。其实,灵活地利用windows注册表的特点,可以很方便地为应用系统设置用户名和口令,这种方法不但编程简便,而且安全性比较高。下面举例说明,该例程在windows98,delphi4环境下调试通过。

设计流程为:先判断是否为第一次使用,如果为第一次使用,则要求设置新的用户名和口令,系统将用户名和口令保存起来;如果不是第一次使用,则要求输入用户名和口令才能登录。

delphi开发环境中,新建一个工程project1,在project1中增加一个窗体form1;在form1上增加两个按纽,一个为确定,另一个为取消;再在form1上增加两个text框edituser和editpass,分别为用户名和口令输入框。定义窗体级全局变量regf:tregistry。并在interface的uses中加上registry。

增加函数wsz_iffirst,判断是否是第一次使用;增加过程wsz_saveuse,保存用户名和口令;增加函数wsz_checkuser,判断用户名和口令是否正确。然后在form1的formshow事件和确定按纽事件中分别调用这三个函数和过程,达到设计要求。

//判断是否是第一次使用

function wsz_iffirst:boolean;

var

 suser:string;

 spass:string;

begin

 result := false;

 regf:=tregistry.create;

 regf.rootkey :=hkey_local_machine;

 regf.openkey("software\microsoft\whh726",true);

 suser := regf.readstring("user");

 spass := regf.readstring("pass");

 if ((length(suser)<=0) and (length(spass)<=0)) then

result := true ;

regf.free;

 end;

file://判断用户名和口令是否正确

function wsz_checkuser(myuser:string;mypass :string):boolean;

var

 suser:string;

 spass:string;

begin

 result:= false;

 regf:=tregistry.create;

 regf.rootkey :=hkey_local_machine;

 regf.openkey("software\microsoft\whh726",true);

 suser := regf.readstring("user");

 spass := regf.readstring("pass");

 if (suser=myuser) and (spass=mypass) then

result := true;

regf.free;

 end;

file://保存用户名和口令

procedure wsz_saveuser(myuser:string;mypass :string);

begin

 regf:=tregistry.create;

 regf.rootkey :=hkey_local_machine;

 regf.openkey("software\microsoft\whh726",true);

 regf.writestring("user",myuser);

 regf.writestring("pass",mypass);

 regf.free;

end;

file://取消按纽事件

procedure twszloginform.bitbtncloseclick(sender: tobject);

begin

 close;

end;

file://确定按纽事件

procedure twszloginform.bitbtnyesclick(sender: tobject);

var

 suser:string;

 spass:string;

begin

 suser := trim(edituser.text);

 spass := trim(editpass.text);

 if (length(suser)<=0) or (length(spass)<=0) then

 begin

application.messagebox("用户名和口令都必须输入!",

  "系统提示",

 mb_ok + mb_iconinformation);

 edituser.setfocus ;

exit;

 end;

 if wsz_iffirst then

 begin

file://处理第一次登录的用户名和口令

if application.messagebox(

 "您是第一次使用本软件,请记住您的用户名和口令,

 以便下次登录。现在进入系统吗?",

 "系统提示",

 mb_okcancel + mb_iconquestion) = idok then

 begin

file://保存

wsz_saveuser(suser,spass);

 end

else

 begin

 edituser.setfocus ;

 exit;

 end;

end else

 begin

if not wsz_checkuser(suser,spass) then

begin

application.messagebox

 ("对不起,用户名和口令输入不正确!", "系统提示",

mb_ok + mb_iconinformation);

 edituser.setfocus ;

exit;

end;

 end;

 file://全局变量

 gb_wsz_user := suser;

 gb_wsz_pass := spass;

 close;

 end;

 file://窗体的show事件

procedure twszloginform.formshow(sender: tobject);

 begin

if wsz_iffirst then

 begin

application.messagebox(

"您是第一次使用本软件,您可以随便输入用户名和口令。",

"系统提示",

mb_ok + mb_iconinformation);

end;

 end;



  


 


关键字 本文所属关键字

相关 与本文相关文章

分类 所有文章关键字导航

源码编程相关

Java   Asp   PHP   .Net   XML   C/C++   CGI   VB   Jsp   J2ee   J2se   J2me   EJB   Servlet   Tomcat   Resin   Struts   Weblogic   Eclipse   ANT   GUI   JMS   Web servise   IDEA   Webphere   Hibernate   Spring   Jboss   Applet   Swing   Socket   Javamail   Perl   Ajax   P2P   安全   模式   框架   测试   开源   游戏

SQL数据库相关

My-SQL   Ms-SQL   Access   DB2   Oracle   Sybase   SQLserver   索引   存储过程   加密   数据库   分页   视图  

手机无线相关

3G   Wap   CDMA   GRPS   GSM   IVR   彩信   短信   无线   增值业务

网页设计制作相关

HTML   CSS   网页配色   网页特效   Javascript   VBscript   Dreamweaver   Frontpage   JS   Web   网站设计

网站建设推广相关

建站经验   网站优化   网站排名   推广   Alexa

操作系统/服务器相关

Windows XP   Windows 2000   Windows 2003   Windows Me   Windows 9.x   Linux   UNIX   注册表   操作系统   服务器   应用服务器

图形图像多媒体相关

Photoshop   Fireworks   Flash   Coreldraw   Illustrator   Freehand   Photoimpact   多媒体   图形图像

标准 网站致力的规范

Valid CSS!

无不良内容,无不良广告,无恶意代码

Valid XHTML 1.0 Transitional

creativecommons