namespace ISupply.WinFrm.Client { public partial class Main : DevExpress.XtraBars.FluentDesignSystem.FluentDesignForm { public Main() { InitializeComponent(); this.IconOptions.Icon = ISupply.Icons.Resource.App; if (this.components == null) _app = new System.Windows.Forms.NotifyIcon(); else _app = new System.Windows.Forms.NotifyIcon(this.components); _app.Text = "曲线管理"; _app.Visible = true; _app.Click += (object sender, EventArgs e) => { this.Visible = true; this.WindowState = FormWindowState.Maximized; _app.Visible = false; }; } //icon readonly NotifyIcon _app = null; /*//禁止标题扩展 protected override bool ExtendNavigationControlToFormTitle { get { return false; } }*/ //开启界面双缓冲 解决窗体切换闪屏问题 protected override CreateParams CreateParams { get { CreateParams cp = base.CreateParams; cp.ExStyle |= 0x02000000; return cp; } } /// /// 初始化 /// internal void Initial() { var list = FuncCreater.GetFuncElements(); if (list != null && list.Count > 0) { foreach (var element in list) { var accEle = GetAccordionControlElement(element); this.accordionControl.Elements.Add(accEle); } } } #region TabbedView //选中 private void tabbedView1_DocumentSelected(object sender, DocumentEventArgs e) { if (e.Document == null) return; if (!(e.Document.Tag is WinFrm.SurfaceGuid surfaceGuid)) return; foreach (var item in this.accordionControl.Elements) { if (SelectAccordionControlElement(item, surfaceGuid)) break; } } #endregion #region Page //是否存在Page private bool IsExistPage(WinFrm.SurfaceGuid sguid, bool isActivateDoc) { if (sguid == null) return false; if (this.tabbedView1.Documents != null && this.tabbedView1.Documents.Count > 0) { foreach (BaseDocument doc in this.tabbedView1.Documents) { if (doc.Tag != null) { if (doc.Tag is WinFrm.SurfaceGuid) { if ((doc.Tag as WinFrm.SurfaceGuid).ToString() == sguid.ToString()) { if (isActivateDoc) this.tabbedView1.Controller.Activate(doc); return true; } } } } } return false; } //更新数据 private void RefreshPageData(WinFrm.SurfaceGuid sguid) { if (sguid == null) return; if (this.tabbedView1.Documents != null && this.tabbedView1.Documents.Count > 0) { var doc = this.tabbedView1.Documents.ToList().Find(x => x.Tag != null && x.Tag is WinFrm.SurfaceGuid && (x.Tag as WinFrm.SurfaceGuid).ToString() == sguid.ToString()); if (doc != null) { if (doc.Control is WinFrm.DocumentPage page) { page.RefreshDataSource(); } return; } } } //更新PageTitle private void UpdatePageTitle(WinFrm.SurfaceGuid sguid, WinFrm.PageTitle title) { if (sguid == null || title == null) return; if (this.tabbedView1.Documents != null && this.tabbedView1.Documents.Count > 0) { var doc = this.tabbedView1.Documents.ToList().Find(x => x.Tag != null && x.Tag is WinFrm.SurfaceGuid && (x.Tag as WinFrm.SurfaceGuid).ToString() == sguid.ToString()); if (doc != null) { doc.Caption = title.Caption; doc.ImageOptions.Image = title.HeaderImage; doc.ImageOptions.SvgImage = title.HeaderSvgImage; doc.ImageOptions.SvgImageSize = title.SvgImageSize; if (doc.Control is WinFrm.DocumentPage page) { page.PageTitle = title; } return; } } } //创建Page private bool CreatePage(WinFrm.DocumentPage page, WinFrm.SurfaceGuid sguid) { if (page == null || sguid == null) return false; page.SurfaceGuid = sguid; page.Dock = DockStyle.Fill; page.IsExistPageEvent += IsExistPage; page.CreatePageEvent += CreatePage; page.RefreshPageDataEvent += RefreshPageData; page.UpdatePageTitleEvent += UpdatePageTitle; page.ClosePageEvent += ClosePage; page.ResetAllPagesEvent += ResetAllPages; page.RegistEvents(); this.tabbedView1.BeginUpdate(); var doc = this.tabbedView1.AddDocument(page); doc.Footer = Directory.GetCurrentDirectory(); if (page.PageTitle != null) { doc.Caption = page.PageTitle.Caption; doc.ImageOptions.Image = page.PageTitle.HeaderImage; doc.ImageOptions.SvgImage = page.PageTitle.HeaderSvgImage; doc.ImageOptions.SvgImageSize = page.PageTitle.SvgImageSize; } else { doc.Caption = page.SurfaceGuid?.Function; } doc.Tag = sguid; this.tabbedView1.EndUpdate(); this.tabbedView1.Controller.Activate(doc); this.tabbedView1.ActivateDocument(doc.Control); page.InitialDataSource(); return true; } //关闭Page private void ClosePage(WinFrm.SurfaceGuid sguid) { if (this.tabbedView1.Documents != null && this.tabbedView1.Documents.Count > 0) { var doc = this.tabbedView1.Documents.ToList().Find(x => x.Tag != null && x.Tag is WinFrm.SurfaceGuid && (x.Tag as WinFrm.SurfaceGuid).ToString() == sguid.ToString()); if (doc != null) { var page = doc.Control as WinFrm.DocumentPage; this.tabbedView1.Controller.Close(doc);//会触发正在关闭和关闭事件 } } } //查找page private WinFrm.DocumentPage FindPage(WinFrm.SurfaceGuid sguid, bool isActiveDoc) { if (sguid == null) return default; var sguid_msg = sguid.ToString(); if (this.tabbedView1.Documents != null && this.tabbedView1.Documents.Count > 0) { var doc = this.tabbedView1.Documents.ToList().Find(x => x.Tag != null && x.Tag.ToString() == sguid_msg); if (doc != null) { if (isActiveDoc) this.tabbedView1.Controller.Activate(doc); if (doc.Control is WinFrm.DocumentPage page) { return page; } return null; } } return null; } //重置所有页面 private void ResetAllPages() { this.tabbedView1.Controller.CloseAll(); } #endregion #region AccordionControl //功能点击事件 private void accordionControl_ElementClick(object sender, DevExpress.XtraBars.Navigation.ElementClickEventArgs e) { if (e.Element.Tag == null) return; var ele = e.Element.Tag as FuncElement; if (ele.SurfaceGuid != null) { CreateFuncPage(ele); } } //获取模块元素 private DevExpress.XtraBars.Navigation.AccordionControlElement GetAccordionControlElement(FuncElement ele) { var accEle = new DevExpress.XtraBars.Navigation.AccordionControlElement(); if (ele.SurfaceGuid != null) { accEle.Name = ele.SurfaceGuid.ToString(); } else { accEle.Name = ele.Id; } accEle.Text = ele.Name; accEle.Image = ele.Image; accEle.Expanded = ele.Expand; accEle.AllowGlyphSkinning = DevExpress.Utils.DefaultBoolean.False; accEle.Tag = ele; accEle.Style = DevExpress.XtraBars.Navigation.ElementStyle.Item; if (ele.FuncElements != null && ele.FuncElements.Count > 0) { foreach (var item in ele.FuncElements) { accEle.Elements.Add(GetAccordionControlElement(item)); } accEle.Style = DevExpress.XtraBars.Navigation.ElementStyle.Group; } return accEle; } //选择功能元素 private bool SelectAccordionControlElement(DevExpress.XtraBars.Navigation.AccordionControlElement element, WinFrm.SurfaceGuid surfaceGuid) { if (element == null) return false; if (surfaceGuid == null) return false; if (element.Name == surfaceGuid.ToString()) { this.accordionControl.SelectedElement = element; if (element.OwnerElement != null) { element.OwnerElement.Expanded = true; this.accordionControl.Refresh(); } return true; } else { if (element.Elements != null && element.Elements.Count > 0) { foreach (var item in element.Elements) { SelectAccordionControlElement(item, surfaceGuid); } } } return false; } //创建功能页 private void CreateFuncPage(FuncElement obj) { try { var control = (Control)Assembly.LoadFrom(obj.DllFileName).CreateInstance(obj.ControlFullName); if (control is WinFrm.DocumentPage page) { if (IsExistPage(obj.SurfaceGuid, true)) return; CreatePage(page, obj.SurfaceGuid); } } catch (Exception) { XtraMessageBox.Show($"{obj.Name} 配置错误!"); } } #endregion } }