在DELPHI下新建一工程,放置Edit1,Edit2,Label1,Label2,Button1组件.具体代码如下:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls,Registry;//在此加上Registry以便调用注册表.
type
TForm1 = class(TForm)
Button1: TButton;
Edit1: TEdit;
Edit2: TEdit;
Label1: TLabel;
Label2: TLabel;
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
Function Check():Boolean;
Procedure CheckReg();
Procedure CreateReg();
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
PName:string; //全局变量,存放用户名和注册码.
PPass:integer;
implementation
{$R *.DFM}
Procedure TForm1.CreateReg();//创建用户信息.
var Rego:TRegistry;
begin
Rego:=TRegistry.Create;
Rego.RootKey:=HKEY_USERS;
rego.OpenKey('.DEFAULT\Software\AngelSoft\Demo',True);//键名为AngelSoft\Demo,可自行修改.
Rego.WriteString('Name',PName);//写入用户名.
Rego.WriteInteger('Pass',PPass);//写入注册码.
Rego.Free;
ShowMessage('程序已经注册,谢谢!');
CheckReg; //刷新.
end;
Procedure TForm1.CheckReg();//检查程序是否在注册表中注册.
var Rego:TRegistry;
begin
Rego:=TRegistry.Create;
Rego.RootKey:=HKEY_USERS;
IF Rego.OpenKey('.DEFAULT\Software\AngelSoft\Demo',False) then
begin
Form1.Caption:='软件已经注册';
Button1.Enabled:=false;
Label1.Caption:=rego.ReadString('Name');//读用户名.
Label2.Caption:=IntToStr(Rego.ReadInteger('Pass')); //读注册码.
rego.Free;
end
else Form1.Caption:='软件未注册,请注册';
end;
Function TForm1.Check():Boolean;//检查注册码是否正确.
var
Temp:pchar;
Name:string;
c:char;
i,Long,Pass:integer;
begin
Pass:=0;
Name:=edit1.Text;
long:=length(Name);
for i:=1 to Long do
begin
temp:=pchar(copy(Name,i,1));
c:=temp^;
Pass:=Pass+ord(c); //将用户名每个字符转换为ASCII码后相加.
end;
if StrToInt(Edit2.Text)=pass then
begin
Result:=True;
PName:=Name;
PPass:=Pass;
end
else Result:=False;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
if Check then CreateReg
else ShowMessage('注册码不正确,无法注册');
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
CheckReg;
end;
end.
<注册器>
在DELPHI下新建一工程,放置Edit1,Edit2,Button1组件.具体代码如下:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Edit1: TEdit;
Edit2: TEdit;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.Button1Click(Sender: TObject);
var
Temp:pchar;
Name:string;
c:char;
i,Long,Pass:integer;
begin
Pass:=0;
Name:=edit1.Text;
long:=length(Name);
for i:=1 to Long do
begin
temp:=pchar(copy(Name,i,1));
c:=temp^;
Pass:=Pass+ord(c);
end;
edit2.text:=IntToStr(pass);
end;
end.