From 3735b6d2728881d932ad4ccc967216295fed00b6 Mon Sep 17 00:00:00 2001 From: Elivo Date: Wed, 7 May 2025 15:46:25 +0800 Subject: [PATCH] =?UTF-8?q?FormsUtility=EF=BC=9A=E5=A2=9E=E5=8A=A0=20Dispo?= =?UTF-8?q?se=20=E6=96=B9=E6=B3=95=EF=BC=8C=E9=87=8A=E6=94=BE=E5=AD=90?= =?UTF-8?q?=E6=8E=A7=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Apewer.Windows/Surface/FormsUtility.cs | 43 ++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/Apewer.Windows/Surface/FormsUtility.cs b/Apewer.Windows/Surface/FormsUtility.cs index 4a12d8c..cc89ff2 100644 --- a/Apewer.Windows/Surface/FormsUtility.cs +++ b/Apewer.Windows/Surface/FormsUtility.cs @@ -660,6 +660,49 @@ namespace Apewer.Surface if (form != null) SetTopMost(form.Handle, value); } + /// 释放系统资源。 + public static void Dispose(Control control) + { + if (control == null) return; + + if (control.InvokeRequired) + { + control.Invoke(() => Dispose(control)); + return; + } + + Dispose(control.Controls); + + if (control is Form form) + { + try + { + if (form.Visible) form.Close(); + } + catch { } + } + + RuntimeUtility.Dispose(control); + } + + /// 释放系统资源。 + public static void Dispose(Control.ControlCollection controls) + { + if (controls == null) return; + + var count = controls.Count; + if (count < 1) return; + + var children = new Control[count]; + controls.CopyTo(children, 0); + + foreach (var child in children) + { + controls.Remove(child); + Dispose(child); + } + } + #endif #endregion