Widow

Subversion Repositories:
Compare Path: Rev
With Path: Rev
?path1? @ HEAD  →  ?path2? @ 1
/trunk/Widow/RuleEditForm.cs
@@ -1,11 +1,7 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Threading.Tasks;
using System.Windows.Forms;
using Windows;
using Gma.System.MouseKeyHook;
 
namespace Widow
{
@@ -15,19 +11,15 @@
 
public Windows.Windows Windows { get; }
 
public MainForm Form { get; set; }
public Form1 Form { get; set; }
 
public DrawOverlayForm DrawOverlayForm { get; set; }
 
public IKeyboardMouseEvents GlobalMouseHook { get; set; }
 
#endregion
 
#region Constructors, Destructors and Finalizers
 
public RuleEditForm(MainForm mainForm, Windows.Windows windows) : this(windows)
public RuleEditForm(Form1 form1, Windows.Windows windows) : this(windows)
{
Form = mainForm;
Form = form1;
 
Form.WindowCreated += Form_WindowCreated;
Form.WindowDestroyed += Form_WindowDestroyed;
@@ -47,12 +39,10 @@
{
InitializeComponent();
 
desktopWindowsListBox.DisplayMember = nameof(Window.Title);
desktopWindowsListBox.SetDoubleBuffered();
windowRulesListBox.DisplayMember = nameof(Window.Title);
windowRulesListBox.SetDoubleBuffered();
desktopWindowsListBox.DisplayMember = "Name";
windowRulesListBox.DisplayMember = "Name";
 
Task.Run(RefreshWindows);
RefreshWindows();
}
 
/// <summary>
@@ -78,7 +68,7 @@
 
private void Form_WindowDestroyed(object sender, WindowDestroyedEventArgs e)
{
if (string.IsNullOrEmpty(e.Title))
if (string.IsNullOrEmpty(e.Name))
{
return;
}
@@ -86,7 +76,7 @@
var indices = new List<int>();
for (var i = 0; i < desktopWindowsListBox.Items.Count; ++i)
{
if (((Window) desktopWindowsListBox.Items[i]).Title == e.Title)
if (((Window) desktopWindowsListBox.Items[i]).Name == e.Name)
{
indices.Add(i);
}
@@ -100,7 +90,7 @@
 
private void Form_WindowCreated(object sender, WindowCreatedEventArgs e)
{
if (string.IsNullOrEmpty(e.Title))
if (string.IsNullOrEmpty(e.Name))
{
return;
}
@@ -108,7 +98,7 @@
var found = false;
foreach (var window in desktopWindowsListBox.Items)
{
if (((Window) window).Title == e.Title)
if (((Window) window).Name == e.Name)
{
found = true;
break;
@@ -120,22 +110,13 @@
return;
}
 
if (!Natives.GetWindowRect(e.Handle, out var rect))
if (Natives.GetWindowRect(e.Handle, out var rect))
{
return;
}
var window = new Window(e.Name, rect.Top, rect.Left, rect.Right - rect.Left,
rect.Bottom - rect.Top);
 
var process = Helpers.GetProcessName(e.Handle);
 
if (string.IsNullOrEmpty(process))
{
return;
desktopWindowsListBox.Items.Add(window);
}
 
var newWindow = new Window(process, e.Title, rect.Top, rect.Left, rect.Right - rect.Left,
rect.Bottom - rect.Top);
 
desktopWindowsListBox.Items.Add(newWindow);
}
 
private void WindowRulesListBox_SelectedIndexChanged(object sender, EventArgs e)
@@ -144,14 +125,10 @@
 
if (listBox.SelectedIndex == -1)
{
WindowTitle.Text = string.Empty;
ignoreLeftCheckBox.Checked = false;
WindowName.Text = string.Empty;
WindowLeft.Text = string.Empty;
ignoreTopCheckBox.Checked = false;
WindowTop.Text = string.Empty;
ignoreWidthCheckBox.Checked = false;
WindowWidth.Text = string.Empty;
ignoreHeightCheckBox.Checked = false;
WindowHeight.Text = string.Empty;
return;
}
@@ -163,20 +140,16 @@
return;
}
 
WindowTitle.Text = window.Title;
ignoreLeftCheckBox.Checked = window.IgnoreLeft;
WindowName.Text = window.Name;
WindowLeft.Text = window.Left.ToString();
ignoreTopCheckBox.Checked = window.IgnoreTop;
WindowTop.Text = window.Top.ToString();
ignoreWidthCheckBox.Checked = window.IgnoreWidth;
WindowWidth.Text = window.Width.ToString();
ignoreHeightCheckBox.Checked = window.IgnoreHeight;
WindowHeight.Text = window.Height.ToString();
}
 
private void RefreshButton_Click(object sender, EventArgs e)
{
Task.Run(RefreshWindows);
RefreshWindows();
}
 
private void RemoveButton_Click(object sender, EventArgs e)
@@ -190,13 +163,11 @@
var index = -1;
for (var i = 0; i < windowRulesListBox.Items.Count; ++i)
{
if (((Window) windowRulesListBox.Items[i]).Title != window.Title)
if (((Window) windowRulesListBox.Items[i]).Name == window.Name)
{
continue;
index = i;
break;
}
 
index = i;
break;
}
 
if (index == -1)
@@ -219,13 +190,11 @@
var found = false;
foreach (var windowRule in windowRulesListBox.Items)
{
if (((Window) windowRule).Title != window.Title)
if (((Window) windowRule).Name == window.Name)
{
continue;
found = true;
break;
}
 
found = true;
break;
}
 
if (found)
@@ -243,7 +212,7 @@
 
if (selectedWindow == null)
{
WindowTitle.Text = string.Empty;
WindowName.Text = string.Empty;
WindowLeft.Text = string.Empty;
WindowTop.Text = string.Empty;
WindowWidth.Text = string.Empty;
@@ -255,8 +224,8 @@
var textBox = (TextBox) sender;
switch (textBox.Name)
{
case nameof(WindowTitle):
selectedWindow.Title = textBox.Text;
case nameof(WindowName):
selectedWindow.Name = textBox.Text;
break;
case nameof(WindowLeft):
if (int.TryParse(textBox.Text, out var left))
@@ -289,162 +258,6 @@
}
}
 
private void OnIgnoreWindowSettings_CheckedChanged(object sender, EventArgs e)
{
var selectedWindow = (Window) windowRulesListBox.SelectedItem;
 
if (selectedWindow == null)
{
ignoreLeftCheckBox.Checked = false;
ignoreTopCheckBox.Checked = false;
ignoreWidthCheckBox.Checked = false;
ignoreHeightCheckBox.Checked = false;
 
return;
}
 
var checkBox = (CheckBox) sender;
switch (checkBox.Name)
{
case nameof(ignoreHeightCheckBox):
selectedWindow.IgnoreHeight = checkBox.Checked;
break;
case nameof(ignoreWidthCheckBox):
selectedWindow.IgnoreWidth = checkBox.Checked;
break;
case nameof(ignoreTopCheckBox):
selectedWindow.IgnoreTop = checkBox.Checked;
break;
case nameof(ignoreLeftCheckBox):
selectedWindow.IgnoreLeft = checkBox.Checked;
break;
}
}
 
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;
}
 
private void PickButton_Click(object sender, EventArgs e)
{
// Start global mouse key hook.
GlobalMouseHook = Hook.GlobalEvents();
GlobalMouseHook.MouseClick += GlobalMouseHook_MouseClick;
 
// Set cross cursor.
foreach (var cursor in Enum.GetValues(typeof(Natives.OCR_SYSTEM_CURSORS)))
{
Natives.SetSystemCursor(
Natives.CopyIcon(Natives.LoadCursor(IntPtr.Zero, (int) Natives.OCR_SYSTEM_CURSORS.OCR_CROSS)),
(uint) cursor);
}
}
 
private void GlobalMouseHook_MouseClick(object sender, MouseEventArgs e)
{
var hWnd = Natives.WindowFromPoint(new Point(e.X, e.Y));
Natives.GetWindowThreadProcessId(hWnd, out var pid);
Process process;
try
{
process = Process.GetProcessById((int) pid);
}
catch
{
return;
}
 
var windowTitle = Helpers.GetWindowTitle(process.MainWindowHandle);
if (string.IsNullOrEmpty(windowTitle))
{
return;
}
 
var windowClass = Helpers.GetWindowClass(process.MainWindowHandle);
if (string.IsNullOrEmpty(windowClass))
{
return;
}
 
if (!Natives.GetWindowRect(process.MainWindowHandle, out var rect))
{
return;
}
 
var window = new Window(windowClass, windowTitle, rect.Top, rect.Left, rect.Right - rect.Left,
rect.Bottom - rect.Top);
 
var found = false;
foreach (var windowRule in windowRulesListBox.Items)
{
if (((Window) windowRule).Title != window.Title)
{
continue;
}
 
found = true;
break;
}
 
if (found)
{
return;
}
 
windowRulesListBox.Items.Add(window);
Windows.Window.Add(window);
 
// Revert cursor.
Natives.SystemParametersInfo(0x0057, 0, null, 0);
 
// Remove global mouse key hook.
GlobalMouseHook.MouseClick -= GlobalMouseHook_MouseClick;
GlobalMouseHook.Dispose();
GlobalMouseHook = null;
}
 
#endregion
 
#region Private Methods
@@ -451,40 +264,26 @@
 
private void RefreshWindows()
{
foreach (var handle in Helpers.FindWindows((wnd, param) => true))
foreach (var desktopWindow in Helpers.GetDesktopWindows())
{
var windowTitle = Helpers.GetWindowTitle(handle);
if (string.IsNullOrEmpty(windowTitle))
if (string.IsNullOrEmpty(desktopWindow.Title))
{
continue;
}
 
var windowClass = Helpers.GetWindowClass(handle);
if (string.IsNullOrEmpty(windowClass))
if (Natives.GetWindowRect(desktopWindow.Handle, out var rect))
{
continue;
}
var window = new Window(desktopWindow.Title, rect.Top, rect.Left, rect.Right - rect.Left,
rect.Bottom - rect.Top);
 
if (!Natives.GetWindowRect(handle, out var rect))
{
continue;
}
 
var window = new Window(windowClass, windowTitle, rect.Top, rect.Left, rect.Right - rect.Left,
rect.Bottom - rect.Top);
 
this.InvokeIfRequired(form =>
{
var found = false;
foreach (var item in desktopWindowsListBox.Items)
{
if (((Window) item).Title != window.Title)
if (((Window) item).Name == window.Name)
{
continue;
found = true;
break;
}
 
found = true;
break;
}
 
if (!found)
@@ -491,7 +290,7 @@
{
desktopWindowsListBox.Items.Add(window);
}
});
}
}
}