Cleaned and formatted

pull/30/head
David Hall 2019-01-11 19:59:43 -07:00
parent eae5d23af2
commit 0387c8b30e
1 changed files with 50 additions and 104 deletions

View File

@ -7,9 +7,7 @@ using Vanara.Extensions;
namespace Vanara.Windows.Forms namespace Vanara.Windows.Forms
{ {
/// <summary> /// <summary>GlassExtenderProvider extends a <see cref="Form"/> and provides glass margins.</summary>
/// GlassExtenderProvider extends a <see cref="System.Windows.Forms.Form"/> and provides glass margins.
/// </summary>
[ProvideProperty("GlassEnabled", typeof(Form))] [ProvideProperty("GlassEnabled", typeof(Form))]
[ProvideProperty("GlassMarginMovesForm", typeof(Form))] [ProvideProperty("GlassMarginMovesForm", typeof(Form))]
[ProvideProperty("GlassMargins", typeof(Form))] [ProvideProperty("GlassMargins", typeof(Form))]
@ -19,112 +17,54 @@ namespace Vanara.Windows.Forms
{ {
private readonly Dictionary<Form, GlassFormProperties> formProps = new Dictionary<Form, GlassFormProperties>(); private readonly Dictionary<Form, GlassFormProperties> formProps = new Dictionary<Form, GlassFormProperties>();
/// <summary> /// <summary>Initializes a new instance of the <see cref="GlassExtenderProvider"/> class.</summary>
/// Properties for each form that is extended.
/// </summary>
private class GlassFormProperties
{
public Point FormMoveLastMousePos = Point.Empty;
public bool FormMoveTracking = false;
public bool GlassEnabled = true;
public Padding GlassMargins = Padding.Empty;
public bool GlassMarginMovesForm = true;
}
/// <summary>
/// Initializes a new instance of the <see cref="GlassExtenderProvider"/> class.
/// </summary>
public GlassExtenderProvider() public GlassExtenderProvider()
{ {
} }
/// <summary> /// <summary>Gets whether glass should be extended into the client space.</summary>
/// Gets whether glass should be extended into the client space. /// <param name="form">The <see cref="Form"/> to be extended.</param>
/// </summary>
/// <param name="form">The <see cref="System.Windows.Forms.Form"/> to be extended.</param>
/// <returns><c>true</c> if the glass is enabled; otherwise <c>false</c>.</returns> /// <returns><c>true</c> if the glass is enabled; otherwise <c>false</c>.</returns>
[DisplayName("GlassEnabled")] [DisplayName("GlassEnabled")]
[DefaultValue(true)] [DefaultValue(true)]
[Category("Behavior")] [Category("Behavior")]
[Description("Indicates whether extending glass into the client area is enabled.")] [Description("Indicates whether extending glass into the client area is enabled.")]
public bool GetGlassEnabled(Form form) public bool GetGlassEnabled(Form form) => formProps.TryGetValue(form, out var prop) ? prop.GlassEnabled : true;
{
GlassFormProperties prop;
if (formProps.TryGetValue(form, out prop))
return prop.GlassEnabled;
return true;
}
/// <summary> /// <summary>Gets a value indicating whether clicking and dragging within the top margin will move the form.</summary>
/// Gets a value indicating whether clicking and dragging within the top margin will move the form. /// <param name="form">The <see cref="Form"/> to be extended.</param>
/// </summary>
/// <param name="form">The <see cref="System.Windows.Forms.Form"/> to be extended.</param>
/// <returns><c>true</c> if clicking and dragging on the top margin moves the form; otherwise, <c>false</c>.</returns> /// <returns><c>true</c> if clicking and dragging on the top margin moves the form; otherwise, <c>false</c>.</returns>
[DisplayName("GlassMarginMovesForm")] [DisplayName("GlassMarginMovesForm")]
[DefaultValue(true)] [DefaultValue(true)]
[Category("Behavior")] [Category("Behavior")]
[Description("Specifies if clicking and dragging within the margin will move the form. ")] [Description("Specifies if clicking and dragging within the margin will move the form. ")]
public bool GetGlassMarginMovesForm(Form form) public bool GetGlassMarginMovesForm(Form form) => formProps.TryGetValue(form, out var prop) ? prop.GlassMarginMovesForm : true;
{
GlassFormProperties prop;
if (formProps.TryGetValue(form, out prop))
return prop.GlassMarginMovesForm;
return true;
}
/// <summary> /// <summary>Gets the glass margins.</summary>
/// Gets the glass margins. /// <param name="form">The <see cref="Form"/> to be extended.</param>
/// </summary>
/// <param name="form">The <see cref="System.Windows.Forms.Form"/> to be extended.</param>
/// <returns>The margins where the glass will be extended.</returns> /// <returns>The margins where the glass will be extended.</returns>
[DefaultValue(typeof(Padding), "0")] [DefaultValue(typeof(Padding), "0")]
[DisplayName("GlassMargins")] [DisplayName("GlassMargins")]
[Description("Specifies the interior glass margin of the form. Set to -1 for full window glass.")] [Description("Specifies the interior glass margin of the form. Set to -1 for full window glass.")]
[Category("Layout")] [Category("Layout")]
public Padding GetGlassMargins(Form form) public Padding GetGlassMargins(Form form) => formProps.TryGetValue(form, out var prop) ? prop.GlassMargins : Padding.Empty;
{
GlassFormProperties prop;
if (formProps.TryGetValue(form, out prop))
return prop.GlassMargins;
return Padding.Empty;
}
/// <summary> /// <summary>Set whether the glass should be extended into the client space.</summary>
/// Specifies whether this object can provide its extender properties to the specified object. /// <param name="form">The <see cref="Form"/> to be extended.</param>
/// </summary>
/// <param name="form">The <see cref="T:System.Object"/> to receive the extender properties.</param>
/// <returns>
/// true if this object can provide extender properties to the specified object; otherwise, false.
/// </returns>
bool IExtenderProvider.CanExtend(object form) => (form != this) && (form is Form);
/// <summary>
/// Set whether the glass should be extended into the client space.
/// </summary>
/// <param name="form">The <see cref="System.Windows.Forms.Form"/> to be extended.</param>
/// <param name="value">The enabled value.</param> /// <param name="value">The enabled value.</param>
public void SetGlassEnabled(Form form, bool value) public void SetGlassEnabled(Form form, bool value)
{ {
var prop = GetFormProperties(form); GetFormProperties(form).GlassEnabled = value;
prop.GlassEnabled = value;
GlassifyForm(form); GlassifyForm(form);
} }
/// <summary> /// <summary>Sets a value indicating whether clicking and dragging within the margin will move the form.</summary>
/// Sets a value indicating whether clicking and dragging within the margin will move the form. /// <param name="form">The <see cref="Form"/> to be extended.</param>
/// </summary>
/// <param name="form">The <see cref="System.Windows.Forms.Form"/> to be extended.</param>
/// <param name="value"><c>true</c> if clicking and dragging within the margin moves the form; otherwise, <c>false</c>.</param> /// <param name="value"><c>true</c> if clicking and dragging within the margin moves the form; otherwise, <c>false</c>.</param>
public void SetGlassMarginMovesForm(Form form, bool value) public void SetGlassMarginMovesForm(Form form, bool value) => GetFormProperties(form).GlassMarginMovesForm = value;
{
var prop = GetFormProperties(form);
prop.GlassMarginMovesForm = value;
}
/// <summary> /// <summary>Sets the glass margins.</summary>
/// Sets the glass margins. /// <param name="form">The <see cref="Form"/> to be extended.</param>
/// </summary>
/// <param name="form">The <see cref="System.Windows.Forms.Form"/> to be extended.</param>
/// <param name="value">The margins where the glass will be extended.</param> /// <param name="value">The margins where the glass will be extended.</param>
public void SetGlassMargins(Form form, Padding value) public void SetGlassMargins(Form form, Padding value)
{ {
@ -153,6 +93,11 @@ namespace Vanara.Windows.Forms
form.Invalidate(); form.Invalidate();
} }
/// <summary>Specifies whether this object can provide its extender properties to the specified object.</summary>
/// <param name="form">The <see cref="T:System.Object"/> to receive the extender properties.</param>
/// <returns>true if this object can provide extender properties to the specified object; otherwise, false.</returns>
bool IExtenderProvider.CanExtend(object form) => !ReferenceEquals(form, this) && form is Form;
/// <summary> /// <summary>
/// Releases the unmanaged resources used by the <see cref="T:System.ComponentModel.Component"/> and optionally releases the managed resources. /// Releases the unmanaged resources used by the <see cref="T:System.ComponentModel.Component"/> and optionally releases the managed resources.
/// </summary> /// </summary>
@ -161,7 +106,7 @@ namespace Vanara.Windows.Forms
{ {
if (disposing) if (disposing)
{ {
foreach (Form form in formProps.Keys) foreach (var form in formProps.Keys)
{ {
if (!form.IsDisposed) if (!form.IsDisposed)
{ {
@ -172,6 +117,14 @@ namespace Vanara.Windows.Forms
base.Dispose(disposing); base.Dispose(disposing);
} }
private static Rectangle GetNonGlassArea(Form form, GlassFormProperties prop)
{
if (prop == null)
return form.ClientRectangle;
return new Rectangle(form.ClientRectangle.Left + prop.GlassMargins.Left, form.ClientRectangle.Top + prop.GlassMargins.Top,
form.ClientRectangle.Width - prop.GlassMargins.Horizontal, form.ClientRectangle.Height - prop.GlassMargins.Vertical);
}
private void form_MouseDown(object sender, MouseEventArgs e) private void form_MouseDown(object sender, MouseEventArgs e)
{ {
if (e.Button == MouseButtons.Left) if (e.Button == MouseButtons.Left)
@ -207,53 +160,36 @@ namespace Vanara.Windows.Forms
{ {
if (e.Button == MouseButtons.Left) if (e.Button == MouseButtons.Left)
{ {
var prop = GetFormProperties(sender as Form); GetFormProperties(sender as Form).FormMoveTracking = false;
prop.FormMoveTracking = false;
} }
} }
private void form_Paint(object sender, PaintEventArgs e) private void form_Paint(object sender, PaintEventArgs e) => GlassifyForm(sender as Form, e.Graphics);
{
GlassifyForm(sender as Form, e.Graphics);
}
private void form_Resize(object sender, EventArgs e) private void form_Resize(object sender, EventArgs e)
{ {
var form = sender as Form; var form = sender as Form;
if ((DesktopWindowManager.CompositionEnabled && GetGlassEnabled(form)) || form.IsDesignMode()) if (DesktopWindowManager.CompositionEnabled && GetGlassEnabled(form) || form.IsDesignMode())
InvalidateNonGlassClientArea(form); InvalidateNonGlassClientArea(form);
} }
private void form_Shown(object sender, EventArgs e) private void form_Shown(object sender, EventArgs e) => GlassifyForm(sender as Form);
{
GlassifyForm(sender as Form);
}
private GlassFormProperties GetFormProperties(Form form) private GlassFormProperties GetFormProperties(Form form)
{ {
GlassFormProperties prop; if (!formProps.TryGetValue(form, out var prop))
if (!formProps.TryGetValue(form, out prop))
formProps.Add(form, prop = new GlassFormProperties()); formProps.Add(form, prop = new GlassFormProperties());
return prop; return prop;
} }
private static Rectangle GetNonGlassArea(Form form, GlassFormProperties prop)
{
if (prop == null)
return form.ClientRectangle;
return new Rectangle(form.ClientRectangle.Left + prop.GlassMargins.Left, form.ClientRectangle.Top + prop.GlassMargins.Top,
form.ClientRectangle.Width - prop.GlassMargins.Horizontal, form.ClientRectangle.Height - prop.GlassMargins.Vertical);
}
private void GlassifyForm(Form form, Graphics g = null) private void GlassifyForm(Form form, Graphics g = null)
{ {
if (!(DesktopWindowManager.CompositionEnabled && GetGlassEnabled(form)) && !form.IsDesignMode()) if (!(DesktopWindowManager.CompositionEnabled && GetGlassEnabled(form)) && !form.IsDesignMode())
return; return;
if (g == null) g = form.CreateGraphics(); if (g is null) g = form.CreateGraphics();
GlassFormProperties prop; if (!formProps.TryGetValue(form, out var prop))
if (!formProps.TryGetValue(form, out prop))
return; return;
// Paint the glass effect. // Paint the glass effect.
@ -292,5 +228,15 @@ namespace Vanara.Windows.Forms
form.Resize -= form_Resize; form.Resize -= form_Resize;
form.Paint -= form_Paint; form.Paint -= form_Paint;
} }
/// <summary>Properties for each form that is extended.</summary>
private class GlassFormProperties
{
public Point FormMoveLastMousePos = Point.Empty;
public bool FormMoveTracking = false;
public bool GlassEnabled = true;
public bool GlassMarginMovesForm = true;
public Padding GlassMargins = Padding.Empty;
}
} }
} }