Widow

Subversion Repositories:
Compare Path: Rev
With Path: Rev
?path1? @ 14  →  ?path2? @ 13
File deleted
\ No newline at end of file
/trunk/Widow/WindowModification.cs
/trunk/Widow/Helpers.cs
@@ -48,13 +48,26 @@
return windows;
}
 
public static string GetWindowClass(IntPtr hWnd)
public static IEnumerable<IntPtr> EnumerateWindows()
{
var windowClass = new StringBuilder(256);
Natives.GetClassName(hWnd, windowClass, windowClass.Capacity);
return windowClass.ToString();
var windows = new List<IntPtr>();
 
Natives.EnumWindows(delegate(IntPtr wnd, IntPtr param)
{
windows.Add(wnd);
return true;
}, IntPtr.Zero);
 
return windows;
}
 
/// <summary> Find all windows that contain the given title text </summary>
/// <param name="titleText"> The text that the window title must contain. </param>
public static IEnumerable<IntPtr> FindWindowsWithText(string titleText)
{
return FindWindows((wnd, param) => GetWindowText(wnd).Contains(titleText));
}
 
public static string GetWindowTitle(IntPtr hWnd)
{
var length = Natives.GetWindowTextLength(hWnd) + 1;
/trunk/Widow/Natives.cs
@@ -6,9 +6,6 @@
{
public static class Natives
{
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
public static extern int GetClassName(IntPtr hWnd, StringBuilder lpClassName, int nMaxCount);
 
#region Public Events & Delegates
 
public delegate bool EnumWindowsProc(IntPtr hWnd, IntPtr lParam);
/trunk/Widow/RuleEditForm.cs
@@ -315,52 +315,6 @@
}
}
 
private void DrawButton_Click(object sender, EventArgs e)
{
var selectedWindow = (Window) windowRulesListBox.SelectedItem;
if (selectedWindow == null)
{
return;
}
 
if (DrawOverlayForm != null)
{
return;
}
 
DrawOverlayForm = new DrawOverlayForm();
DrawOverlayForm.WindowDrawn += DrawOverlayForm_WindowDrawn;
DrawOverlayForm.Closed += DrawOverlayForm_Closed;
DrawOverlayForm.Show();
}
 
private void DrawOverlayForm_WindowDrawn(object sender, WindowDrawnEventArgs e)
{
var selectedWindow = (Window) windowRulesListBox.SelectedItem;
if (selectedWindow == null)
{
return;
}
 
WindowLeft.Text = e.Left.ToString();
WindowTop.Text = e.Top.ToString();
WindowWidth.Text = e.Width.ToString();
WindowHeight.Text = e.Height.ToString();
 
selectedWindow.Left = e.Left;
selectedWindow.Top = e.Top;
selectedWindow.Width = e.Width;
selectedWindow.Height = e.Height;
}
 
private void DrawOverlayForm_Closed(object sender, EventArgs e)
{
DrawOverlayForm.Closed -= DrawOverlayForm_Closed;
DrawOverlayForm.WindowDrawn -= DrawOverlayForm_WindowDrawn;
DrawOverlayForm.Dispose();
DrawOverlayForm = null;
}
 
#endregion
 
#region Private Methods
@@ -369,14 +323,14 @@
{
foreach (var handle in Helpers.FindWindows((wnd, param) => true))
{
var windowTitle = Helpers.GetWindowTitle(handle);
if (string.IsNullOrEmpty(windowTitle))
var title = Helpers.GetWindowTitle(handle);
if (string.IsNullOrEmpty(title))
{
continue;
}
 
var windowClass = Helpers.GetWindowClass(handle);
if (string.IsNullOrEmpty(windowClass))
var process = Helpers.GetProcessName(handle);
if (string.IsNullOrEmpty(process))
{
continue;
}
@@ -386,7 +340,7 @@
continue;
}
 
var window = new Window(windowClass, windowTitle, rect.Top, rect.Left, rect.Right - rect.Left,
var window = new Window(process, title, rect.Top, rect.Left, rect.Right - rect.Left,
rect.Bottom - rect.Top);
 
this.Execute(() =>
@@ -412,5 +366,52 @@
}
 
#endregion
 
private void DrawButton_Click(object sender, EventArgs e)
{
var selectedWindow = (Window) windowRulesListBox.SelectedItem;
if (selectedWindow == null)
{
return;
}
 
if (DrawOverlayForm != null)
{
return;
}
 
DrawOverlayForm = new DrawOverlayForm();
DrawOverlayForm.WindowDrawn += DrawOverlayForm_WindowDrawn;
DrawOverlayForm.Closed += DrawOverlayForm_Closed;
DrawOverlayForm.Show();
 
}
 
private void DrawOverlayForm_WindowDrawn(object sender, WindowDrawnEventArgs e)
{
var selectedWindow = (Window) windowRulesListBox.SelectedItem;
if (selectedWindow == null)
{
return;
}
 
WindowLeft.Text = e.Left.ToString();
WindowTop.Text = e.Top.ToString();
WindowWidth.Text = e.Width.ToString();
WindowHeight.Text = e.Height.ToString();
 
selectedWindow.Left = e.Left;
selectedWindow.Top = e.Top;
selectedWindow.Width = e.Width;
selectedWindow.Height = e.Height;
}
 
private void DrawOverlayForm_Closed(object sender, EventArgs e)
{
DrawOverlayForm.Closed -= DrawOverlayForm_Closed;
DrawOverlayForm.WindowDrawn -= DrawOverlayForm_WindowDrawn;
DrawOverlayForm.Dispose();
DrawOverlayForm = null;
}
}
}
/trunk/Widow/Widow.csproj
@@ -96,7 +96,6 @@
<Compile Include="Utilities.cs" />
<Compile Include="WindowCreatedEventArgs.cs" />
<Compile Include="WindowDestroyedEventArgs.cs" />
<Compile Include="WindowModification.cs" />
<EmbeddedResource Include="AboutForm.resx">
<DependentUpon>AboutForm.cs</DependentUpon>
</EmbeddedResource>
/trunk/Widow/WindowManipulation.cs
@@ -11,6 +11,12 @@
{
public class WindowManipulation : IDisposable
{
#region Public Events & Delegates
 
public event EventHandler<WindowManipulatedEventArgs> WindowManipulated;
 
#endregion
 
#region Public Enums, Properties and Fields
 
public bool OnWindowCreate { get; set; }
@@ -31,9 +37,9 @@
 
private Task ApplyEveryTask { get; }
 
private BufferBlock<WindowModification> WindowsBufferBlock { get; }
private BufferBlock<Window> WindowsBufferBlock { get; }
 
private ActionBlock<WindowModification> WindowsActionBlock { get; }
private ActionBlock<Window> WindowsActionBlock { get; }
 
private IDisposable WindowsLink { get; set; }
 
@@ -44,8 +50,8 @@
public WindowManipulation()
{
CancellationTokenSource = new CancellationTokenSource();
WindowsBufferBlock = new BufferBlock<WindowModification>();
WindowsActionBlock = new ActionBlock<WindowModification>(ManipulateWindows);
WindowsBufferBlock = new BufferBlock<Window>();
WindowsActionBlock = new ActionBlock<Window>(ManipulateWindows);
WindowsLink = WindowsBufferBlock.LinkTo(WindowsActionBlock);
 
ApplyEveryTask = ApplyEvery(CancellationTokenSource.Token);
@@ -98,8 +104,7 @@
return;
}
 
var @class = Helpers.GetWindowClass(e.Handle);
if (!WindowsToManipulate.TryGetWindow(new WindowHash(e.Title, @class), out _))
if (!WindowsToManipulate.TryGetWindow(e.Title, out _))
{
return;
}
@@ -113,32 +118,25 @@
 
public async Task Apply()
{
var windows = new List<WindowModification>();
foreach (var hWnd in Helpers.FindWindows((wnd, param) => true))
var windows = new List<Window>();
foreach (var hWnd in Helpers.EnumerateWindows())
{
var windowTitle = Helpers.GetWindowTitle(hWnd);
var title = Helpers.GetWindowTitle(hWnd);
 
if (string.IsNullOrEmpty(windowTitle))
if (string.IsNullOrEmpty(title))
{
continue;
}
 
var windowClass = Helpers.GetWindowClass(hWnd);
if (string.IsNullOrEmpty(windowClass))
if (!WindowsToManipulate.TryGetWindow(title, out var window))
{
continue;
}
 
var windowHash = new WindowHash(windowTitle, windowClass);
if (!WindowsToManipulate.TryGetWindow(windowHash, out var window))
{
continue;
}
 
windows.Add(new WindowModification(hWnd, window));
windows.Add(window);
}
 
await Task.WhenAll(windows.Select(modification => WindowsBufferBlock.SendAsync(modification)));
await Task.WhenAll(windows.Select(window => WindowsBufferBlock.SendAsync(window)));
}
 
#endregion
@@ -145,43 +143,48 @@
 
#region Private Methods
 
private static void ManipulateWindows(WindowModification modification)
private void ManipulateWindows(Window window)
{
if (modification.Handle == IntPtr.Zero)
foreach (var hWnd in Helpers.FindWindowsWithText(window.Title))
{
return;
}
if (hWnd == IntPtr.Zero)
{
continue;
}
 
if (!Natives.GetWindowRect(modification.Handle, out var rect))
{
return;
}
if (!Natives.GetWindowRect(hWnd, out var rect))
{
continue;
}
 
var left = modification.Window.Left;
if (modification.Window.IgnoreLeft)
{
left = rect.Left;
}
var left = window.Left;
if (window.IgnoreLeft)
{
left = rect.Left;
}
 
var top = modification.Window.Top;
if (modification.Window.IgnoreTop)
{
top = rect.Top;
}
var top = window.Top;
if (window.IgnoreTop)
{
top = rect.Top;
}
 
var width = modification.Window.Width;
if (modification.Window.IgnoreWidth)
{
width = rect.Left - rect.Right;
}
var width = window.Width;
if (window.IgnoreWidth)
{
width = rect.Left - rect.Right;
}
 
var height = modification.Window.Height;
if (modification.Window.IgnoreHeight)
{
height = rect.Top - rect.Bottom;
var height = window.Height;
if (window.IgnoreHeight)
{
height = rect.Top - rect.Bottom;
}
 
var success = Natives.MoveWindow(hWnd, left, top, width, height, true);
 
WindowManipulated?.Invoke(this, new WindowManipulatedEventArgs(success, window));
}
 
Natives.MoveWindow(modification.Handle, left, top, width, height, true);
}
 
private async Task ApplyEvery(CancellationToken cancellationToken)
File deleted
\ No newline at end of file
/trunk/Windows/WindowHash.cs
/trunk/Windows/Window.cs
@@ -11,17 +11,17 @@
#region Public Enums, Properties and Fields
 
[XmlElement(ElementName = "Process")]
public string Class
public string Process
{
get => _class;
get => _process;
set
{
if (value == _class)
if (value == _process)
{
return;
}
 
_class = value;
_process = value;
OnPropertyChanged();
}
}
@@ -174,8 +174,6 @@
 
#region Private Delegates, Events, Enums, Properties, Indexers and Fields
 
private string _class;
 
private int _height;
 
private bool _ignoreHeight;
@@ -188,6 +186,8 @@
 
private int _left;
 
private string _process;
 
private string _title;
 
private int _top;
@@ -203,9 +203,9 @@
{
}
 
public Window(string @class, string title, int top, int left, int width, int height) : this()
public Window(string process, string title, int top, int left, int width, int height) : this()
{
Class = @class;
Process = process;
Title = title;
Top = top;
Left = left;
/trunk/Windows/Windows.cs
@@ -34,14 +34,12 @@
{
_window.Add(window);
 
var windowHash = new WindowHash(window.Title, window.Class);
 
if (_windows.ContainsKey(windowHash))
if (_windows.ContainsKey(window.Title))
{
continue;
}
 
_windows.Add(windowHash, window);
_windows.Add(window.Title, window);
}
 
_window.CollectionChanged += Window_CollectionChanged;
@@ -55,7 +53,7 @@
 
private readonly ObservableCollection<Window> _window = new ObservableCollection<Window>();
 
private readonly Dictionary<WindowHash, Window> _windows = new Dictionary<WindowHash, Window>();
private readonly Dictionary<string, Window> _windows = new Dictionary<string, Window>();
 
#endregion
 
@@ -88,11 +86,9 @@
{
foreach (var window in e.OldItems.OfType<Window>())
{
var windowHash = new WindowHash(window.Title, window.Class);
 
if (_windows.ContainsKey(windowHash))
if (_windows.ContainsKey(window.Title))
{
_windows.Remove(windowHash);
_windows.Remove(window.Title);
}
}
}
@@ -102,11 +98,9 @@
{
foreach (var window in e.NewItems.OfType<Window>())
{
var windowHash = new WindowHash(window.Title, window.Class);
 
if (!_windows.ContainsKey(windowHash))
if (!_windows.ContainsKey(window.Title))
{
_windows.Add(windowHash, window);
_windows.Add(window.Title, window);
}
}
}
@@ -116,9 +110,9 @@
 
#region Public Methods
 
public bool TryGetWindow(WindowHash hash, out Window window)
public bool TryGetWindow(string title, out Window window)
{
return _windows.TryGetValue(hash, out window);
return _windows.TryGetValue(title, out window);
}
 
#endregion
/trunk/Windows/Windows.csproj
@@ -43,7 +43,6 @@
<ItemGroup>
<Compile Include="Properties\Annotations.cs" />
<Compile Include="Window.cs" />
<Compile Include="WindowHash.cs" />
<Compile Include="Windows.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>