Widow – Blame information for rev 21
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | office | 1 | using System; |
2 | using System.Collections.Generic; |
||
18 | office | 3 | using System.Diagnostics; |
4 | using System.Drawing; |
||
11 | office | 5 | using System.Threading.Tasks; |
1 | office | 6 | using System.Windows.Forms; |
7 | using Windows; |
||
18 | office | 8 | using Gma.System.MouseKeyHook; |
1 | office | 9 | |
10 | namespace Widow |
||
11 | { |
||
12 | public partial class RuleEditForm : Form |
||
13 | { |
||
14 | #region Public Enums, Properties and Fields |
||
15 | |||
16 | public Windows.Windows Windows { get; } |
||
17 | |||
6 | office | 18 | public MainForm Form { get; set; } |
1 | office | 19 | |
13 | office | 20 | public DrawOverlayForm DrawOverlayForm { get; set; } |
21 | |||
18 | office | 22 | public IKeyboardMouseEvents GlobalMouseHook { get; set; } |
23 | |||
1 | office | 24 | #endregion |
25 | |||
26 | #region Constructors, Destructors and Finalizers |
||
27 | |||
6 | office | 28 | public RuleEditForm(MainForm mainForm, Windows.Windows windows) : this(windows) |
1 | office | 29 | { |
6 | office | 30 | Form = mainForm; |
1 | office | 31 | |
32 | Form.WindowCreated += Form_WindowCreated; |
||
33 | Form.WindowDestroyed += Form_WindowDestroyed; |
||
34 | } |
||
35 | |||
36 | public RuleEditForm(Windows.Windows windows) : this() |
||
37 | { |
||
38 | Windows = windows; |
||
39 | |||
40 | foreach (var window in windows.Window) |
||
41 | { |
||
42 | windowRulesListBox.Items.Add(window); |
||
43 | } |
||
44 | } |
||
45 | |||
46 | private RuleEditForm() |
||
47 | { |
||
48 | InitializeComponent(); |
||
49 | |||
11 | office | 50 | desktopWindowsListBox.DisplayMember = nameof(Window.Title); |
51 | desktopWindowsListBox.SetDoubleBuffered(); |
||
52 | windowRulesListBox.DisplayMember = nameof(Window.Title); |
||
53 | windowRulesListBox.SetDoubleBuffered(); |
||
1 | office | 54 | |
11 | office | 55 | Task.Run(RefreshWindows); |
1 | office | 56 | } |
57 | |||
58 | /// <summary> |
||
59 | /// Clean up any resources being used. |
||
60 | /// </summary> |
||
61 | /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> |
||
62 | protected override void Dispose(bool disposing) |
||
63 | { |
||
64 | if (disposing && components != null) |
||
65 | { |
||
66 | Form.WindowCreated -= Form_WindowCreated; |
||
67 | Form.WindowDestroyed -= Form_WindowDestroyed; |
||
68 | |||
69 | components.Dispose(); |
||
70 | } |
||
71 | |||
72 | base.Dispose(disposing); |
||
73 | } |
||
74 | |||
75 | #endregion |
||
76 | |||
77 | #region Event Handlers |
||
78 | |||
79 | private void Form_WindowDestroyed(object sender, WindowDestroyedEventArgs e) |
||
80 | { |
||
11 | office | 81 | if (string.IsNullOrEmpty(e.Title)) |
1 | office | 82 | { |
83 | return; |
||
84 | } |
||
85 | |||
86 | var indices = new List<int>(); |
||
87 | for (var i = 0; i < desktopWindowsListBox.Items.Count; ++i) |
||
88 | { |
||
11 | office | 89 | if (((Window) desktopWindowsListBox.Items[i]).Title == e.Title) |
1 | office | 90 | { |
91 | indices.Add(i); |
||
92 | } |
||
93 | } |
||
94 | |||
95 | foreach (var index in indices) |
||
96 | { |
||
97 | desktopWindowsListBox.Items.RemoveAt(index); |
||
98 | } |
||
99 | } |
||
100 | |||
101 | private void Form_WindowCreated(object sender, WindowCreatedEventArgs e) |
||
102 | { |
||
11 | office | 103 | if (string.IsNullOrEmpty(e.Title)) |
1 | office | 104 | { |
105 | return; |
||
106 | } |
||
107 | |||
108 | var found = false; |
||
109 | foreach (var window in desktopWindowsListBox.Items) |
||
110 | { |
||
11 | office | 111 | if (((Window) window).Title == e.Title) |
1 | office | 112 | { |
113 | found = true; |
||
114 | break; |
||
115 | } |
||
116 | } |
||
117 | |||
118 | if (found) |
||
119 | { |
||
120 | return; |
||
121 | } |
||
122 | |||
11 | office | 123 | if (!Natives.GetWindowRect(e.Handle, out var rect)) |
1 | office | 124 | { |
11 | office | 125 | return; |
126 | } |
||
1 | office | 127 | |
11 | office | 128 | var process = Helpers.GetProcessName(e.Handle); |
129 | if (string.IsNullOrEmpty(process)) |
||
130 | { |
||
131 | return; |
||
1 | office | 132 | } |
11 | office | 133 | |
134 | var newWindow = new Window(process, e.Title, rect.Top, rect.Left, rect.Right - rect.Left, |
||
135 | rect.Bottom - rect.Top); |
||
136 | |||
137 | desktopWindowsListBox.Items.Add(newWindow); |
||
1 | office | 138 | } |
139 | |||
140 | private void WindowRulesListBox_SelectedIndexChanged(object sender, EventArgs e) |
||
141 | { |
||
142 | var listBox = (ListBox) sender; |
||
143 | |||
144 | if (listBox.SelectedIndex == -1) |
||
145 | { |
||
11 | office | 146 | WindowTitle.Text = string.Empty; |
9 | office | 147 | ignoreLeftCheckBox.Checked = false; |
1 | office | 148 | WindowLeft.Text = string.Empty; |
9 | office | 149 | ignoreTopCheckBox.Checked = false; |
1 | office | 150 | WindowTop.Text = string.Empty; |
9 | office | 151 | ignoreWidthCheckBox.Checked = false; |
1 | office | 152 | WindowWidth.Text = string.Empty; |
9 | office | 153 | ignoreHeightCheckBox.Checked = false; |
1 | office | 154 | WindowHeight.Text = string.Empty; |
155 | return; |
||
156 | } |
||
157 | |||
158 | var window = (Window) listBox.SelectedItem; |
||
159 | |||
160 | if (window == null) |
||
161 | { |
||
162 | return; |
||
163 | } |
||
164 | |||
11 | office | 165 | WindowTitle.Text = window.Title; |
9 | office | 166 | ignoreLeftCheckBox.Checked = window.IgnoreLeft; |
1 | office | 167 | WindowLeft.Text = window.Left.ToString(); |
9 | office | 168 | ignoreTopCheckBox.Checked = window.IgnoreTop; |
1 | office | 169 | WindowTop.Text = window.Top.ToString(); |
9 | office | 170 | ignoreWidthCheckBox.Checked = window.IgnoreWidth; |
1 | office | 171 | WindowWidth.Text = window.Width.ToString(); |
9 | office | 172 | ignoreHeightCheckBox.Checked = window.IgnoreHeight; |
1 | office | 173 | WindowHeight.Text = window.Height.ToString(); |
174 | } |
||
175 | |||
176 | private void RefreshButton_Click(object sender, EventArgs e) |
||
177 | { |
||
11 | office | 178 | Task.Run(RefreshWindows); |
1 | office | 179 | } |
180 | |||
181 | private void RemoveButton_Click(object sender, EventArgs e) |
||
182 | { |
||
183 | var window = (Window) windowRulesListBox.SelectedItem; |
||
184 | if (window == null) |
||
185 | { |
||
186 | return; |
||
187 | } |
||
188 | |||
189 | var index = -1; |
||
190 | for (var i = 0; i < windowRulesListBox.Items.Count; ++i) |
||
191 | { |
||
11 | office | 192 | if (((Window) windowRulesListBox.Items[i]).Title != window.Title) |
1 | office | 193 | { |
11 | office | 194 | continue; |
1 | office | 195 | } |
11 | office | 196 | |
197 | index = i; |
||
198 | break; |
||
1 | office | 199 | } |
200 | |||
201 | if (index == -1) |
||
202 | { |
||
203 | return; |
||
204 | } |
||
205 | |||
206 | windowRulesListBox.Items.RemoveAt(index); |
||
207 | Windows.Window.Remove(window); |
||
208 | } |
||
209 | |||
210 | private void AddButton_Click(object sender, EventArgs e) |
||
211 | { |
||
212 | var window = (Window) desktopWindowsListBox.SelectedItem; |
||
213 | if (window == null) |
||
214 | { |
||
215 | return; |
||
216 | } |
||
217 | |||
218 | var found = false; |
||
219 | foreach (var windowRule in windowRulesListBox.Items) |
||
220 | { |
||
11 | office | 221 | if (((Window) windowRule).Title != window.Title) |
1 | office | 222 | { |
11 | office | 223 | continue; |
1 | office | 224 | } |
11 | office | 225 | |
226 | found = true; |
||
227 | break; |
||
1 | office | 228 | } |
229 | |||
230 | if (found) |
||
231 | { |
||
232 | return; |
||
233 | } |
||
234 | |||
235 | windowRulesListBox.Items.Add(window); |
||
236 | Windows.Window.Add(window); |
||
237 | } |
||
238 | |||
239 | private void OnWindowSettings_TextChanged(object sender, EventArgs e) |
||
240 | { |
||
241 | var selectedWindow = (Window) windowRulesListBox.SelectedItem; |
||
242 | |||
243 | if (selectedWindow == null) |
||
244 | { |
||
11 | office | 245 | WindowTitle.Text = string.Empty; |
1 | office | 246 | WindowLeft.Text = string.Empty; |
247 | WindowTop.Text = string.Empty; |
||
248 | WindowWidth.Text = string.Empty; |
||
249 | WindowHeight.Text = string.Empty; |
||
250 | |||
251 | return; |
||
252 | } |
||
253 | |||
254 | var textBox = (TextBox) sender; |
||
255 | switch (textBox.Name) |
||
256 | { |
||
11 | office | 257 | case nameof(WindowTitle): |
258 | selectedWindow.Title = textBox.Text; |
||
1 | office | 259 | break; |
260 | case nameof(WindowLeft): |
||
261 | if (int.TryParse(textBox.Text, out var left)) |
||
262 | { |
||
263 | selectedWindow.Left = left; |
||
264 | } |
||
265 | |||
266 | break; |
||
267 | case nameof(WindowTop): |
||
268 | if (int.TryParse(textBox.Text, out var top)) |
||
269 | { |
||
270 | selectedWindow.Top = top; |
||
271 | } |
||
272 | |||
273 | break; |
||
274 | case nameof(WindowWidth): |
||
275 | if (int.TryParse(textBox.Text, out var width)) |
||
276 | { |
||
277 | selectedWindow.Width = width; |
||
278 | } |
||
279 | |||
280 | break; |
||
281 | case nameof(WindowHeight): |
||
282 | if (int.TryParse(textBox.Text, out var height)) |
||
283 | { |
||
284 | selectedWindow.Height = height; |
||
285 | } |
||
286 | |||
287 | break; |
||
288 | } |
||
289 | } |
||
290 | |||
9 | office | 291 | private void OnIgnoreWindowSettings_CheckedChanged(object sender, EventArgs e) |
292 | { |
||
293 | var selectedWindow = (Window) windowRulesListBox.SelectedItem; |
||
294 | |||
295 | if (selectedWindow == null) |
||
296 | { |
||
297 | ignoreLeftCheckBox.Checked = false; |
||
298 | ignoreTopCheckBox.Checked = false; |
||
299 | ignoreWidthCheckBox.Checked = false; |
||
300 | ignoreHeightCheckBox.Checked = false; |
||
301 | |||
302 | return; |
||
303 | } |
||
304 | |||
305 | var checkBox = (CheckBox) sender; |
||
306 | switch (checkBox.Name) |
||
307 | { |
||
308 | case nameof(ignoreHeightCheckBox): |
||
309 | selectedWindow.IgnoreHeight = checkBox.Checked; |
||
310 | break; |
||
311 | case nameof(ignoreWidthCheckBox): |
||
312 | selectedWindow.IgnoreWidth = checkBox.Checked; |
||
313 | break; |
||
314 | case nameof(ignoreTopCheckBox): |
||
315 | selectedWindow.IgnoreTop = checkBox.Checked; |
||
316 | break; |
||
317 | case nameof(ignoreLeftCheckBox): |
||
318 | selectedWindow.IgnoreLeft = checkBox.Checked; |
||
319 | break; |
||
320 | } |
||
321 | } |
||
322 | |||
14 | office | 323 | private void DrawButton_Click(object sender, EventArgs e) |
324 | { |
||
325 | var selectedWindow = (Window) windowRulesListBox.SelectedItem; |
||
326 | if (selectedWindow == null) |
||
327 | { |
||
328 | return; |
||
329 | } |
||
330 | |||
331 | if (DrawOverlayForm != null) |
||
332 | { |
||
333 | return; |
||
334 | } |
||
335 | |||
336 | DrawOverlayForm = new DrawOverlayForm(); |
||
337 | DrawOverlayForm.WindowDrawn += DrawOverlayForm_WindowDrawn; |
||
338 | DrawOverlayForm.Closed += DrawOverlayForm_Closed; |
||
339 | DrawOverlayForm.Show(); |
||
340 | } |
||
341 | |||
342 | private void DrawOverlayForm_WindowDrawn(object sender, WindowDrawnEventArgs e) |
||
343 | { |
||
344 | var selectedWindow = (Window) windowRulesListBox.SelectedItem; |
||
345 | if (selectedWindow == null) |
||
346 | { |
||
347 | return; |
||
348 | } |
||
349 | |||
350 | WindowLeft.Text = e.Left.ToString(); |
||
351 | WindowTop.Text = e.Top.ToString(); |
||
352 | WindowWidth.Text = e.Width.ToString(); |
||
353 | WindowHeight.Text = e.Height.ToString(); |
||
354 | |||
355 | selectedWindow.Left = e.Left; |
||
356 | selectedWindow.Top = e.Top; |
||
357 | selectedWindow.Width = e.Width; |
||
358 | selectedWindow.Height = e.Height; |
||
359 | } |
||
360 | |||
361 | private void DrawOverlayForm_Closed(object sender, EventArgs e) |
||
362 | { |
||
363 | DrawOverlayForm.Closed -= DrawOverlayForm_Closed; |
||
364 | DrawOverlayForm.WindowDrawn -= DrawOverlayForm_WindowDrawn; |
||
365 | DrawOverlayForm.Dispose(); |
||
366 | DrawOverlayForm = null; |
||
367 | } |
||
368 | |||
18 | office | 369 | private void PickButton_Click(object sender, EventArgs e) |
370 | { |
||
371 | // Start global mouse key hook. |
||
372 | GlobalMouseHook = Hook.GlobalEvents(); |
||
373 | GlobalMouseHook.MouseClick += GlobalMouseHook_MouseClick; |
||
374 | |||
375 | // Set cross cursor. |
||
376 | foreach (var cursor in Enum.GetValues(typeof(Natives.OCR_SYSTEM_CURSORS))) |
||
377 | { |
||
378 | Natives.SetSystemCursor( |
||
379 | Natives.CopyIcon(Natives.LoadCursor(IntPtr.Zero, (int) Natives.OCR_SYSTEM_CURSORS.OCR_CROSS)), |
||
380 | (uint) cursor); |
||
381 | } |
||
382 | } |
||
383 | |||
384 | private void GlobalMouseHook_MouseClick(object sender, MouseEventArgs e) |
||
385 | { |
||
386 | var hWnd = Natives.WindowFromPoint(new Point(e.X, e.Y)); |
||
387 | Natives.GetWindowThreadProcessId(hWnd, out var pid); |
||
388 | var process = Process.GetProcessById((int) pid); |
||
389 | var windowTitle = Helpers.GetWindowTitle(process.MainWindowHandle); |
||
390 | if (string.IsNullOrEmpty(windowTitle)) |
||
391 | { |
||
392 | return; |
||
393 | } |
||
394 | |||
395 | var windowClass = Helpers.GetWindowClass(process.MainWindowHandle); |
||
396 | if (string.IsNullOrEmpty(windowClass)) |
||
397 | { |
||
398 | return; |
||
399 | } |
||
400 | |||
401 | if (!Natives.GetWindowRect(process.MainWindowHandle, out var rect)) |
||
402 | { |
||
403 | return; |
||
404 | } |
||
405 | |||
406 | var window = new Window(windowClass, windowTitle, rect.Top, rect.Left, rect.Right - rect.Left, |
||
407 | rect.Bottom - rect.Top); |
||
408 | |||
409 | var found = false; |
||
410 | foreach (var windowRule in windowRulesListBox.Items) |
||
411 | { |
||
412 | if (((Window) windowRule).Title != window.Title) |
||
413 | { |
||
414 | continue; |
||
415 | } |
||
416 | |||
417 | found = true; |
||
418 | break; |
||
419 | } |
||
420 | |||
421 | if (found) |
||
422 | { |
||
423 | return; |
||
424 | } |
||
425 | |||
426 | windowRulesListBox.Items.Add(window); |
||
427 | Windows.Window.Add(window); |
||
428 | |||
429 | // Revert cursor. |
||
430 | Natives.SystemParametersInfo(0x0057, 0, null, 0); |
||
431 | |||
432 | // Remove global mouse key hook. |
||
433 | GlobalMouseHook.MouseClick -= GlobalMouseHook_MouseClick; |
||
434 | GlobalMouseHook.Dispose(); |
||
435 | GlobalMouseHook = null; |
||
436 | } |
||
437 | |||
1 | office | 438 | #endregion |
439 | |||
440 | #region Private Methods |
||
441 | |||
442 | private void RefreshWindows() |
||
443 | { |
||
11 | office | 444 | foreach (var handle in Helpers.FindWindows((wnd, param) => true)) |
1 | office | 445 | { |
14 | office | 446 | var windowTitle = Helpers.GetWindowTitle(handle); |
447 | if (string.IsNullOrEmpty(windowTitle)) |
||
1 | office | 448 | { |
449 | continue; |
||
450 | } |
||
451 | |||
14 | office | 452 | var windowClass = Helpers.GetWindowClass(handle); |
453 | if (string.IsNullOrEmpty(windowClass)) |
||
1 | office | 454 | { |
11 | office | 455 | continue; |
456 | } |
||
1 | office | 457 | |
11 | office | 458 | if (!Natives.GetWindowRect(handle, out var rect)) |
459 | { |
||
460 | continue; |
||
461 | } |
||
462 | |||
14 | office | 463 | var window = new Window(windowClass, windowTitle, rect.Top, rect.Left, rect.Right - rect.Left, |
11 | office | 464 | rect.Bottom - rect.Top); |
465 | |||
21 | office | 466 | this.InvokeIfRequired(form => |
11 | office | 467 | { |
1 | office | 468 | var found = false; |
469 | foreach (var item in desktopWindowsListBox.Items) |
||
470 | { |
||
11 | office | 471 | if (((Window) item).Title != window.Title) |
1 | office | 472 | { |
11 | office | 473 | continue; |
1 | office | 474 | } |
11 | office | 475 | |
476 | found = true; |
||
477 | break; |
||
1 | office | 478 | } |
479 | |||
480 | if (!found) |
||
481 | { |
||
482 | desktopWindowsListBox.Items.Add(window); |
||
483 | } |
||
11 | office | 484 | }); |
1 | office | 485 | } |
486 | } |
||
487 | |||
488 | #endregion |
||
489 | } |
||
490 | } |