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