using System.ComponentModel; using System.Windows.Forms; using System.Windows.Forms.VisualStyles; using Vanara.Interop.DesktopWindowManager; namespace AeroWizard { /// /// A table layout panel that supports a glass overlay. /// [ToolboxItem(true), System.Drawing.ToolboxBitmap(typeof(ThemedTableLayoutPanel), "ThemedTableLayoutPanel.bmp")] public class ThemedTableLayoutPanel : TableLayoutPanel { private VisualStyleRenderer rnd; /// /// Initializes a new instance of the class. /// public ThemedTableLayoutPanel() { SetTheme(VisualStyleElement.Window.Dialog.Normal); } /// /// Clears the theme and defaults to TableLayoutPanel painting. /// public void ClearTheme() { rnd = null; } /// /// Sets the theme using a defined . /// /// The visual element. public void SetTheme(VisualStyleElement element) { if (VisualStyleRenderer.IsSupported && VisualStyleRenderer.IsElementDefined(element)) rnd = new VisualStyleRenderer(element); else rnd = null; } /// /// Sets the theme using theme class information. /// /// Name of the theme class. /// The theme part. /// The theme state. public void SetTheme(string className, int part, int state) { if (VisualStyleRenderer.IsSupported) { try { rnd = new VisualStyleRenderer(className, part, state); return; } catch { } } rnd = null; } /// /// Gets or sets a value indicating whether to watch getting and losing focus. /// /// /// true if watching focus events; otherwise, false. /// [DefaultValue(false), Category("Behavior")] public bool WatchFocus { get; set; } /// /// Gets or sets a value indicating whether this table supports glass (can be enclosed in the glass margin). /// /// /// true if supports glass; otherwise, false. /// [DefaultValue(false), Category("Appearance")] public bool SupportGlass { get; set; } /// /// Raises the event. /// /// An that contains the event data. protected override void OnHandleCreated(System.EventArgs e) { base.OnHandleCreated(e); AttachToFormEvents(); } /// /// Raises the event. /// /// A that contains the event data. protected override void OnPaint(PaintEventArgs e) { if (!this.IsDesignMode() && SupportGlass && DesktopWindowManager.IsCompositionEnabled()) try { e.Graphics.Clear(System.Drawing.Color.Black); } catch { } else { if (this.IsDesignMode() || rnd == null || !Application.RenderWithVisualStyles) try { e.Graphics.Clear(BackColor); } catch { } else rnd.DrawBackground(e.Graphics, ClientRectangle, e.ClipRectangle); } base.OnPaint(e); } private void AttachToFormEvents() { Form pForm = FindForm(); if (pForm != null && WatchFocus) { pForm.Activated += new System.EventHandler(Form_GotFocus); pForm.Deactivate += new System.EventHandler(Form_LostFocus); } } private void Form_GotFocus(object sender, System.EventArgs e) { OnGotFocus(e); if (rnd != null && Application.RenderWithVisualStyles) rnd.SetParameters(rnd.Class, rnd.Part, 1); Refresh(); } private void Form_LostFocus(object sender, System.EventArgs e) { OnLostFocus(e); if (rnd != null && Application.RenderWithVisualStyles) rnd.SetParameters(rnd.Class, rnd.Part, 2); Refresh(); } } }