经过上二篇的铺垫,现在终于可以加载菜单与工具栏了。先来看一下IService的接口实现里LoadCommand方法是如何来加载菜单与工具栏的:
IService接口的实现
public class SignInRunner: IService
{
……
……
private MenuCfg menus;
private ToolBarCfg toolbars;
//……
//……
public SignInRunner ()
{
//……
//……
string cfgPath = Assembly.GetExecutingAssembly().Location + ".config";
ConfigReader reader = new ConfigReader(cfgPath);
menus = reader.GetConfig("Menu") as MenuCfg;
toolbars = reader.GetConfig("ToolBar") as ToolBarCfg;
}
IService 成员#region IService 成员
// ……
public void LoadCommand()
{
try
{
if (menus == null || menus.Items.Count == 0) return;
CommandMenu[] cmdMenu = CommandUtility.GetMenus(menus.Items,Context.OwnerPanel);
Context.OwnerMenu.MenuItems.AddRange(cmdMenu);
if (toolbars == null || toolbars.Items.Count == 0) return;
ToolBarButton sp = new ToolBarButton();
sp.Style = ToolBarButtonStyle.Separator;
Context.OwnerTool.Buttons.Add(sp);
Context.OwnerTool.Buttons.AddRange(CommandUtility.GetButtons(toolbars.Items,cmdMenu,Context.OwnerTool.ImageList));
}
catch(Exception e)
{
logNet.Error("加载[" + Name + "]的菜单与工具栏时出错--" + e.Message);
}
}
//……
//……
//……
#endregion
}
public class SignInRunner: IService
{
……
……
private MenuCfg menus;
private ToolBarCfg toolbars;
//……
//……
public SignInRunner ()
{
//……
//……
string cfgPath = Assembly.GetExecutingAssembly().Location + ".config";
ConfigReader reader = new ConfigReader(cfgPath);
menus = reader.GetConfig("Menu") as MenuCfg;
toolbars = reader.GetConfig("ToolBar") as ToolBarCfg;
}
IService 成员#region IService 成员
// ……
public void LoadCommand()
{
try
{
if (menus == null || menus.Items.Count == 0) return;
CommandMenu[] cmdMenu = CommandUtility.GetMenus(menus.Items,Context.OwnerPanel);
Context.OwnerMenu.MenuItems.AddRange(cmdMenu);
if (toolbars == null || toolbars.Items.Count == 0) return;
ToolBarButton sp = new ToolBarButton();
sp.Style = ToolBarButtonStyle.Separator;
Context.OwnerTool.Buttons.Add(sp);
Context.OwnerTool.Buttons.AddRange(CommandUtility.GetButtons(toolbars.Items,cmdMenu,Context.OwnerTool.ImageList));
}
catch(Exception e)
{
logNet.Error("加载[" + Name + "]的菜单与工具栏时出错--" + e.Message);
}
}
//……
//……
//……
#endregion
}
上述第一步先从模块的配置文件里读取到菜单与工具栏,得到相应的配置类;再把配置类转化为上二篇的带Command模式的菜单项(或工具栏按钮)类的数组;最后把数组加入到主窗体的菜单项集合(或工具栏按钮集合)中。
那如何把配置类转为菜单项(或工具栏按钮)类数组的呢?其实那个配置类本身也就是一个层层叠加的集合,每一项的属性跟菜单项(或工具栏按钮)的属性有对应,转化过程就是层层属性转化的过程,唯一另外要做的一件事就是把相应的单击事件附加上去。
具体过程见下面代码:
从配置到菜单(工具栏)
public sealed class CommandUtility {
private const string ICONPATH = "Icons";
private static EventHandler evh;
private static log4net.ILog logNet;
private static void CommandClick(object sender,System.EventArgs e)
{
ICommand cmd = (sender as ICommandHolder).GetCommand();
cmd.Execute();
}
static CommandUtility() {
evh = new EventHandler(CommandClick);
logNet = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
}
/**//// <summary>
/// 根据配置文件菜单列表得到实际菜单数组
/// </summary>
/// <param name="itemList"></param>
/// <param name="panel"></param>
/// <returns></returns>
public static CommandMenu[] GetMenus(MenuItemList itemList,WeifenLuo.WinFormsUI.DockPanel panel){
int length = itemList.Count;
CommandMenu[] ret = new CommandMenu[length];
MenuItemCfg item;
ICommand command;
for (int i = 0;i < length;i++){
item = itemList[i];
if (item.Key != null && item.Key.Length > 0)
ret[i] = new CommandMenu(item.Caption + "(&" + item.Key + ")");
else
ret[i] = new CommandMenu(item.Caption);
ret[i].ID = item.ID;
ret[i].Enabled = !item.Disable;
if (item.Form != null && item.Form.Length > 0)
{
if (item.Method != null && item.Method.Length > 0)
command = new ExecuteCommand(item.ID,item.Form,panel.FindForm(),item.Method);
else
command = new ShowFormCommand(item.ID,item.Form,panel,item.IsModal);
ret[i].SetCommand(command);
ret[i].Click += evh;
}
if (item.Items.Count > 0){
ret[i].MenuItems.AddRange(GetMenus(item.Items,panel));
}
}
return ret;
}
/**//// <summary>
/// 根据配置文件工具栏按钮列表得到实际按钮数组
/// </summary>
/// <param name="itemList"></param>
/// <param name="menus"></param>
/// <param name="imgList"></param>
/// <returns></returns>
public static ToolBarButton[] GetButtons(TBarItemList itemList,CommandMenu[] menus,ImageList imgList){
int length = itemList.Count;
CommandToolBarButton[] ret = new CommandToolBarButton[length];
ToolBarItemCfg item;
ICommand cmd;
for (int i = 0;i < length;i++){
item = itemList[i];
ret[i] = new CommandToolBarButton(item.Caption);
ret[i].ID = item.ID;
if (item.Caption == "|")
ret[i].Style = ToolBarButtonStyle.Separator;
else{
ret[i].Enabled = !item.Disable;
if (item.Image != null && item.Image.Length > 0)
{
imgList.Images.Add(new Icon(GetToolBarButtonIcon(item.Image)));
ret[i].ImageIndex = imgList.Images.Count - 1;
}
if (item.RelateMenuID != null && item.RelateMenuID.Length > 0)
{
cmd = getRelateMenu(item.RelateMenuID,menus);
if (cmd == null)
{
continue;
}
ret[i].SetCommand(cmd);
ret[i].OnClick += evh;
}
if (item.Items.Count > 0) {
ret[i].Style = ToolBarButtonStyle.DropDownButton;
ContextMenu dpMenu = new ContextMenu();
dpMenu.MenuItems.AddRange(getContextMenu(item.Items,menus));
ret[i].DropDownMenu = dpMenu;
}
}
}
return ret;
}
/**//// <summary>
/// 取得按钮的下拉菜单
/// </summary>
/// <param name="itemList"></param>
/// <param name="menus"></param>
/// <returns></returns>
private static CommandMenu[] getContextMenu(TBarItemList itemList,CommandMenu[] menus){
int length = itemList.Count;
CommandMenu[] ret = new CommandMenu[length];
ToolBarItemCfg item;
ICommand cmd;
for (int i = 0;i < length;i++){
item = itemList[i];
ret[i] = new CommandMenu(item.Caption);
ret[i].Enabled = !item.Disable;
if (item.RelateMenuID != null && item.RelateMenuID.Length > 0)
{
cmd = getRelateMenu(item.RelateMenuID,menus);
if (cmd == null)
{
continue;
}
ret[i].SetCommand(cmd);
ret[i].Click += evh;
}
if (item.Items.Count > 0){
ret[i].MenuItems.AddRange(getContextMenu(item.Items,menus));
}
}
return ret;
}
/**//// <summary>
/// 取得工具栏按钮对应的菜单项的ICommand
/// </summary>
/// <param name="id">菜单ID</param>
/// <param name="menus">在其中查找菜单项</param>
/// <returns></returns>
private static ICommand getRelateMenu(string id,CommandMenu[] menus){
ICommand cmd;
for (int i = 0;i < menus.Length;i++){
if (menus[i].MenuItems.Count > 0){
CommandMenu[] children = new CommandMenu[menus[i].MenuItems.Count];
menus[i].MenuItems.CopyTo(children,0);
cmd = getRelateMenu(id,children);
if (cmd == null)
continue;
else
return cmd;
}else{
cmd = menus[i].GetCommand();
if (cmd != null && id == cmd.ID)
return cmd;
}
}
return null;
}
public static string GetToolBarButtonIcon(string fileName)
{
return System.IO.Path.Combine(ICONPATH,fileName);
}
}
public sealed class CommandUtility {
private const string ICONPATH = "Icons";
private static EventHandler evh;
private static log4net.ILog logNet;
private static void CommandClick(object sender,System.EventArgs e)
{
ICommand cmd = (sender as ICommandHolder).GetCommand();
cmd.Execute();
}
static CommandUtility() {
evh = new EventHandler(CommandClick);
logNet = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
}
/**//// <summary>
/// 根据配置文件菜单列表得到实际菜单数组
/// </summary>
/// <param name="itemList"></param>
/// <param name="panel"></param>
/// <returns></returns>
public static CommandMenu[] GetMenus(MenuItemList itemList,WeifenLuo.WinFormsUI.DockPanel panel){
int length = itemList.Count;
CommandMenu[] ret = new CommandMenu[length];
MenuItemCfg item;
ICommand command;
for (int i = 0;i < length;i++){
item = itemList[i];
if (item.Key != null && item.Key.Length > 0)
ret[i] = new CommandMenu(item.Caption + "(&" + item.Key + ")");
else
ret[i] = new CommandMenu(item.Caption);
ret[i].ID = item.ID;
ret[i].Enabled = !item.Disable;
if (item.Form != null && item.Form.Length > 0)
{
if (item.Method != null && item.Method.Length > 0)
command = new ExecuteCommand(item.ID,item.Form,panel.FindForm(),item.Method);
else
command = new ShowFormCommand(item.ID,item.Form,panel,item.IsModal);
ret[i].SetCommand(command);
ret[i].Click += evh;
}
if (item.Items.Count > 0){
ret[i].MenuItems.AddRange(GetMenus(item.Items,panel));
}
}
return ret;
}
/**//// <summary>
/// 根据配置文件工具栏按钮列表得到实际按钮数组
/// </summary>
/// <param name="itemList"></param>
/// <param name="menus"></param>
/// <param name="imgList"></param>
/// <returns></returns>
public static ToolBarButton[] GetButtons(TBarItemList itemList,CommandMenu[] menus,ImageList imgList){
int length = itemList.Count;
CommandToolBarButton[] ret = new CommandToolBarButton[length];
ToolBarItemCfg item;
ICommand cmd;
for (int i = 0;i < length;i++){
item = itemList[i];
ret[i] = new CommandToolBarButton(item.Caption);
ret[i].ID = item.ID;
if (item.Caption == "|")
ret[i].Style = ToolBarButtonStyle.Separator;
else{
ret[i].Enabled = !item.Disable;
if (item.Image != null && item.Image.Length > 0)
{
imgList.Images.Add(new Icon(GetToolBarButtonIcon(item.Image)));
ret[i].ImageIndex = imgList.Images.Count - 1;
}
if (item.RelateMenuID != null && item.RelateMenuID.Length > 0)
{
cmd = getRelateMenu(item.RelateMenuID,menus);
if (cmd == null)
{
continue;
}
ret[i].SetCommand(cmd);
ret[i].OnClick += evh;
}
if (item.Items.Count > 0) {
ret[i].Style = ToolBarButtonStyle.DropDownButton;
ContextMenu dpMenu = new ContextMenu();
dpMenu.MenuItems.AddRange(getContextMenu(item.Items,menus));
ret[i].DropDownMenu = dpMenu;
}
}
}
return ret;
}
/**//// <summary>
/// 取得按钮的下拉菜单
/// </summary>
/// <param name="itemList"></param>
/// <param name="menus"></param>
/// <returns></returns>
private static CommandMenu[] getContextMenu(TBarItemList itemList,CommandMenu[] menus){
int length = itemList.Count;
CommandMenu[] ret = new CommandMenu[length];
ToolBarItemCfg item;
ICommand cmd;
for (int i = 0;i < length;i++){
item = itemList[i];
ret[i] = new CommandMenu(item.Caption);
ret[i].Enabled = !item.Disable;
if (item.RelateMenuID != null && item.RelateMenuID.Length > 0)
{
cmd = getRelateMenu(item.RelateMenuID,menus);
if (cmd == null)
{
continue;
}
ret[i].SetCommand(cmd);
ret[i].Click += evh;
}
if (item.Items.Count > 0){
ret[i].MenuItems.AddRange(getContextMenu(item.Items,menus));
}
}
return ret;
}
/**//// <summary>
/// 取得工具栏按钮对应的菜单项的ICommand
/// </summary>
/// <param name="id">菜单ID</param>
/// <param name="menus">在其中查找菜单项</param>
/// <returns></returns>
private static ICommand getRelateMenu(string id,CommandMenu[] menus){
ICommand cmd;
for (int i = 0;i < menus.Length;i++){
if (menus[i].MenuItems.Count > 0){
CommandMenu[] children = new CommandMenu[menus[i].MenuItems.Count];
menus[i].MenuItems.CopyTo(children,0);
cmd = getRelateMenu(id,children);
if (cmd == null)
continue;
else
return cmd;
}else{
cmd = menus[i].GetCommand();
if (cmd != null && id == cmd.ID)
return cmd;
}
}
return null;
}
public static string GetToolBarButtonIcon(string fileName)
{
return System.IO.Path.Combine(ICONPATH,fileName);
}
}
注意这里的设定,工具栏按钮(及其带的下拉菜单)肯定是要有相应的主菜单菜单项对应的,换言之就是不存在孤立的工具栏按钮,否则此按钮(及其带的下拉菜单)是无效的。
上面是模块的菜单与工具栏加载,主窗体的菜单与工具栏的加载当然与此类同,只是那里除此之外,还有一些固定的菜单与工具栏,比如“退出”、“帮助”、“关于”等等,无论需求如何变,这些东西总归是要的,我就直接在主程序的菜单与工具栏里把这些项的内容做死了,用不着经过配置。但顺序没有做死,比如“帮助”这样的菜单(或工具栏)总是加载到最后一项的。