using DevExpress.XtraBars; using DevExpress.XtraBars.Navigation; using DevExpress.XtraTab; using GsDxApp.Properties; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.IO; using System.Linq; using System.Reflection; using System.Resources; using System.Text; using System.Windows.Forms; using Gs.DevApp.ToolBox; namespace Gs.DevApp.DevFrm { public partial class FrmMain : DevExpress.XtraBars.FluentDesignSystem.FluentDesignForm { private static Dictionary TabPageDic = new Dictionary(); //private StatusStrip statusStrip; //private ToolStripStatusLabel statusLabel; public FrmMain() { InitializeComponent(); getInit(); } private void getInit() { string filePath = @"D:\GsMes\GsDXApp\GsDxApp\bin\Debug\menu.txt"; string _strJson = File.ReadAllText(filePath); var _obj = new { rtnCode = 1, rtnData = new { list = new[] { new { guid = "", name = "", pah= "", ico = "", list = new[] { new { guid = "", name = "", pah= "", ico = "" } } } } }, rtnMsg = "" }; System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(FrmMain)); _obj = Newtonsoft.Json.JsonConvert.DeserializeAnonymousType(_strJson, _obj); foreach (dynamic _dy in _obj.rtnData.list) { AccordionControlElement _grp = this.acrd.AddGroup(); _grp.ImageOptions.Image = Utility.GetImgFromResource(_dy.ico); _grp.Name = Guid.NewGuid().ToString(); _grp.Style = DevExpress.XtraBars.Navigation.ElementStyle.Group; _grp.Text = _dy.name; _grp.Click += _grp_Click; foreach (dynamic _dy2 in _dy.list) { AccordionControlElement _itm = this.acrd.AddItem(); _itm.Name = Guid.NewGuid().ToString(); _itm.ImageOptions.Image =Utility.GetImgFromResource(_dy2.ico); _itm.Style = DevExpress.XtraBars.Navigation.ElementStyle.Item; _itm.Text = _dy2.name; _itm.Appearance.Normal.Font = new System.Drawing.Font("Tahoma", 10F); _itm.Tag = _dy2.pah; _itm.Click += _grp_Click; _grp.Elements.Add(_itm); }; } } private void _grp_Click(object sender, EventArgs e) { AccordionControlElement _acd = (AccordionControlElement)sender; if (_acd.Elements.Count > 0) return; string tabPageName = _acd.Name + "_page"; string tabText = _acd.Text; //string newFormName = "GsDxApp.test.ToolbarForm1"; string newFormName = _acd.Tag.ToString(); AddTabpage(tab, tabPageName, tabText, newFormName); } /// /// 新增选项卡页 /// /// 选项卡控件 /// 当期选项卡页name名称 /// 当前选项卡页Text标题 /// 当前选项卡中的新窗体 public void AddTabpage(XtraTabControl tabControl, string tabPageName, string tabText, string newFormName) { if (IsTabpageExsit(tabControl, tabPageName)) { return; } XtraTabPage newPage = new XtraTabPage(); newPage.Name = tabPageName; newPage.Text = tabText; newPage.Tooltip = (tabText + ":" + tabPageName); newPage.Controls.Add(AddNewForm(newFormName)); tabControl.TabPages.Add(newPage); TabPageDic.Add(tabPageName, newPage); tabControl.SelectedTabPage = newPage; } /// /// 移除选项卡页 /// /// /// /// public void RemoveTabPage(XtraTabControl tabControl, EventArgs e) { DevExpress.XtraTab.ViewInfo.ClosePageButtonEventArgs args = (DevExpress.XtraTab.ViewInfo.ClosePageButtonEventArgs)e; string name = args.Page.Tooltip; foreach (XtraTabPage item in tabControl.TabPages) { if (item.Name == name) { tabControl.TabPages.Remove(item); item.Dispose(); TabPageDic.Remove(name); return; } } } /// /// 判断选项卡是否已经存在 /// /// 选项卡控件 /// 选项卡名称 /// private bool IsTabpageExsit(XtraTabControl tabControl, string tabPageName) { foreach (var item in TabPageDic) { if (item.Key == tabPageName) { tabControl.SelectedTabPage = item.Value; return true; } } return false; } /// /// 在选项卡中生成窗体 /// /// 窗体名称 private Form AddNewForm(string formName) { Form newForm = (Form)Assembly.GetExecutingAssembly().CreateInstance(formName); newForm.FormBorderStyle = FormBorderStyle.None; newForm.TopLevel = false; //newForm.Parent = ((XtraTabControl)sender).SelectedTabPage; newForm.ControlBox = false; newForm.Dock = DockStyle.Fill; newForm.Visible = true; return newForm; } } }