using System; using System.Collections.Generic; using System.Linq; using System.Runtime.InteropServices; using System.Windows.Forms; namespace Core.Common.Controls.Swf { /// /// Extension methods for the class Control /// public static class ControlExtensions { private const int WM_SETREDRAW = 11; /// /// Gets the first found control of type in the control container. Search is recursive. /// /// The control container to search in /// The type of control to search for /// Returns the first control found, returns null otherwise public static T GetFirstControlOfType(this Control control) where T : Control { return GetAllControlsRecursive(control).FirstOrDefault(); } /// /// Gets all child controls (recursive) of a control container (parent) /// /// The parent container /// The type of control to look for /// A list of controls of type public static IEnumerable GetAllControlsRecursive(this Control container) where T : Control { return GetAllControlsRecursive(container).OfType(); } /// /// Gets all child controls (recursive) of a control container (parent) /// /// The parent container /// A list of all child controls> public static IEnumerable GetAllControlsRecursive(this Control container) { return GetAllControlsRecursive(container.Controls); } /// /// Gets all child controls (recursive) of a control container (parent) /// /// The parent container /// A list of all child controls> public static IEnumerable GetAllControlsRecursive(this Control.ControlCollection controlCollection) { foreach (Control child in controlCollection) { foreach (Control parent in GetAllControlsRecursive(child).Where(parent => parent != null)) { yield return parent; } if (child != null) { yield return child; } } } [DllImport("user32.dll")] public static extern int SendMessage(IntPtr hWnd, Int32 wMsg, bool wParam, Int32 lParam); public static void SuspendDrawing(this Control parent) { SendMessage(parent.Handle, WM_SETREDRAW, false, 0); } public static void ResumeDrawing(this Control parent) { SendMessage(parent.Handle, WM_SETREDRAW, true, 0); parent.Refresh(); } } }