5,388
社区成员
发帖
与我相关
我的任务
分享
有折腾了一套客户端,对接自己的 java 后端,有涉及到这些东东,可以参考下。
系统功能定义,className是窗体名称,用于动态实例化相应窗体类,窗体里的具体功能权限约束,是由接口决定是否可调用。如下:
type
PFunc = ^TFunc;
TFunc = record
id : integer;
code : string;
url : string;
className: string;
showStyle: smallint;
title : string;
funcs : string;
leaf : smallint;
remark : string;
enabled : smallint;
end;
TFuncs = record
count: integer;
datas: array of TFunc;
end;
以下代码为构建操作目录树:
class procedure TSMUIDrv.FreeFuncTV(const tv: TTreeView);
var
i: Integer;
begin
tv.OnChange := nil;
for i:=0 to tv.Items.Count-1 do
begin
Dispose(PFunc(tv.Items[i].Data));
end;
tv.Items.Clear;
end;
function GetFuncParentNode(childNode: TTreeNode; childKey: string; ItemLen: Integer=1): TTreeNode;
var
ParentKey: string;
ParentNode: TTreeNode;
begin
ParentKey := LeftStr(ChildKey,Length(ChildKey)-ItemLen);
ParentNode := ChildNode;
while ParentNode<>nil do
begin
if PFunc(ParentNode.Data)^.code = ParentKey then Break;
ParentNode := ParentNode.Parent;
end;
Result := ParentNode;
end;
procedure BuildTreeFunNode(tv: TTreeView; funcs: string; buidFun: boolean; parentNode: TTreeNode);
var
i: integer;
lpFun: PFunc;
jo: ISuperObject;
ja: TSuperArray;
newNode: TTreeNode;
begin
if (not buidFun) or (funcs='') then exit;
jo := TSMSystem.GetJsonObject(funcs);
if jo=nil then exit;
ja := jo.AsArray;
if (ja=nil) or (ja.Length=0) then exit;
for i:=0 to ja.Length-1 do
begin
newNode := tv.Items.AddChild(parentNode,TSMSystem.GetJsonValue(ja[i],'title',jsStr));
new(lpFun);
lpFun^.id := 0;
lpFun^.code := TSMSystem.GetJsonValue(ja[i],'id',jsStr);
newNode.Data := lpFun;
newNode.SelectedIndex := 2;
newNode.StateIndex := 2;
end;
end;
class procedure TSMUIDrv.BuidFuncTV(tv: TTreeView; funcs: TFuncs; buildFun: boolean; root: string='');
var
i: Integer;
lpFun: PFunc;
NewNode,ParentNode: TTreeNode;
begin
NewNode := nil;
FreeFuncTV(tv);
if root<>'' then
begin
NewNode := tv.Items.AddChild(nil,root);
new(lpFun);
lpFun.title := root;
lpFun^.code := '';
lpFun^.leaf := 0;
if tv.Images<>nil then
begin
NewNode.ImageIndex := 0;
NewNode.SelectedIndex := 0;
NewNode.StateIndex := 0;
end;
NewNode.Data := lpFun;
end;
for i:=0 to funcs.count-1 do
begin
if (funcs.datas[i].enabled<>1) then continue;
ParentNode := GetFuncParentNode(NewNode,funcs.datas[i].code);
NewNode := tv.Items.AddChild(ParentNode,funcs.datas[i].title);
NewNode.ImageIndex := 1;
NewNode.SelectedIndex := 2;
NewNode.StateIndex := 2;
new(lpFun);
lpFun^ := funcs.datas[i];
NewNode.Data := lpFun;
BuildTreeFunNode(tv,funcs.datas[i].funcs,buildFun,newNode);
end;
end;
以下代码为动态执行树对应的功能(发消息到主窗体统一执行):
procedure TFmNavicate.OnExecFunc(sender: TObject);
var
func: TFunc;
node: TTreeNode;
level: integer;
begin
if lvFuns.Selected<>nil then
begin
func := PFunc(lvFuns.Selected.Data)^;
if func.className<>'' then
begin
SendMessage(FmMain.Handle,WM_EXECUTE_FUN,Integer(@func),0);
end;
node := FNavTree.Selected;
level:= node.Level;
if func.leaf=0 then
begin
while (node<>nil) and (node.Level>=level) do
begin
if PFunc(node.Data)^.code=func.code then
begin
node.Selected := true;
node.Expand(true);
break;
end;
node := node.GetNext;
end;
end;
end;
end;
主窗体执行相应功能:
procedure TFmMain.OnExecuteTvFunc(sender: TObject);
var
func: TFunc;
begin
if tvFunc.Selected<>nil then
begin
func := PFunc(tvFunc.Selected.Data)^;
TSMUIDrv.LoadFuncForm(pcCtl, func.className, func.showStyle);
end;
end;
动态创建窗体:
class function TSMUIDrv.LoadFuncForm(parent: TRzPageControl; clsName: string; showStyle: smallint): TAWin;
var
i,j: integer;
tmp: TComponent;
sht: TRzTabSheet;
begin
if (showStyle=1) then
begin
tmp := LoadComponent(clsName,nil);
if tmp<>nil then
begin
result := TAWin(tmp);
result.ShowModal;
result.Free;
end;
result := nil;
exit;
end;
//form exists?
for i:=0 to parent.PageCount-1 do
begin
for j:=0 to parent.Pages[i].ControlCount-1 do
begin
if parent.Pages[i].Controls[j].ClassName=clsName then
begin
parent.ActivePageIndex := i;
result := TAWin(parent.Pages[i].Controls[j]);
exit;
end;
end;
end;
//to load new form
sht := TRzTabSheet.Create(parent);
sht.PageControl := parent;
tmp := LoadComponent(clsName,sht);
if tmp<>nil then
begin
result := TAWin(tmp);
result.Parent := sht;
TAWin(tmp).BorderStyle := bsNone;
sht.Caption := TAWin(tmp).Caption;
result.Align := alClient;
sht.Tag := TAWin(tmp).Tag;
result.Show;
parent.ActivePageIndex := parent.PageCount-1;
end
else begin
sht.Free;
result := nil;
end;
end;
效果图及主要数据表配置: