using DevExpress.XtraBars.Docking2010.Views; using DevExpress.XtraBars.Docking2010.Views.Tabbed; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using Ips.Library.Basic; using DevExpress.XtraBars.Docking2010; using DevExpress.XtraBars.Docking; using Ips.Library.DxpLib; using DevExpress.Utils.Extensions; using DevExpress.XtraEditors; namespace Ips.Library.DxpLib { public static class FormManager { static XtraForm _mainWin; static DocumentManager _documentManager; static TabbedView _tabbedView; static DockManager _dockManager; public static void Init(XtraForm mainWin, DocumentManager documentManager, TabbedView tabbedView, DockManager dockManager) { _mainWin = mainWin; _documentManager = documentManager; _tabbedView = tabbedView; _dockManager = dockManager; _tabbedView.QueryControl += TabbedView_QueryControl; } private static void TabbedView_QueryControl(object sender, DevExpress.XtraBars.Docking2010.Views.QueryControlEventArgs e) { try { var ctrlType = _mainWin.GetType().Assembly.GetType(e.Document.ControlName); if (ctrlType == null) { MsgHelper.ShowError($"未找到类型【{e.Document.ControlName}】"); return; } e.Control = Activator.CreateInstance(ctrlType) as Control; } catch (Exception ex) { MsgHelper.ShowError($"打开模块【{e.Document.Caption}】出错,错误消息:" + ex.Message); } } public static void ShowDocument(string caption = null, Action optionAction = null) where T : Control { string controlName = typeof(T).FullName; BaseDocument document = _tabbedView.Documents.FindFirst(d => d.ControlName == controlName); if (document == null) document = _tabbedView.AddDocument(caption, controlName); optionAction?.Invoke(document); _tabbedView.Controller.Activate(document); } public static void ShowDocument(Control control, string caption = null) { caption = caption ?? control.Text; var document = _tabbedView.Documents.FirstOrDefault(m => m.Caption == caption); if (document == null) { document = _tabbedView.AddDocument(control, caption); } _tabbedView.Controller.Activate(document); } public static void ShowDockPanel(DockingStyle dockingStyle, string caption = null, int index = -1) where T : UserControl { if (_dockManager == null) return; var oldPanel = _dockManager.Panels.FirstOrDefault(m => m.ControlContainer != null && m.ControlContainer.Controls.Count > 0 && m.ControlContainer.Controls[0] is T ); if (oldPanel != null) { oldPanel.Show(); return; } var targetDockPanel = _dockManager.RootPanels.FirstOrDefault(m => m.Dock == dockingStyle); DockPanel dockPanel; if (targetDockPanel == null) { dockPanel = _dockManager.AddPanel(dockingStyle); } else { dockPanel = _dockManager.AddPanel(DockingStyle.Float); dockPanel.DockAsTab(targetDockPanel); } var ctrl = Activator.CreateInstance(); dockPanel.Text = caption ?? ctrl.Text; ctrl.Dock = DockStyle.Fill; if (dockPanel.ParentPanel != null) { dockPanel.ParentPanel.Tabbed = true; } dockPanel.ControlContainer.Controls.Add(ctrl); } } }