一、如何动态创建dxNavBar内容:

function TMain.GetAcitonByCaption(const aCategory,aCaption: string): Integer;
var
i: Integer;
begin
Result := -1;
for i := 0 to ActionManager1.ActionCount-1 do
begin
if System.SysUtils.SameText(TAction(ActionManager1.Actions[i]).Category,aCategory) and
System.SysUtils.SameText(TAction(ActionManager1.Actions[i]).Caption,aCaption)
then
begin
Result := i;
Exit;
end;
end;
end; procedure TMain.FormShow(Sender: TObject);
var
aBarItem:TdxNavBarItem;
aBarGroup:TdxNavBargroup;
iAction :Integer;
begin
dxNavBar1.Items.Clear;
dxNavBar1.Groups.Clear;
//++++++++++++++++++++++++++++++++++++++++++++++++//
aBarGroup:=dxNavBar1.Groups.Add;
aBarGroup.Caption := '系统设置'; aBarItem:=dxNavBar1.Items.Add;
aBarItem.Caption:= '操作员管理';
//aBarItem.OnClick:= button1.OnClick; //绑定事件
iAction := GetAcitonByCaption(cds_Master.FieldByName(aBarItem.Caption).AsString,sMenu);
aBarItem.Action := TAction(ActionManager1.Actions[iAction]); aBarGroup.CreateLink(aBarItem); aBarItem:=dxNavBar1.Items.Add;
aBarItem.Caption := '权限管理';
iAction := GetAcitonByCaption(cds_Master.FieldByName(aBarItem.Caption).AsString,sMenu);
aBarItem.Action := TAction(ActionManager1.Actions[iAction]); aBarGroup.CreateLink(aBarItem); aBarItem:=dxNavBar1.Items.Add;
aBarItem.Caption := '报表设计中心';
iAction := GetAcitonByCaption(cds_Master.FieldByName(aBarItem.Caption).AsString,sMenu);
aBarItem.Action := TAction(ActionManager1.Actions[iAction]);
aBarGroup.CreateLink(aBarItem); aBarItem:=dxNavBar1.Items.Add;
aBarItem.Caption:= '系统退出';
iAction := GetAcitonByCaption(cds_Master.FieldByName(aBarItem.Caption).AsString,sMenu);
aBarItem.Action := TAction(ActionManager1.Actions[iAction]);
aBarGroup.CreateLink(aBarItem); //++++++++++++++++++++++++++++++++++++++++++++++++//
aBarGroup := dxNavBar1.Groups.Add;
aBarGroup.Caption := '接口管理'; aBarItem:=dxNavBar1.Items.Add;
aBarItem.Caption := '学生基本信息接口';
iAction := GetAcitonByCaption(cds_Master.FieldByName(aBarItem.Caption).AsString,sMenu);
aBarItem.Action := TAction(ActionManager1.Actions[iAction]);
aBarGroup.CreateLink(aBarItem); aBarItem:=dxNavBar1.Items.Add;
aBarItem.Caption := '学生教学任务接口';
iAction := GetAcitonByCaption(cds_Master.FieldByName(aBarItem.Caption).AsString,sMenu);
aBarItem.Action := TAction(ActionManager1.Actions[iAction]);
aBarGroup.CreateLink(aBarItem); aBarItem:=dxNavBar1.Items.Add; aBarItem.Caption := '学生教材缴费接口';
iAction := GetAcitonByCaption(cds_Master.FieldByName(aBarItem.Caption).AsString,sMenu); aBarItem.Action := TAction(ActionManager1.Actions[iAction]); aBarGroup.CreateLink(aBarItem); aBarItem:=dxNavBar1.Items.Add;
aBarItem.Caption := '学生教材结算接口';
iAction := GetAcitonByCaption(cds_Master.FieldByName(aBarItem.Caption).AsString,sMenu);
aBarItem.Action := TAction(ActionManager1.Actions[iAction]);
aBarGroup.CreateLink(aBarItem);
//++++++++++++++++++++++++++++++++++++++++++++++++//
aBarGroup := dxNavBar1.Groups.Add;
aBarGroup.Caption := '帮助'; aBarItem:=dxNavBar1.Items.Add;
aBarItem.Caption := '用户手册';
iAction := GetAcitonByCaption(cds_Master.FieldByName(aBarItem.Caption).AsString,sMenu);
aBarItem.Action := TAction(ActionManager1.Actions[iAction]);
aBarGroup.CreateLink(aBarItem); aBarItem.Caption := '关于……';
iAction := GetAcitonByCaption(cds_Master.FieldByName(aBarItem.Caption).AsString,sMenu);
aBarItem.Action := TAction(ActionManager1.Actions[iAction]);
aBarGroup.CreateLink(aBarItem);
end;

