关闭

dxbarmanager菜单增加项目

2483人阅读 评论(0) 收藏 举报

---无代码下载
{

经常上盒子查找需要的资料,今天给大家发一点有用的代码

相信好多人都用过dxbarmanager的菜单,但要动态增加项目可能会感觉不知所然!

以下代码在D7测试通过

bigwell

}
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls, dxsbar, ImgList, dxBar;
type
  TForm1 = class(TForm)
    SideBar: TdxSideBar;
    ImageList1: TImageList;
    BarManager: TdxBarManager;
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  private
    { Private declarations }
    //side bar click proc
    procedure OnBarItemClick(Sender: TObject;Item: TdxSideBarItem);
    //menu item click proc
    procedure OnMenuItemClick(Sender: TObject);
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}


procedure TForm1.FormCreate(Sender: TObject);
var
i,c:integer;
g:TdxSideGroup;
it:TdxSideBarItem;

  ACategoryName: string;
  ACategory: Integer;
  AItemLinks: TdxBarItemLinks;

    AItem, AChildItem: TdxBarItem;
    AItemLink: TdxBarItemLink;

begin
  //创建MainMenu
  with BarManager.Bars.Add do
  begin
    Caption := 'Main Menu';
    DockingStyle := dsTop;
    IsMainMenu := True;
    Visible := True;

  end;
  //获取Mainmenubar.itemlins
  AItemLinks := BarManager.MainMenuBar.ItemLinks;
  ACategoryName := 'Menus';
  BarManager.MainMenuBar.LockUpdate := True;
  //Get Category index
  with BarManager.Categories do
  begin
    if IndexOf(ACategoryName) < 0 then Add(ACategoryName);
    ACategory := IndexOf(ACategoryName);
  end;

 

  for i:=1 to 4 do
   with SideBar do
   begin
      //创建组
      g:=Groups.Add;
      with g do
      begin
        Index:=i-1;
        Caption:=Format('Group%d',[i]);
        IconType := dxsgSmallIcon;
      end;
      //创建主菜单单项目
      AItem := TdxBarSubItem.Create(Form1);
      with AItem do
      begin
        Category := ACategory;
        Caption := g.Caption;
      end;
      //添加到主菜单
      AItemLink:=AItemLinks.Add;
      AItemLink.Item:=AItem;

      //创建组的子项目
      for c:=1 to 5 do
      begin
        it:=g.Items.Add;
        with it do
        begin
          Index:=c-1;
          Caption:=Format('Group%d - Item %d',[i,c]);
          SmallImage:=0;
          //comm item click proc
          OnItemClick:=OnBarItemClick;
          //或使用action
          //Action:=act_XXX;

        end;
        //创建子菜单
        AChildItem:=TdxBarButton.Create(form1);
        with AChildItem do
        begin
          caption:=it.Caption;
          Visible:=ivAlways;
          OnClick:=OnMenuItemClick;
          ImageIndex:=0;
          //或使用action
          //Action:=act_XXX;
        end;
        //添加子菜单到主菜单
        AItemLink := TdxBarSubItem(AItem).ItemLinks.Add;
        AItemLink.Item := AChildItem;
      end;
   end;
    with BarManager.Categories do
      Move(ACategory, Count - 1);
   BarManager.MainMenuBar.LockUpdate := False;

end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  SideBar.Groups.Clear;
  BarManager.MainMenuBar.ItemLinks.Clear;
end;

procedure TForm1.OnBarItemClick(Sender: TObject; Item: TdxSideBarItem);
begin
  showmessage(item.Caption);
end;

procedure TForm1.OnMenuItemClick(Sender: TObject);
begin
    showmessage((sender as TdxBarButton).Caption);
end;

end.
 

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:69821次
    • 积分:1393
    • 等级:
    • 排名:千里之外
    • 原创:73篇
    • 转载:5篇
    • 译文:0篇
    • 评论:1条
    最新评论
  • FastReport的使用

    highfiresun: 楼主威武,写的太好了。我有个问题还想请教:我怎么调用ExportTo导出各种文件?另外,能否加我Q探...