/// <summary> /// 根据节点列表创建菜单项,有子节点的亦将创建为子菜单项 /// </summary> static void BuildMenuItems(ToolStripMenuItem parentMenuItem, XmlNodeList nodelist, string inputToolbarOwner) { if (inputToolbarOwner != null && ToolbarManager.ShoudAddSeparator(inputToolbarOwner)) { ToolbarManager.AddToolStripItem(inputToolbarOwner, new ToolStripSeparator()); } foreach (XmlNode node in nodelist) { if (node.NodeType == XmlNodeType.Element) { //先处理分隔线 if (string.Compare(node.Name, "Separator", true) == 0) { //从父菜单项中添加此菜单项 parentMenuItem.DropDownItems.Add(new ToolStripSeparator()); continue; } //存储Menu.xml中每项的数据 string strText = null; string strHotkey = null; Keys shortcut = Keys.None; string strShortcut = null; bool blMore = false; string icoPath = null; string recent = null; bool isToolbar = false; bool isExpand = false; string strToolbarOwner = null; string strToolbarType = null; string strLink = null; bool isMustOpenSite = false; foreach (XmlAttribute att in node.Attributes) { switch (att.Name.ToLower()) { case "text": strText = att.Value; break; case "hotkey": strHotkey = string.IsNullOrEmpty(att.Value) ? "" : "(&" + att.Value + ")"; break; case "shortcut": shortcut = ParseShortcut(att.Value); strShortcut = att.Value; break; case "more": blMore = (att.Value == "1"); break; case "ico": icoPath = att.Value; break; case "istoolbar": isToolbar = (att.Value == "1"); break; case "toolbarowner": strToolbarOwner = att.Value; break; case "toolbartype": strToolbarType = att.Value; break; case "link": strLink = att.Value; break; case "expand": isExpand = (att.Value == "1"); break; case "recent": recent = att.Value; break; case "ismustopensite": isMustOpenSite = bool.Parse(att.Value); break; default: throw new Exception("未知的类型:" + att.Name); } } //作为Key值,如:MainMenu.file.open string keyId = GetKeyFullName(node); MyMenuItem item = null; if (isExpand) { switch (keyId) { case "MainMenu.page.expandInsert": item = new ExpandInsertJsMenuItem(strText + strHotkey + (blMore ? "..." : ""), keyId); break; case "MainMenu.file.recentProj": item = new MyMenuItem(strText + strHotkey + (blMore ? "..." : ""), keyId); break; case "MainMenu.page.expandInsertHtml": item = new ExpandInserHtmlMenuItem(strText + strHotkey + (blMore ? "..." : ""), keyId); break; default: throw new Exception("expand项中出现未知的菜单项:" + keyId); } } else if (strLink != null) { //带link项的,表示用浏览器打开一个网页或文件 item = new LinkMenuItem(strText + strHotkey + (blMore ? "..." : ""), keyId, strLink); } else if (recent != null) { item = new RecentRecordMenuItem(strText + strHotkey + (blMore ? "..." : ""), keyId, recent); } else { item = new MyMenuItem(strText + strHotkey + (blMore ? "..." : ""), keyId, isMustOpenSite); } item.ShortcutKeys = shortcut; if (isToolbar) { item.ImageKey = keyId; } if (item.KeyId == "MainMenu.view.toolBar") { ToolbarManager.ContextMenuItemsCreated += delegate { item.DropDownItems.AddRange(ToolbarManager.ContextMenuItems); }; } //构造工具栏按钮 ToolStripItem toolbarItem = null; if (isToolbar) { toolbarItem = new ToolStripButton(); toolbarItem.ImageKey = keyId; toolbarItem.ToolTipText = strText + (shortcut == Keys.None ? "" : " (" + strShortcut + ")"); toolbarItem.Tag = item; toolbarItem.Click += delegate(object sender, EventArgs e) { //将Toolbar按钮的点击映射到MenuItem中 ((sender as ToolStripItem).Tag as ToolStripMenuItem).PerformClick(); }; item.ToolStripItem = toolbarItem; ToolbarManager.AddToolStripItem(inputToolbarOwner, toolbarItem); } if (parentMenuItem == null) { //添加到1级菜单项的容器中 _peakMenuItem.Add(node.Name, item); } else { //从父菜单项中添加此菜单项 parentMenuItem.DropDownItems.Add(item); ///设置ImageList,以显示出图标 item.Owner.ImageList = ResourceService.MainImageList; } //添加到所有菜单项的容器中 _allMenuItem.Add(keyId, item); //添加子菜单项 if (node.HasChildNodes) { BuildMenuItems(item, node.ChildNodes, inputToolbarOwner == null ? strToolbarOwner : inputToolbarOwner); //if (parentMenuItem == null) //{ // ToolbarManager.AddToolStripItem(strToolbarOwner, new ToolStripSeparator()); //} } else if (item.GetType() == typeof(MyMenuItem) && item.KeyId != "MainMenu.view.toolBar") { item.Click += AllMenuClick; } } } }
/// <summary> /// 初始化 /// </summary> public static void Initialize(string sdsite) { System.Windows.Forms.Application.ThreadException += new ThreadExceptionEventHandler(Application_ThreadException); AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException); _willOpenFile = sdsite; CssResources.Initialize(); //初始化的顺序是有一定规则的 Service.Util.Initialize(); Service.Property.Initialize(); PathService.Initialize(Application.StartupPath); Utility.Pinyin.Initialize(Path.Combine(PathService.SoftwarePath, "pinyin.mb")); ResourceService.Initialize(); StringParserService.Initialize(ResourceService.GetResourceText); SoftwareOption.Load(); ResourcesReader.InitializeResources("Configuration", SoftwareOption.General.ApplicationLanguage, null); Service.FileBinding.Initialize(); Service.DesignData.Load(PathService.Config_PadLayout); Service.RecentFiles.Initialize(); Service.SiteDataManager.Initialize(); Service.ListView.InitColumn(); SiteResourceService.Initialize(SelectResource, GetResourcePath, GetResourceAbsPath, GetResourceUrl, ImportResourceFile); AutoLayoutPanel.Initialize(PathService.CL_DataSources_Folder); LayoutConfiguration.LoadLayoutConfiguration(); _mainForm = new WorkbenchForm(); CssUtility.Initialize(_mainForm.SelectImageResource); ////初始化的顺序是有一定规则的 Service.User.Initialize(_mainForm.ShowLoginForm); Service.WebView.Initialize(_mainForm.SinglePagePublish); MessageService.Initialize(_mainForm); ToolbarManager.Initialize(_mainForm); MenuStripManager.Initialize(_mainForm); StatusBarManager.Initialize(_mainForm); FindAndReplaceForm.Initialize(_mainForm); //ResultsPad.Initialize(_mainForm); MenuStateManager.Initialize(); Service.StatusBar.Initialize(StatusBarManager.CurrentStatusStrip); Service.Workbench.Initialize( _mainForm, _mainForm.OpenWorkDocument, _mainForm.NavigationUrl, _mainForm.MainTreePad.TreeViewExPad.RefreshSiteTreeData, ShowDialogForCreateProject, ShowDialogForOpenProject, _mainForm.GotoTree, _mainForm.ActivateForm); ///监听项目的打开事件 Service.Project.ProjectOpened += new EventHandler(ProjectService_ProjectOpened); Service.Project.ProjectClosing += new EventHandler(ProjectService_ProjectClosing); Service.Project.ProjectClosed += new EventHandler(ProjectService_ProjectClosed); OnWorkbenchCreated(); _mainForm.OnActiveWorkspaceTypeChanged(new WorkspaceTypeEventArgs(WorkspaceType.Default)); _mainForm.MainDockPanel.ActiveDocumentChanged += new EventHandler(_mainForm.MainDockPanel_ActiveDocumentChanged); if (Initialized != null) { Initialized(null, EventArgs.Empty); } }