二、实际生成一个左边的dxBar菜单例子:

procedure TMain.InitMenuItem(dxNavBar1:TdxNavBar);
var
iAction: Integer;
aTag:string;
sMenu:string; dxNavBarGroup:TdxNavBarGroup;
dxNavBarItem:TdxNavBarItem;
dxNavBarLink:TdxNavBarItemLink;
dxNavBarSeparator:TdxNavBarSeparator;
cds_Master,cds_Delta:TClientDataSet; procedure InitdxNavBar;
begin
dxNavBar1.Groups.Clear;
with dxNavBar1.DefaultStyles do
begin
GroupHeader.Font.Name := '微软雅黑';
GroupHeader.Font.Style := [fsBold];
GroupHeader.Font.Size := 9;
GroupHeader.Font.Color := clBlack; Item.Font.Name := '微软雅黑';
Item.Font.Style := [];
Item.Font.Size := 9;
Item.Font.Color := clBlack; ItemHotTracked.Font.Color := clBlue;
ItemHotTracked.Font.Style := [fsUnderline]; ItemPressed.Font.Color := clBlue;
ItemPressed.Font.Style := [];
end;
end;
procedure OpenMaster;
var
sqlstr:string;
begin
if gb_Czy_Level='-1' then
sqlstr := 'select 项目名称,Id from 菜单项目表 where pId=-1 order by id'
else
sqlstr := 'select 项目名称,Id from view_操作员权限表 where 操作员编号='+
quotedstr(gb_Czy_ID)+' and pid=-1 order by id';
cds_Master.XMLData := dm.OpenData(sqlstr);
end;
procedure OpenDelta(const id:integer);
var
sqlstr:string;
begin
if gb_Czy_Level='-1' then
sqlstr := 'select * from 菜单项目表 where pId='+IntToStr(id)+' and pId<>-1 order by id'
else
sqlstr := 'select * from view_操作员权限表 where 操作员编号='+quotedstr(gb_Czy_ID)+
' and 是否可用=1 and pId='+IntToStr(id)+' and pId<>-1 order by id';
cds_Delta.XMLData := dm.OpenData(sqlstr);
end;
begin
cds_Master := TClientDataSet.Create(nil);
cds_Delta := TClientDataSet.Create(nil);
try
InitdxNavBar;//设置一些格式,清除设计期的数据等
OpenMaster;
while not cds_Master.Eof do
begin
dxNavBarGroup := dxNavBar1.Groups.Add;
dxNavBar1.Hint := cds_Master.FieldByName('项目名称').AsString;
dxNavBarGroup.Caption := Copy(dxNavBar1.Hint,4,100);
dxNavBarGroup.Visible := True;
dxNavBarGroup.Expandable := True;
dxNavBarGroup.Expanded := dxNavBarGroup.Index < 3;
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++// OpenDelta(cds_Master.FieldByName('id').AsInteger); cds_Delta.First;
aTag := cds_Delta.FieldByName('Tag').AsString;
while not cds_Delta.Eof do
begin
sMenu := cds_Delta.FieldByName('项目名称').AsString;
iAction := GetAcitonByCaption(cds_Master.FieldByName('项目名称').AsString,sMenu); if iAction<>-1 then
begin //创建一个dxBarButton,相当于创建一个菜单项
dxNavBarItem := dxNavBar1.Items.Add;
dxNavBarItem.Action := TAction(ActionManger1.Actions[iAction]); dxNavBarGroup := TdxNavBarGroup(dxNavBar1.Groups.ItemByCaption(Copy(ActionManger1.Actions[iAction].Category,4,100)));
dxNavBarLink := dxNavBarGroup.CreateLink(dxNavBarItem);
dxNavBarGroup.LinksUseSmallImages := True;
//==================加分隔线,但是不成功,不知道为何?==============//
if (cds_Delta.FieldByName('Tag').AsString<>aTag) then
begin
aTag := cds_Delta.FieldByName('Tag').AsString;
if (cds_Delta.RecNo<cds_Delta.RecordCount) then
begin
//dxNavBarSeparator := TdxNavBarSeparator(dxNavBar1.Items.Add);//
//dxNavBarSeparator.Name := 'dxNavBarSeparator1';
//dxNavBarLink := dxNavBarGroup.CreateLink(dxNavBarSeparator);
//dxNavBarLink.DisplayName := '';//
end;
end;
//==================================================================//
end;
cds_Delta.Next;
end; //end while ...
cds_Master.Next;
end;
finally
cds_Delta.Free;
cds_Master.Free;
end;
end;

