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