Widow – Rev 9
?pathlinks?
using System;
using System.Threading;
using System.Threading.Tasks;
using Widow.Properties;
namespace Widow
{
public class WindowManipulation : IDisposable
{
#region Public Enums, Properties and Fields
public bool OnWindowCreate { get; set; }
public int ApplyEveryTime { get; set; }
public bool ApplyEveryTimeEnabled { get; set; }
#endregion
#region Private Delegates, Events, Enums, Properties, Indexers and Fields
private MainForm Form { get; }
private Windows.Windows Windows { get; }
private CancellationTokenSource CancellationTokenSource { get; set; }
private Task ApplyEveryTask { get; }
#endregion
#region Constructors, Destructors and Finalizers
public WindowManipulation()
{
CancellationTokenSource = new CancellationTokenSource();
ApplyEveryTask = ApplyEvery(CancellationTokenSource.Token);
}
public WindowManipulation(MainForm mainForm) : this()
{
Form = mainForm;
Form.WindowCreated += Form_WindowCreated;
}
public WindowManipulation(MainForm mainForm, Windows.Windows windows) : this(mainForm)
{
Windows = windows;
}
public WindowManipulation(MainForm mainForm, Windows.Windows windows, Settings settings) : this(mainForm,
windows)
{
OnWindowCreate = settings.OnWindowCreate;
ApplyEveryTimeEnabled = settings.ApplyEveryTimeEnabled;
if (int.TryParse(settings.ApplyEveryTime, out var time))
{
ApplyEveryTime = time;
}
}
public void Dispose()
{
Form.WindowCreated -= Form_WindowCreated;
CancellationTokenSource.Cancel();
ApplyEveryTask.Wait();
CancellationTokenSource.Dispose();
CancellationTokenSource = null;
}
#endregion
#region Event Handlers
private void Form_WindowCreated(object sender, WindowCreatedEventArgs e)
{
if (!OnWindowCreate)
{
return;
}
if (!Windows.Contains(e.Name))
{
return;
}
Apply();
}
#endregion
#region Public Methods
public void Apply()
{
foreach (var window in Windows.Window)
{
var hWnd = Helpers.FindWindowByTitle(window.Name);
if (hWnd == IntPtr.Zero)
{
continue;
}
if (!Natives.GetWindowRect(hWnd, out var rect))
{
continue;
}
var left = window.Left;
if (window.IgnoreLeft)
{
left = rect.Left;
}
var top = window.Top;
if (window.IgnoreTop)
{
top = rect.Top;
}
var width = window.Width;
if (window.IgnoreWidth)
{
width = rect.Left - rect.Right;
}
var height = window.Height;
if (window.IgnoreHeight)
{
height = rect.Top - rect.Bottom;
}
Natives.MoveWindow(hWnd, left, top, width, height, true);
}
}
#endregion
#region Private Methods
private async Task ApplyEvery(CancellationToken cancellationToken)
{
do
{
if (!ApplyEveryTimeEnabled)
{
await Task.Delay(1000, cancellationToken);
continue;
}
await Task.Delay(ApplyEveryTime, cancellationToken);
Apply();
} while (!cancellationToken.IsCancellationRequested);
}
#endregion
}
}
Generated by GNU Enscript 1.6.5.90.