Widow – Blame information for rev 26

Subversion Repositories:
Rev:
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  
26 office 128 var process = Helpers.GetProcessName(e.Handle);
24 office 129  
11 office 130 if (string.IsNullOrEmpty(process))
131 {
132 return;
1 office 133 }
11 office 134  
135 var newWindow = new Window(process, e.Title, rect.Top, rect.Left, rect.Right - rect.Left,
136 rect.Bottom - rect.Top);
137  
138 desktopWindowsListBox.Items.Add(newWindow);
1 office 139 }
140  
141 private void WindowRulesListBox_SelectedIndexChanged(object sender, EventArgs e)
142 {
143 var listBox = (ListBox) sender;
144  
145 if (listBox.SelectedIndex == -1)
146 {
11 office 147 WindowTitle.Text = string.Empty;
9 office 148 ignoreLeftCheckBox.Checked = false;
1 office 149 WindowLeft.Text = string.Empty;
9 office 150 ignoreTopCheckBox.Checked = false;
1 office 151 WindowTop.Text = string.Empty;
9 office 152 ignoreWidthCheckBox.Checked = false;
1 office 153 WindowWidth.Text = string.Empty;
9 office 154 ignoreHeightCheckBox.Checked = false;
1 office 155 WindowHeight.Text = string.Empty;
156 return;
157 }
158  
159 var window = (Window) listBox.SelectedItem;
160  
161 if (window == null)
162 {
163 return;
164 }
165  
11 office 166 WindowTitle.Text = window.Title;
9 office 167 ignoreLeftCheckBox.Checked = window.IgnoreLeft;
1 office 168 WindowLeft.Text = window.Left.ToString();
9 office 169 ignoreTopCheckBox.Checked = window.IgnoreTop;
1 office 170 WindowTop.Text = window.Top.ToString();
9 office 171 ignoreWidthCheckBox.Checked = window.IgnoreWidth;
1 office 172 WindowWidth.Text = window.Width.ToString();
9 office 173 ignoreHeightCheckBox.Checked = window.IgnoreHeight;
1 office 174 WindowHeight.Text = window.Height.ToString();
175 }
176  
177 private void RefreshButton_Click(object sender, EventArgs e)
178 {
11 office 179 Task.Run(RefreshWindows);
1 office 180 }
181  
182 private void RemoveButton_Click(object sender, EventArgs e)
183 {
184 var window = (Window) windowRulesListBox.SelectedItem;
185 if (window == null)
186 {
187 return;
188 }
189  
190 var index = -1;
191 for (var i = 0; i < windowRulesListBox.Items.Count; ++i)
192 {
11 office 193 if (((Window) windowRulesListBox.Items[i]).Title != window.Title)
1 office 194 {
11 office 195 continue;
1 office 196 }
11 office 197  
198 index = i;
199 break;
1 office 200 }
201  
202 if (index == -1)
203 {
204 return;
205 }
206  
207 windowRulesListBox.Items.RemoveAt(index);
208 Windows.Window.Remove(window);
209 }
210  
211 private void AddButton_Click(object sender, EventArgs e)
212 {
213 var window = (Window) desktopWindowsListBox.SelectedItem;
214 if (window == null)
215 {
216 return;
217 }
218  
219 var found = false;
220 foreach (var windowRule in windowRulesListBox.Items)
221 {
11 office 222 if (((Window) windowRule).Title != window.Title)
1 office 223 {
11 office 224 continue;
1 office 225 }
11 office 226  
227 found = true;
228 break;
1 office 229 }
230  
231 if (found)
232 {
233 return;
234 }
235  
236 windowRulesListBox.Items.Add(window);
237 Windows.Window.Add(window);
238 }
239  
240 private void OnWindowSettings_TextChanged(object sender, EventArgs e)
241 {
242 var selectedWindow = (Window) windowRulesListBox.SelectedItem;
243  
244 if (selectedWindow == null)
245 {
11 office 246 WindowTitle.Text = string.Empty;
1 office 247 WindowLeft.Text = string.Empty;
248 WindowTop.Text = string.Empty;
249 WindowWidth.Text = string.Empty;
250 WindowHeight.Text = string.Empty;
251  
252 return;
253 }
254  
255 var textBox = (TextBox) sender;
256 switch (textBox.Name)
257 {
11 office 258 case nameof(WindowTitle):
259 selectedWindow.Title = textBox.Text;
1 office 260 break;
261 case nameof(WindowLeft):
262 if (int.TryParse(textBox.Text, out var left))
263 {
264 selectedWindow.Left = left;
265 }
266  
267 break;
268 case nameof(WindowTop):
269 if (int.TryParse(textBox.Text, out var top))
270 {
271 selectedWindow.Top = top;
272 }
273  
274 break;
275 case nameof(WindowWidth):
276 if (int.TryParse(textBox.Text, out var width))
277 {
278 selectedWindow.Width = width;
279 }
280  
281 break;
282 case nameof(WindowHeight):
283 if (int.TryParse(textBox.Text, out var height))
284 {
285 selectedWindow.Height = height;
286 }
287  
288 break;
289 }
290 }
291  
9 office 292 private void OnIgnoreWindowSettings_CheckedChanged(object sender, EventArgs e)
293 {
294 var selectedWindow = (Window) windowRulesListBox.SelectedItem;
295  
296 if (selectedWindow == null)
297 {
298 ignoreLeftCheckBox.Checked = false;
299 ignoreTopCheckBox.Checked = false;
300 ignoreWidthCheckBox.Checked = false;
301 ignoreHeightCheckBox.Checked = false;
302  
303 return;
304 }
305  
306 var checkBox = (CheckBox) sender;
307 switch (checkBox.Name)
308 {
309 case nameof(ignoreHeightCheckBox):
310 selectedWindow.IgnoreHeight = checkBox.Checked;
311 break;
312 case nameof(ignoreWidthCheckBox):
313 selectedWindow.IgnoreWidth = checkBox.Checked;
314 break;
315 case nameof(ignoreTopCheckBox):
316 selectedWindow.IgnoreTop = checkBox.Checked;
317 break;
318 case nameof(ignoreLeftCheckBox):
319 selectedWindow.IgnoreLeft = checkBox.Checked;
320 break;
321 }
322 }
323  
14 office 324 private void DrawButton_Click(object sender, EventArgs e)
325 {
326 var selectedWindow = (Window) windowRulesListBox.SelectedItem;
327 if (selectedWindow == null)
328 {
329 return;
330 }
331  
332 if (DrawOverlayForm != null)
333 {
334 return;
335 }
336  
337 DrawOverlayForm = new DrawOverlayForm();
338 DrawOverlayForm.WindowDrawn += DrawOverlayForm_WindowDrawn;
339 DrawOverlayForm.Closed += DrawOverlayForm_Closed;
340 DrawOverlayForm.Show();
341 }
342  
343 private void DrawOverlayForm_WindowDrawn(object sender, WindowDrawnEventArgs e)
344 {
345 var selectedWindow = (Window) windowRulesListBox.SelectedItem;
346 if (selectedWindow == null)
347 {
348 return;
349 }
350  
351 WindowLeft.Text = e.Left.ToString();
352 WindowTop.Text = e.Top.ToString();
353 WindowWidth.Text = e.Width.ToString();
354 WindowHeight.Text = e.Height.ToString();
355  
356 selectedWindow.Left = e.Left;
357 selectedWindow.Top = e.Top;
358 selectedWindow.Width = e.Width;
359 selectedWindow.Height = e.Height;
360 }
361  
362 private void DrawOverlayForm_Closed(object sender, EventArgs e)
363 {
364 DrawOverlayForm.Closed -= DrawOverlayForm_Closed;
365 DrawOverlayForm.WindowDrawn -= DrawOverlayForm_WindowDrawn;
366 DrawOverlayForm.Dispose();
367 DrawOverlayForm = null;
368 }
369  
18 office 370 private void PickButton_Click(object sender, EventArgs e)
371 {
372 // Start global mouse key hook.
373 GlobalMouseHook = Hook.GlobalEvents();
374 GlobalMouseHook.MouseClick += GlobalMouseHook_MouseClick;
375  
376 // Set cross cursor.
377 foreach (var cursor in Enum.GetValues(typeof(Natives.OCR_SYSTEM_CURSORS)))
378 {
379 Natives.SetSystemCursor(
380 Natives.CopyIcon(Natives.LoadCursor(IntPtr.Zero, (int) Natives.OCR_SYSTEM_CURSORS.OCR_CROSS)),
381 (uint) cursor);
382 }
383 }
384  
385 private void GlobalMouseHook_MouseClick(object sender, MouseEventArgs e)
386 {
387 var hWnd = Natives.WindowFromPoint(new Point(e.X, e.Y));
388 Natives.GetWindowThreadProcessId(hWnd, out var pid);
24 office 389 Process process;
390 try
391 {
392 process = Process.GetProcessById((int) pid);
393 }
394 catch
395 {
396 return;
397 }
398  
18 office 399 var windowTitle = Helpers.GetWindowTitle(process.MainWindowHandle);
400 if (string.IsNullOrEmpty(windowTitle))
401 {
402 return;
403 }
404  
405 var windowClass = Helpers.GetWindowClass(process.MainWindowHandle);
406 if (string.IsNullOrEmpty(windowClass))
407 {
408 return;
409 }
410  
411 if (!Natives.GetWindowRect(process.MainWindowHandle, out var rect))
412 {
413 return;
414 }
415  
416 var window = new Window(windowClass, windowTitle, rect.Top, rect.Left, rect.Right - rect.Left,
417 rect.Bottom - rect.Top);
418  
419 var found = false;
420 foreach (var windowRule in windowRulesListBox.Items)
421 {
422 if (((Window) windowRule).Title != window.Title)
423 {
424 continue;
425 }
426  
427 found = true;
428 break;
429 }
430  
431 if (found)
432 {
433 return;
434 }
435  
436 windowRulesListBox.Items.Add(window);
437 Windows.Window.Add(window);
438  
439 // Revert cursor.
440 Natives.SystemParametersInfo(0x0057, 0, null, 0);
441  
442 // Remove global mouse key hook.
443 GlobalMouseHook.MouseClick -= GlobalMouseHook_MouseClick;
444 GlobalMouseHook.Dispose();
445 GlobalMouseHook = null;
446 }
447  
1 office 448 #endregion
449  
450 #region Private Methods
451  
452 private void RefreshWindows()
453 {
11 office 454 foreach (var handle in Helpers.FindWindows((wnd, param) => true))
1 office 455 {
14 office 456 var windowTitle = Helpers.GetWindowTitle(handle);
457 if (string.IsNullOrEmpty(windowTitle))
1 office 458 {
459 continue;
460 }
461  
14 office 462 var windowClass = Helpers.GetWindowClass(handle);
463 if (string.IsNullOrEmpty(windowClass))
1 office 464 {
11 office 465 continue;
466 }
1 office 467  
11 office 468 if (!Natives.GetWindowRect(handle, out var rect))
469 {
470 continue;
471 }
472  
14 office 473 var window = new Window(windowClass, windowTitle, rect.Top, rect.Left, rect.Right - rect.Left,
11 office 474 rect.Bottom - rect.Top);
475  
21 office 476 this.InvokeIfRequired(form =>
11 office 477 {
1 office 478 var found = false;
479 foreach (var item in desktopWindowsListBox.Items)
480 {
11 office 481 if (((Window) item).Title != window.Title)
1 office 482 {
11 office 483 continue;
1 office 484 }
11 office 485  
486 found = true;
487 break;
1 office 488 }
489  
490 if (!found)
491 {
492 desktopWindowsListBox.Items.Add(window);
493 }
11 office 494 });
1 office 495 }
496 }
497  
498 #endregion
499 }
500 }