FormManager.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. using DevExpress.XtraBars.Docking2010.Views;
  2. using DevExpress.XtraBars.Docking2010.Views.Tabbed;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using System.Windows.Forms;
  9. using Ips.Library.Basic;
  10. using DevExpress.XtraBars.Docking2010;
  11. using DevExpress.XtraBars.Docking;
  12. using Ips.Library.DxpLib;
  13. using DevExpress.Utils.Extensions;
  14. using DevExpress.XtraEditors;
  15. namespace Ips.Library.DxpLib
  16. {
  17. public static class FormManager
  18. {
  19. static XtraForm _mainWin;
  20. static DocumentManager _documentManager;
  21. static TabbedView _tabbedView;
  22. static DockManager _dockManager;
  23. public static void Init(XtraForm mainWin, DocumentManager documentManager, TabbedView tabbedView, DockManager dockManager)
  24. {
  25. _mainWin = mainWin;
  26. _documentManager = documentManager;
  27. _tabbedView = tabbedView;
  28. _dockManager = dockManager;
  29. _tabbedView.QueryControl += TabbedView_QueryControl;
  30. }
  31. private static void TabbedView_QueryControl(object sender, DevExpress.XtraBars.Docking2010.Views.QueryControlEventArgs e)
  32. {
  33. try
  34. {
  35. var ctrlType = _mainWin.GetType().Assembly.GetType(e.Document.ControlName);
  36. if (ctrlType == null)
  37. {
  38. MsgHelper.ShowError($"未找到类型【{e.Document.ControlName}】");
  39. return;
  40. }
  41. e.Control = Activator.CreateInstance(ctrlType) as Control;
  42. }
  43. catch (Exception ex)
  44. {
  45. MsgHelper.ShowError($"打开模块【{e.Document.Caption}】出错,错误消息:" + ex.Message);
  46. }
  47. }
  48. public static void ShowDocument<T>(string caption = null, Action<BaseDocument> optionAction = null)
  49. where T : Control
  50. {
  51. string controlName = typeof(T).FullName;
  52. BaseDocument document = _tabbedView.Documents.FindFirst(d => d.ControlName == controlName);
  53. if (document == null)
  54. document = _tabbedView.AddDocument(caption, controlName);
  55. optionAction?.Invoke(document);
  56. _tabbedView.Controller.Activate(document);
  57. }
  58. public static void ShowDocument(Control control, string caption = null)
  59. {
  60. caption = caption ?? control.Text;
  61. var document = _tabbedView.Documents.FirstOrDefault(m => m.Caption == caption);
  62. if (document == null)
  63. {
  64. document = _tabbedView.AddDocument(control, caption);
  65. }
  66. _tabbedView.Controller.Activate(document);
  67. }
  68. public static void ShowDockPanel<T>(DockingStyle dockingStyle, string caption = null, int index = -1)
  69. where T : UserControl
  70. {
  71. if (_dockManager == null) return;
  72. var oldPanel = _dockManager.Panels.FirstOrDefault(m => m.ControlContainer != null
  73. && m.ControlContainer.Controls.Count > 0
  74. && m.ControlContainer.Controls[0] is T
  75. );
  76. if (oldPanel != null)
  77. {
  78. oldPanel.Show();
  79. return;
  80. }
  81. var targetDockPanel = _dockManager.RootPanels.FirstOrDefault(m => m.Dock == dockingStyle);
  82. DockPanel dockPanel;
  83. if (targetDockPanel == null)
  84. {
  85. dockPanel = _dockManager.AddPanel(dockingStyle);
  86. }
  87. else
  88. {
  89. dockPanel = _dockManager.AddPanel(DockingStyle.Float);
  90. dockPanel.DockAsTab(targetDockPanel);
  91. }
  92. var ctrl = Activator.CreateInstance<T>();
  93. dockPanel.Text = caption ?? ctrl.Text;
  94. ctrl.Dock = DockStyle.Fill;
  95. if (dockPanel.ParentPanel != null)
  96. {
  97. dockPanel.ParentPanel.Tabbed = true;
  98. }
  99. dockPanel.ControlContainer.Controls.Add(ctrl);
  100. }
  101. }
  102. }