ChildWinManagement.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. using ProjectBase.Controls.Others;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using System.Windows.Forms;
  8. namespace ProjectBase.Controls
  9. {
  10. /// <summary>
  11. /// 窗口管理辅助类
  12. /// </summary>
  13. public sealed class ChildWinManagement
  14. {
  15. private ChildWinManagement()
  16. {
  17. }
  18. /// <summary>
  19. /// 获取MDI父窗口是否有窗口标题为指定字符串的子窗口(如果已经存在把此子窗口推向前台)
  20. /// </summary>
  21. /// <param name="MDIwin">MDI父窗口</param>
  22. /// <param name="caption">窗口标题</param>
  23. /// <returns></returns>
  24. public static bool ExistWin(Form MDIwin, string caption)
  25. {
  26. bool R = false;
  27. foreach (Form f in MDIwin.MdiChildren)
  28. {
  29. if (f.Text == caption)
  30. {
  31. R = true;
  32. f.Show();
  33. f.Activate();
  34. break;
  35. }
  36. }
  37. return R;
  38. }
  39. /// <summary>
  40. /// 唯一加载某个类型的窗体,如果存在则显示,否则创建。
  41. /// </summary>
  42. /// <param name="mainDialog">主窗体对象</param>
  43. /// <param name="formType">待显示的窗体类型</param>
  44. /// <returns></returns>
  45. public static Form LoadMdiForm(Form mainDialog, Type formType)
  46. {
  47. bool bFound = false;
  48. Form tableForm = null;
  49. foreach (Form form in mainDialog.MdiChildren)
  50. {
  51. if (form.GetType() == formType)
  52. {
  53. bFound = true;
  54. tableForm = form;
  55. break;
  56. }
  57. }
  58. if (!bFound)
  59. {
  60. tableForm = (Form)Activator.CreateInstance(formType);
  61. tableForm.MdiParent = mainDialog;
  62. tableForm.Show();
  63. }
  64. //tableForm.Dock = DockStyle.Fill;
  65. //tableForm.WindowState = FormWindowState.Maximized;
  66. tableForm.BringToFront();
  67. tableForm.Activate();
  68. return tableForm;
  69. }
  70. /// <summary>
  71. /// 把控件附加到窗体上弹出
  72. /// </summary>
  73. /// <param name="control">待显示的控件</param>
  74. /// <param name="caption">窗体显示的标题</param>
  75. public static void PopControlForm(Type control, string caption)
  76. {
  77. object ctr = ReflectionUtil.CreateInstance(control);
  78. if ((typeof(Control)).IsAssignableFrom(ctr.GetType()))
  79. {
  80. Form tmp = new Form();
  81. tmp.WindowState = FormWindowState.Maximized;
  82. tmp.ShowIcon = false;
  83. tmp.Text = caption;
  84. tmp.ShowInTaskbar = false;
  85. tmp.StartPosition = FormStartPosition.CenterScreen;
  86. Control ctrtmp = ctr as Control;
  87. ctrtmp.Dock = DockStyle.Fill;
  88. tmp.Controls.Add(ctrtmp);
  89. tmp.ShowDialog();
  90. }
  91. }
  92. /// <summary>
  93. /// 弹出窗体
  94. /// </summary>
  95. /// <param name="type">待显示的窗体类型</param>
  96. public static void PopDialogForm(Type type)
  97. {
  98. object form = ReflectionUtil.CreateInstance(type);
  99. if ((typeof(Form)).IsAssignableFrom(form.GetType()))
  100. {
  101. Form tmp = form as Form;
  102. tmp.ShowInTaskbar = false;
  103. tmp.StartPosition = FormStartPosition.CenterScreen;
  104. tmp.ShowDialog();
  105. }
  106. }
  107. }
  108. }