Widow – Rev 11
?pathlinks?
using System;
using System.ComponentModel;
using System.Configuration;
using System.Globalization;
using System.Threading.Tasks;
using System.Windows.Forms;
using Widow.Properties;
using Widow.Serialization;
namespace Widow
{
public partial class MainForm : Form
{
#region Public Events & Delegates
public event EventHandler<WindowCreatedEventArgs> WindowCreated;
public event EventHandler<WindowDestroyedEventArgs> WindowDestroyed;
#endregion
#region Public Enums, Properties and Fields
public Windows.Windows Windows { get; set; }
public RuleEditForm RuleEditForm { get; set; }
public WindowManipulation WindowManipulation { get; set; }
public AboutForm AboutForm { get; set; }
#endregion
#region Constructors, Destructors and Finalizers
public MainForm()
{
InitializeComponent();
// Bind to settings changed event.
Settings.Default.SettingsLoaded += Default_SettingsLoaded;
Settings.Default.SettingsSaving += Default_SettingsSaving;
Settings.Default.PropertyChanged += Default_PropertyChanged;
Natives.RegisterShellHookWindow(Handle);
LoadWindows().ContinueWith(task =>
{
WindowManipulation = new WindowManipulation(this, Windows, Settings.Default);
});
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && components != null)
{
Settings.Default.SettingsLoaded -= Default_SettingsLoaded;
Settings.Default.SettingsSaving -= Default_SettingsSaving;
Settings.Default.PropertyChanged -= Default_PropertyChanged;
components.Dispose();
}
base.Dispose(disposing);
}
#endregion
#region Private Overrides
protected override void WndProc(ref Message m)
{
var handle = m.LParam;
var windowName = Helpers.GetWindowTitle(handle);
switch (m.WParam.ToInt32())
{
case (int) Natives.WM.HSHELL_WINDOWCREATED:
WindowCreated?.Invoke(this, new WindowCreatedEventArgs(windowName, handle));
break;
case (int) Natives.WM.HSHELL_WINDOWDESTROYED:
WindowDestroyed?.Invoke(this, new WindowDestroyedEventArgs(windowName, handle));
break;
}
base.WndProc(ref m);
}
#endregion
#region Event Handlers
private static void Default_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
Settings.Default.Save();
}
private static void Default_SettingsSaving(object sender, CancelEventArgs e)
{
}
private static void Default_SettingsLoaded(object sender, SettingsLoadedEventArgs e)
{
}
private void NewToolStripMenuItem_Click(object sender, EventArgs e)
{
if (RuleEditForm != null)
{
return;
}
RuleEditForm = new RuleEditForm(this, Windows);
RuleEditForm.Closed += RuleEditForm_Closed;
RuleEditForm.Show();
}
private async void RuleEditForm_Closed(object sender, EventArgs e)
{
RuleEditForm.Closed -= RuleEditForm_Closed;
RuleEditForm.Dispose();
RuleEditForm = null;
switch (await WindowsSerialization.Serialize(Windows, "Windows.xml"))
{
case SerializationSuccess serializationSuccess:
break;
case SerializationFailure serializationFailure:
break;
}
}
private void ExitToolStripMenuItem_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void LaunchOnBootToolStripMenuItem_CheckedChanged(object sender, EventArgs e)
{
Settings.Default.LaunchOnBoot = ((ToolStripMenuItem) sender).Checked;
LaunchOnBoot.Set(Settings.Default.LaunchOnBoot);
}
private void OnWindowCreateToolStripMenuItem_CheckedChanged(object sender, EventArgs e)
{
Settings.Default.OnWindowCreate = ((ToolStripMenuItem) sender).Checked;
WindowManipulation.OnWindowCreate = Settings.Default.OnWindowCreate;
}
private void AboutToolStripMenuItem_Click(object sender, EventArgs e)
{
// Show the about form.
AboutForm = new AboutForm();
AboutForm.Show();
}
private async void ApplyNowToolStripMenuItem_Click(object sender, EventArgs e)
{
await WindowManipulation.Apply();
}
private void OnEveryToolStripMenuItem_CheckedChanged(object sender, EventArgs e)
{
var toolStripMenuItem = (ToolStripMenuItem) sender;
if (!int.TryParse(toolStripTextBox1.Text, out var time))
{
return;
}
Settings.Default.ApplyEveryTimeEnabled = toolStripMenuItem.Checked;
Settings.Default.ApplyEveryTime = time.ToString(CultureInfo.InvariantCulture);
WindowManipulation.ApplyEveryTime = time;
WindowManipulation.ApplyEveryTimeEnabled = toolStripMenuItem.Checked;
}
#endregion
#region Private Methods
private async Task LoadWindows()
{
switch (await WindowsSerialization.Deserialize("Windows.xml"))
{
case SerializationSuccess serializationSuccess:
Windows = serializationSuccess.Windows;
break;
case SerializationFailure serializationFailure:
Windows = new Windows.Windows();
break;
}
}
#endregion
private void NotifyIcon1_DoubleClick(object sender, EventArgs e)
{
if (RuleEditForm != null)
{
return;
}
RuleEditForm = new RuleEditForm(this, Windows);
RuleEditForm.Closed += RuleEditForm_Closed;
RuleEditForm.Show();
}
private async void NotifyIcon1_Click(object sender, EventArgs e)
{
await WindowManipulation.Apply();
}
}
}
Generated by GNU Enscript 1.6.5.90.