一,新建unigui项目,一切选默认
二,数据库方面准备,这里我们使用unidac连接oracle数据库,并在数据里面准备一个用户表
create table user_t (username varchar2(20),password varchar2(20),cdate date default sysdate);
insert into user_t(username,password) values('u1','p1');
insert into user_t(username,password) values('u2','p2');
insert into user_t(username,password) values('u3','p3');
三,在mainmodule进行数据库连接的设定,如下图
四,新建unigui窗体,如下图
注意下面的选项,确认为登录窗口选项
然后在窗口上面放以下的控件,完成后如下图
登录窗口中的代码如下:
unit loginform_pas;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics,
Controls, Forms, uniGUITypes, uniGUIAbstractClasses,
uniGUIClasses, uniGUIRegClasses, uniGUIForm, uniButton, uniLabel,
uniGUIBaseClasses, uniEdit;
type
TLoginForm = class(TUniLoginForm)
UniEdit1: TUniEdit;
UniLabel1: TUniLabel;
UniLabel2: TUniLabel;
UniEdit2: TUniEdit;
UniButton1: TUniButton;
UniButton2: TUniButton;
procedure UniButton1Click(Sender: TObject);
procedure UniButton2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
function LoginForm: TLoginForm;
implementation
{$R *.dfm}
uses
uniGUIVars, MainModule, uniGUIApplication;
function LoginForm: TLoginForm;
begin
Result := TLoginForm(UniMainModule.GetFormInstance(TLoginForm));
end;
procedure TLoginForm.UniButton1Click(Sender: TObject);
begin
if not UniMainModule.linkoracle.Connected then
UniMainModule.linkoracle.Connected:=true;
with UniMainModule.login_u do
begin
close;
sql.Clear;
sql.Add('select count(*) as count from user_t where username=:na and password=:pa');
ParamByName('na').Value := uniedit1.Text ;
ParamByName('pa').Value := uniedit2.Text ;
open;
if fieldvalues['count']>0 then
begin
UniMainModule.user_name:=uniedit1.Text;
self.ModalResult:=mrok;
end
else
begin
unisession.AddJS('alert("用户名或密码错误!");');
end;
end;
end;
procedure TLoginForm.UniButton2Click(Sender: TObject);
begin
self.ModalResult:=mrcancel;
end;
initialization
RegisterAppFormClass(TLoginForm);
end.
在MainModule单元中加入
main窗口的show事件中加入以下代码
self.Caption :='当前登录用户为:'+UniMainModule.user_name;
一切OK了,让我们开始运行代码,效果如下
ok,超级简单的完成了登录过程,并且将当前登录用户传递到了主程序窗口。