传统界面设计(不同于ribbon):BarManager管理菜单栏、工具栏和状态栏的管理器

来源:互联网 发布:php从入门到放弃 编辑:程序博客网 时间:2024/06/05 20:14
Namespace:DevExpress.XtraBars

Assembly:DevExpress.XtraBars.v14.1.dll

 BarManager 是 XtraBars Library.最重要元素。 It is a container for bar items, categories and bars. For every form you want to place bars on, you should place a BarManager component .

要在运行时激活自定义(工具管理)窗口  ,调用Customize 方法.

BarManager 提供保存和恢复工具布局到注册表的辅助工具. 保存布局, 调用 SaveToRegistry 方法, and  RestoreFromRegistry 恢复布局. 在保存和加载之前,需指定注册表路径( RegistryPath 属性).

XtraBars 引入 BarAndDockingController 来保存程序总工具条的自定义和外观设置.BarAndDockingController 提供默认设置 . 也就是说这些设置都可以被独立设置覆盖。

 BarAndDockingController 允许你实现 centralized control over the bars settings. 默认所有工具条通过BarAndDockingController得到设置. 获取和定制默认控制器通过静态 BarAndDockingController.Default 属性或通过DefaultBarAndDockingController 组件.

If you need to provide different settings for bars residing within specific forms, as compared to bars within other forms, you need to create new BarAndDockingController objects and assign them to the Controller properties of the required BarManagers. In this case, bars will get their settings from this controller and not from the default controller. The GetController function returns the actual controller providing a bar's settings.

弹出菜单(扩展控件)

设计时拖放 BarManager 到窗体,所有控件公开 PopupContextMenu 扩展属性 (属性窗口中的标题像 'PopupContextMenu on barManager1')一样. 可以使用此属性给控件关联PopupMenu orRadialMenu . 右击时将显示该菜单

代码关联弹出菜单, 调用 SetPopupContextMenu 方法.

可以在弹出菜单显示前定制菜单,通过处理 QueryShowPopupMenu 事件

NoteNote

某些特定复杂的控件such as XtraGrid and XtraTreeList 不公开 PopupContextMenu 属性, as these controls support different context menus for different visual elements. See the help documentation on these products to learn how to work with context menus in these controls.

Notes

NoteNote

不允许添加两个或多个 BarManager 组件到同一窗体或用户控件.

NoteNote

不推荐常规工具条 ( BarManager 组件) 和 RibbonControl 在同一窗体或用户控件中同时使用, 可能会冲突. 

Examples

 示例1 :个人实践

新建窗体项目,拖动BarManager到窗体设计器,会自动显示菜单栏和工具栏和状态栏(都是XtraBars.Bar),默认name工具栏bar1,BarName=Tools,菜单栏bar2,BarName=Main Menu,状态栏bar3,BarName=Statusbar。

BarManager属性里只可以看到MainMenu和StatusBar。

因为每个Form仅有一个主菜单和状态栏,而ToolBar可以有多个,即自定义工具。(CustomBar)

 How to obtain a value of the BarEditItem during editing

Object  value=(barManager1.ActiveEditor as DevExpress.XtraEditors.ButtonEdit).EditValue;



工具栏通过Bars集合添加. 工具栏项通过 Bar.AddItem and Bar.AddItems 方法添加。

避免添加和定制工具栏和功能项时闪烁,处理定制的代码通过, BeginUpdate and EndUpdate 方法调用进行闭合操作。

添加子菜单通过单击,并选择添加Menu(BarSubItem),命名subMenuFile、subMenuEdit、subMenu+名称

添加菜单项通过选择具体的功能控件就OK,命名 空间类型+(子菜单名)+功能名,如buttonOpen、buttonViewOutput。

通过设计器将同一个功能项同时指向菜单和工具栏没找到?????


示例2:

官方文档:示例展示如何通过代码创建工具条和工具条选项。

创建两个工具条-主菜单(工具条扩展到匹配窗口宽度)和常规工具条. 主菜单包含三个子菜单 (File, Edit and View), 每个有自己的功能项. 第二个工具条为工具栏,显示 Output 按钮, Output项也可以通过主菜单的View视图子菜单获取:

工具栏通过Bars集合添加. 工具栏项通过 Bar.AddItem and Bar.AddItems 方法添加。

避免添加和定制工具栏和功能项时闪烁,处理定制的代码通过, BeginUpdate and EndUpdate 方法调用进行闭合操作。

处理功能项的单击事件

  private void btnViewOutput_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)        {           //处理某一项        }// 放在barManager的ItemClick处理,可以集中处理所有的        private void barManager1_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)        {            DevExpress.XtraBars.BarSubItem subMenu = e.Item as DevExpress.XtraBars.BarSubItem;            if (subMenu != null) return;            MessageBox.Show("Item '" + e.Item.Caption + "' has been clicked");        }



C#
VB
using DevExpress.XtraBars;private void Form1_Load(object sender, EventArgs e) {    BarManager barManager = new BarManager();    barManager.Form = this;//指定所属窗体    // Prevent excessive updates while adding and customizing bars and bar items.    // The BeginUpdate must match the EndUpdate method.    barManager.BeginUpdate();    // Create two bars and dock them to the top of the form.    // Bar1 - is a main menu, which is stretched to match the form's width.    // Bar2 - is a regular bar.    Bar bar1 = new Bar(barManager, "My MainMenu");    Bar bar2 = new Bar(barManager, "My Bar");    bar1.DockStyle = BarDockStyle.Top;    bar2.DockStyle = BarDockStyle.Top;    // Position the bar1 above the bar2    bar1.DockRow = 0;    // The bar1 must act as the main menu.    barManager.MainMenu = bar1;    // Create bar items for the bar1 and bar2    BarSubItem subMenuFile = new BarSubItem(barManager, "File");    BarSubItem subMenuEdit = new BarSubItem(barManager, "Edit");    BarSubItem subMenuView = new BarSubItem(barManager, "View");    BarButtonItem buttonOpen = new BarButtonItem(barManager, "Open");    BarButtonItem buttonExit = new BarButtonItem(barManager, "Exit");    BarButtonItem buttonCopy = new BarButtonItem(barManager, "Copy");    BarButtonItem buttonCut = new BarButtonItem(barManager, "Cut");    BarButtonItem buttonViewOutput = new BarButtonItem(barManager, "Output");    subMenuFile.AddItems(new BarItem[] { buttonOpen, buttonExit});    subMenuEdit.AddItems(new BarItem[] { buttonCopy, buttonCut});    subMenuView.AddItem(buttonViewOutput);        //Add the sub-menus to the bar1    bar1.AddItems(new BarItem[] {subMenuFile, subMenuEdit, subMenuView });    // Add the buttonViewOutput to the bar2.    bar2.AddItem(buttonViewOutput);    // A handler to process clicks on bar items    barManager.ItemClick += new ItemClickEventHandler(barManager_ItemClick);         barManager.EndUpdate();}void barManager_ItemClick(object sender, ItemClickEventArgs e) {    BarSubItem subMenu = e.Item as BarSubItem;    if (subMenu != null) return;    MessageBox.Show("Item '" + e.Item.Caption + "' has been clicked");}
Inheritance Hierarchy

System.Object
    System.MarshalByRefObject
       System.ComponentModel.Component
          ComponentEditorContainer
             BarManager
                PrintBarManager
                DevExpress.XtraBars.Ribbon.RibbonBarManager

See Also
BarManager Members
DevExpress.XtraBars Namespace
Bar 
BarItem 
BarItemLink 
BarAndDockingController 
Bar Manager 
Bar and Docking Controller

0 0
原创粉丝点击