三、动态生成dxBarManager内容例子(即生成“主菜单”--MainMenu):

procedure TMain.InitMenuItem(dxBarManager1: TdxBarManager);
var
i,iAction: Integer;
iNo,aTag:string;
sName,sMenu,sCategory:string;
dxBarSubItem:TdxBarSubItem;
dxBarButton:TdxBarButton;
dxBarItemLink:TdxBarItemLink;
dxBarSeparator:TdxBarSeparator; //分隔线
cds_Master,cds_Temp:TClientDataSet;
procedure ReleasedxBarManager;
var
i:Integer;
begin
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//
dxBarManager1Bar1.ItemLinks.Clear;
for i := dxBarManager1Bar1.ItemLinks.Count-1 downto 0 do
dxBarManager1Bar1.ItemLinks.Delete(dxBarManager1Bar1.ItemLinks.Count-1);
for i := dxBarManager1.ItemCount-1 downto 0 do
dxBarManager1.Items[i].Free;
for i := dxBarManager1.GroupCount-1 downto 0 do
dxBarManager1.Groups[i].Free; dxBarManager1.Categories.Clear;
dxBarManager1.Categories.Add('default');
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//
end;
procedure OpenMaster;
var
sqlstr:string;
begin
if gb_Czy_Level='-1' then
sqlstr := 'select 项目名称,Id from 菜单项目表 where pId=-1 order by id'
else
sqlstr := 'select 项目名称,Id from view_操作员权限表 where 操作员编号='+
quotedstr(gb_Czy_ID)+' and pid=-1 order by id';
cds_Master.XMLData := dm.OpenData(sqlstr);
end;
procedure OpenDelta(const id:integer);
var
sqlstr:string;
begin
if gb_Czy_Level='-1' then
sqlstr := 'select * from 菜单项目表 where pId='+IntToStr(id)+' and pId<>-1 order by id'
else
sqlstr := 'select * from view_操作员权限表 where 操作员编号='+quotedstr(gb_Czy_ID)+
' and 是否可用=1 and pId='+IntToStr(id)+' and pId<>-1 order by id';
cds_Temp.XMLData := dm.OpenData(sqlstr);
end;
begin
ReleasedxBarManager;//清理dxBarManager
cds_Master := TClientDataSet.Create(nil);
cds_Temp := TClientDataSet.Create(nil);
try
OpenMaster;
while not cds_Master.Eof do
begin
OpenDelta(cds_Master.FieldByName('id').AsInteger);
if cds_Temp.RecordCount>0 then
begin
sCategory := cds_Master.FieldByName('项目名称').AsString;
iNo := Copy(sCategory,1,2);
sName := Format('dxBarSubItem%s',[iNo]);//取出【01.系统设置】中的01数字出来];
sCategory := Copy(sCategory,4,100); //创建一个dxSubItem,相当于创建一个主菜单项
dxBarSubItem := TdxBarSubItem.Create(Self);
dxBarSubItem.Caption := sCategory;
dxBarSubItem.Category := 0;
dxBarSubItem.Visible := ivAlways;//ivNever;//
dxBarSubItem.Name := sName; //放置在dxBar上
dxBarItemLink := dxBarSubItem.BarManager.Bars[0].ItemLinks.Add;
dxBarItemLink.Item := dxBarSubItem;
end;
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++// cds_Temp.First;
aTag := cds_Temp.FieldByName('Tag').AsString;
while not cds_Temp.Eof do
begin
sMenu := cds_Temp.FieldByName('项目名称').AsString;
iAction := GetAcitonByCaption(cds_Master.FieldByName('项目名称').AsString,sMenu); if iAction<>-1 then
begin
if (cds_Temp.FieldByName('Tag').AsString<>aTag) then
begin
aTag := cds_Temp.FieldByName('Tag').AsString;
if (cds_Temp.RecNo<>1) then //and (cds_Temp.RecNo<>cds_Temp.RecordCount) then
begin
dxBarSeparator := TdxBarSeparator.Create(Self);
dxBarSeparator.ShowCaption := False;
dxBarItemLink := dxBarSubItem.ItemLinks.Add;
dxBarItemLink.Item := dxBarSeparator;
end;
end;
//创建一个dxBarButton,相当于创建一个菜单项
dxBarButton := TdxBarButton.Create(Self);//内存资源由Self负责管理
dxBarButton.Caption := sMenu;
dxBarButton.Action := TAction(ActionManger1.Actions[iAction]);
dxBarButton.Category := 0;
dxBarButton.Tag := TAction(ActionManger1.Actions[iAction]).Tag;
//dxBarButton.Name := TAction(ActionManger1.Actions[iAction]).Name;//Format('dxBarButton_%-2d',[ii]); dxBarItemLink := dxBarSubItem.ItemLinks.Add;
dxBarItemLink.Item := dxBarButton;
end;
cds_Temp.Next;
end; //end while ...
cds_Master.Next;
end;
finally
cds_Temp.Free;
cds_Master.Free;
end;
end;

