using Sunny.UI; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; class FormEx { public static void ShowSubForm(Control fatherForm, UIForm subForm) { if (subForm == null || fatherForm == null) { return; } if (fatherForm.Controls.Count > 0) { foreach (var f in fatherForm.Controls) { if (f is Form form) { form.Close(); } } fatherForm.Controls.Clear(); } //subForm.Dock = DockStyle.Fill; subForm.Location=new System.Drawing.Point(0,0); subForm.Size = fatherForm.ClientSize; subForm.ShowTitle = false; subForm.TopLevel = false; subForm.AutoSize = false; fatherForm.SuspendLayout(); fatherForm.Controls.Add(subForm); fatherForm.ResumeLayout(); subForm.Show(); } public static void SetButtonSelected(object sender) { //if (((UISymbolButton)sender).Selected) // return; ((UISymbolButton)sender).Selected = true; } /// /// 关闭所有作为子窗体的窗体 /// public static void CloseAllChildForms(Form form) { try { // 获取所有子窗体 var childForms = GetChildForms(form); foreach (Form childForm in childForms) { try { // 检查窗体是否还有效 if (childForm != null && !childForm.IsDisposed) { // 这会正常触发子窗体的 Closing 事件 childForm.Close(); // 确保资源被释放 childForm.Dispose(); } } catch (ObjectDisposedException) { // 窗体已经被释放,跳过 continue; } catch (Exception ex) { // 记录异常但继续处理其他窗体 System.Diagnostics.Debug.WriteLine( $"关闭子窗体失败: {ex.Message}"); } } } catch (Exception ex) { System.Diagnostics.Debug.WriteLine( $"查找子窗体时出错: {ex.Message}"); } } /// /// 递归获取指定容器中的所有子窗体 /// /// 容器控件 /// 子窗体列表 private static List
GetChildForms(Control container) { List childForms = new List(); if (container == null) return childForms; foreach (Control control in container.Controls) { // 检查是否为窗体且是子窗体(非顶级窗体) if (control is Form form && !form.IsDisposed)// && !form.TopLevel { childForms.Add(form); } // 递归检查嵌套的容器 if (control.HasChildren) { childForms.AddRange(GetChildForms(control)); } } return childForms; } public static string GetFormDescription(Type formType) { var attr = formType.GetCustomAttribute(); return attr?.Description ?? formType.Name; // 没有特性就用类名 } public static string GetFormDescription(Form form) { var formType = form.GetType(); return GetFormDescription(formType); } }