FormEx.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. using Sunny.UI;
  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. class FormEx
  9. {
  10. public static void ShowSubForm(Control fatherForm, UIForm subForm)
  11. {
  12. if (subForm == null || fatherForm == null)
  13. {
  14. return;
  15. }
  16. if (fatherForm.Controls.Count > 0)
  17. {
  18. foreach (var f in fatherForm.Controls)
  19. {
  20. if (f is Form form)
  21. {
  22. form.Close();
  23. }
  24. }
  25. fatherForm.Controls.Clear();
  26. }
  27. //subForm.Dock = DockStyle.Fill;
  28. subForm.Location=new System.Drawing.Point(0,0);
  29. subForm.Size = fatherForm.ClientSize;
  30. subForm.ShowTitle = false;
  31. subForm.TopLevel = false;
  32. subForm.AutoSize = false;
  33. fatherForm.SuspendLayout();
  34. fatherForm.Controls.Add(subForm);
  35. fatherForm.ResumeLayout();
  36. subForm.Show();
  37. }
  38. public static void SetButtonSelected(object sender)
  39. {
  40. //if (((UISymbolButton)sender).Selected)
  41. // return;
  42. ((UISymbolButton)sender).Selected = true;
  43. }
  44. /// <summary>
  45. /// 关闭所有作为子窗体的窗体
  46. /// </summary>
  47. public static void CloseAllChildForms(Form form)
  48. {
  49. try
  50. {
  51. // 获取所有子窗体
  52. var childForms = GetChildForms(form);
  53. foreach (Form childForm in childForms)
  54. {
  55. try
  56. {
  57. // 检查窗体是否还有效
  58. if (childForm != null && !childForm.IsDisposed)
  59. {
  60. // 这会正常触发子窗体的 Closing 事件
  61. childForm.Close();
  62. // 确保资源被释放
  63. childForm.Dispose();
  64. }
  65. }
  66. catch (ObjectDisposedException)
  67. {
  68. // 窗体已经被释放,跳过
  69. continue;
  70. }
  71. catch (Exception ex)
  72. {
  73. // 记录异常但继续处理其他窗体
  74. System.Diagnostics.Debug.WriteLine(
  75. $"关闭子窗体失败: {ex.Message}");
  76. }
  77. }
  78. }
  79. catch (Exception ex)
  80. {
  81. System.Diagnostics.Debug.WriteLine(
  82. $"查找子窗体时出错: {ex.Message}");
  83. }
  84. }
  85. /// <summary>
  86. /// 递归获取指定容器中的所有子窗体
  87. /// </summary>
  88. /// <param name="container">容器控件</param>
  89. /// <returns>子窗体列表</returns>
  90. private static List<Form> GetChildForms(Control container)
  91. {
  92. List<Form> childForms = new List<Form>();
  93. if (container == null) return childForms;
  94. foreach (Control control in container.Controls)
  95. {
  96. // 检查是否为窗体且是子窗体(非顶级窗体)
  97. if (control is Form form && !form.IsDisposed)// && !form.TopLevel
  98. {
  99. childForms.Add(form);
  100. }
  101. // 递归检查嵌套的容器
  102. if (control.HasChildren)
  103. {
  104. childForms.AddRange(GetChildForms(control));
  105. }
  106. }
  107. return childForms;
  108. }
  109. public static string GetFormDescription(Type formType)
  110. {
  111. var attr = formType.GetCustomAttribute<FormDescriptionAttribute>();
  112. return attr?.Description ?? formType.Name; // 没有特性就用类名
  113. }
  114. public static string GetFormDescription(Form form)
  115. {
  116. var formType = form.GetType();
  117. return GetFormDescription(formType);
  118. }
  119. }