四、在实际项目中的一个截图:

五、那么能否从dxBarManager中导入内容到dxNavBar中呢?可以:

procedure TForm1.dxBarManagerTodxNavBar;
var
i, j: Integer;
item: TdxBarSubItem;
group: TdxNavBarGroup;
bar: TdxNavBarItem;
begin
dxNavBar1.Groups.Clear;
for i := 0 to dxBarManager1Bar1.ItemLinks.Count - 1 do
begin
item := TdxBarSubItem(dxBarManager1Bar1.ItemLinks[i].Control.Item);
group := dxNavBar1.Groups.Add;
group.Caption := item.Caption;
for j := 0 to item.ItemLinks.Count - 1 do
begin
bar := dxNavBar1.Items.Add;
bar.Caption := item.ItemLinks.Items[j].Caption;
bar.OnClick := dxNavBarItemClick;
bar.SmallImageIndex := 0;
group.CreateLink(bar);
end;
end;
end;

版权声明:本文为博主原创文章,未经博主允许不得转载。

使用dxNavBar动态创建应用程序菜单的更多相关文章

  1. MFC 对话框中动态创建N级菜单以及响应事件

    创建一个基于对话框的工程,工程名为CreateMenu 为该对话框增加一个文件菜单项和测试菜单项,如下图所示   测试菜单项至少要有一个子菜单项 在对话框属性中关联该菜单 在resource.h中增加 ...

  2. Delphi动态创建菜单

    在程序运行中动态创建菜单,主要使用TMeunItem类,所有菜单的条目都是TMenuItem的一个实例. 打开Delphi7集成开发环境,在默认新建工程里,放置一个Button1按钮和MainMenu ...

  3. EasyUI创建异步树形菜单和动态添加标签页tab

    创建异步树形菜单 创建树形菜单的ul标签 <ul class="easyui-tree" id="treeMenu"> </ul> 写j ...

  4. tp根据数据库动态创建微信菜单

    喻可伟的博客   tp根据数据库动态创建微信菜单 $button = array();$class = M('ucenter_menu')->limit(3)->select();//取出 ...

  5. Delphi -- 创建 桌面、发送到...、快速启动栏、开始菜单、程序菜单、右键菜 单

    {================================================================= 功 能: 创建 桌面.发送到....快速启动栏.开始菜单.程序菜单 ...

  6. .NET使用Task动态创建多任务多线程并行程序计算Redis集群keys计算

    Task是一个很好用的多任务处理类,并且通过Task可以对任务进行很好的控制. 下面将通过代码实现Redis集群在使用IServer.keys时通过多任务对多个服务器示例进行并行计算,并对返回key做 ...

  7. VC++ 创建一个动态增长的层叠菜单

    工作中需要创建一个动态增长的层叠菜单,类似于动态增长的多语言切换菜单,也是废了好大劲哪,分享一下,请交流参考. 类似效果图: 弹出子菜单各菜单项的意义一致,用ON_COMMAND_RANGE宏来统一实 ...

  8. MFC小程序003------MFC使用WebBrowser组件,在对话框中创建滚动视图,动态创建一个静态文本控件并设置鼠标单击的消息响应

    MFC小程序截图: 一.在MFC中简单使用WebBrowser的ActiveX插件的方法: 见博文:  http://blog.csdn.net/supermanking/article/detail ...

  9. Qt5 UI信号、槽自动连接的控件重名大坑(UI生成的槽函数存在一个隐患,即控件重名。对很复杂的控件,不要在 designer 里做提升,而是等到程序启动后,再动态创建,可以避免很多问题)

    对Qt5稍有熟悉的童鞋都知道信号.槽的自动连接机制.该机制使得qt designer 设计的UI中包含的控件,可以不通过显式connect,直接和cpp中的相应槽相关联.该机制的详细文章见 http: ...

随机推荐

  1. iOS---》点击uitableview 的section展开或隐藏

    #import <UIKit/UIKit.h> @interface TestCell : UITableViewCell @property (weak, nonatomic) IBOu ...

  2. [C.Sharp] Color 颜色转换

    URL:http://www.cnblogs.com/taven/archive/2009/09/26/1574329.html 方法1: 引用命名空间:using System.Drawing; 1 ...

  3. 重构9-Extract Interface(提取接口)

    我们来介绍一个常常被忽视的重构:提取接口.如果你发现多于一个类使用另外一个类的某些方法,引入接口解除这种依赖往往十分有用.该重构实现起来非常简单,并且能够享受到松耦合带来的好处. public cla ...

  4. 使用JSON进行数据传输的总结

    一.选择的意义 在异步应用程序中发送和接收信息时,可以选择以纯文本和 XML 作为数据格式.为了更好的使用ajax, 我们将学习一种有用的数据格式 JavaScript Object Notation ...

  5. 连续区间覆盖染色问题 ------ SHUOJ 1716

    题目链接:http://202.121.199.212/JudgeOnline/problem.php?id=1716 [题意] 1到N的区间,一种操作让编号从a到b的数变为z,但不会低于2,问多次操 ...

  6. 剑指Offer47 不用加减乘除做加法

    /************************************************************************* > File Name: 47_AddTwo ...

  7. [转]移动App测试中的最佳做法

    Daniel Knott 用过各种不同编程语言和软件质量保证工具.他在软件开发和测试方面干了七年,自2010年起,他一直在德国汉堡的XING AG公司就职,几个项目里,比如XING调查和XING建议, ...

  8. BootStraps 布局

    <div class="container"> //创建一个容器 <div class="row">  //创建行,每行总有12个格数 ...

  9. kettle菜鸟学习笔记1----相关准备知识

    最近导师让给师弟师妹做个关于kettle的培训,瞬间囧了,kettle我也只是简单学过,连皮毛都算不上,而且,上次使用kettle已然是去年的事了…… 没办法,只好重新学习下,好在之前写了几个文档,也 ...

  10. DWZ与KindEditor编辑器的整合

    DWZ自带的编辑器是xheditor,可能很多人用不习惯.就像我,习惯用kindeditor了.现在就来说说如何整合dwz和kindeditor. 一.打开DWZ的中的dwz.ui.js,进行修改. ...