| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129 |
- 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;
- }
- /// <summary>
- /// 关闭所有作为子窗体的窗体
- /// </summary>
- 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}");
- }
- }
- /// <summary>
- /// 递归获取指定容器中的所有子窗体
- /// </summary>
- /// <param name="container">容器控件</param>
- /// <returns>子窗体列表</returns>
- private static List<Form> GetChildForms(Control container)
- {
- List<Form> childForms = new List<Form>();
- 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<FormDescriptionAttribute>();
- return attr?.Description ?? formType.Name; // 没有特性就用类名
- }
- public static string GetFormDescription(Form form)
- {
- var formType = form.GetType();
- return GetFormDescription(formType);
- }
- }
|