Widow – Diff between revs 1 and 6
?pathlinks?
Rev 1 | Rev 6 | |||
---|---|---|---|---|
1 | using System; |
1 | using System; |
|
2 | using System.Collections.Generic; |
2 | using System.Collections.Generic; |
|
3 | using System.Windows.Forms; |
3 | using System.Windows.Forms; |
|
4 | using Windows; |
4 | using Windows; |
|
5 | |
5 | |
|
6 | namespace Widow |
6 | namespace Widow |
|
7 | { |
7 | { |
|
8 | public partial class RuleEditForm : Form |
8 | public partial class RuleEditForm : Form |
|
9 | { |
9 | { |
|
10 | #region Public Enums, Properties and Fields |
10 | #region Public Enums, Properties and Fields |
|
11 | |
11 | |
|
12 | public Windows.Windows Windows { get; } |
12 | public Windows.Windows Windows { get; } |
|
13 | |
13 | |
|
14 | public Form1 Form { get; set; } |
14 | public MainForm Form { get; set; } |
|
15 | |
15 | |
|
16 | #endregion |
16 | #endregion |
|
17 | |
17 | |
|
18 | #region Constructors, Destructors and Finalizers |
18 | #region Constructors, Destructors and Finalizers |
|
19 | |
19 | |
|
20 | public RuleEditForm(Form1 form1, Windows.Windows windows) : this(windows) |
20 | public RuleEditForm(MainForm mainForm, Windows.Windows windows) : this(windows) |
|
21 | { |
21 | { |
|
22 | Form = form1; |
22 | Form = mainForm; |
|
23 | |
23 | |
|
24 | Form.WindowCreated += Form_WindowCreated; |
24 | Form.WindowCreated += Form_WindowCreated; |
|
25 | Form.WindowDestroyed += Form_WindowDestroyed; |
25 | Form.WindowDestroyed += Form_WindowDestroyed; |
|
26 | } |
26 | } |
|
27 | |
27 | |
|
28 | public RuleEditForm(Windows.Windows windows) : this() |
28 | public RuleEditForm(Windows.Windows windows) : this() |
|
29 | { |
29 | { |
|
30 | Windows = windows; |
30 | Windows = windows; |
|
31 | |
31 | |
|
32 | foreach (var window in windows.Window) |
32 | foreach (var window in windows.Window) |
|
33 | { |
33 | { |
|
34 | windowRulesListBox.Items.Add(window); |
34 | windowRulesListBox.Items.Add(window); |
|
35 | } |
35 | } |
|
36 | } |
36 | } |
|
37 | |
37 | |
|
38 | private RuleEditForm() |
38 | private RuleEditForm() |
|
39 | { |
39 | { |
|
40 | InitializeComponent(); |
40 | InitializeComponent(); |
|
41 | |
41 | |
|
42 | desktopWindowsListBox.DisplayMember = "Name"; |
42 | desktopWindowsListBox.DisplayMember = "Name"; |
|
43 | windowRulesListBox.DisplayMember = "Name"; |
43 | windowRulesListBox.DisplayMember = "Name"; |
|
44 | |
44 | |
|
45 | RefreshWindows(); |
45 | RefreshWindows(); |
|
46 | } |
46 | } |
|
47 | |
47 | |
|
48 | /// <summary> |
48 | /// <summary> |
|
49 | /// Clean up any resources being used. |
49 | /// Clean up any resources being used. |
|
50 | /// </summary> |
50 | /// </summary> |
|
51 | /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> |
51 | /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> |
|
52 | protected override void Dispose(bool disposing) |
52 | protected override void Dispose(bool disposing) |
|
53 | { |
53 | { |
|
54 | if (disposing && components != null) |
54 | if (disposing && components != null) |
|
55 | { |
55 | { |
|
56 | Form.WindowCreated -= Form_WindowCreated; |
56 | Form.WindowCreated -= Form_WindowCreated; |
|
57 | Form.WindowDestroyed -= Form_WindowDestroyed; |
57 | Form.WindowDestroyed -= Form_WindowDestroyed; |
|
58 | |
58 | |
|
59 | components.Dispose(); |
59 | components.Dispose(); |
|
60 | } |
60 | } |
|
61 | |
61 | |
|
62 | base.Dispose(disposing); |
62 | base.Dispose(disposing); |
|
63 | } |
63 | } |
|
64 | |
64 | |
|
65 | #endregion |
65 | #endregion |
|
66 | |
66 | |
|
67 | #region Event Handlers |
67 | #region Event Handlers |
|
68 | |
68 | |
|
69 | private void Form_WindowDestroyed(object sender, WindowDestroyedEventArgs e) |
69 | private void Form_WindowDestroyed(object sender, WindowDestroyedEventArgs e) |
|
70 | { |
70 | { |
|
71 | if (string.IsNullOrEmpty(e.Name)) |
71 | if (string.IsNullOrEmpty(e.Name)) |
|
72 | { |
72 | { |
|
73 | return; |
73 | return; |
|
74 | } |
74 | } |
|
75 | |
75 | |
|
76 | var indices = new List<int>(); |
76 | var indices = new List<int>(); |
|
77 | for (var i = 0; i < desktopWindowsListBox.Items.Count; ++i) |
77 | for (var i = 0; i < desktopWindowsListBox.Items.Count; ++i) |
|
78 | { |
78 | { |
|
79 | if (((Window) desktopWindowsListBox.Items[i]).Name == e.Name) |
79 | if (((Window) desktopWindowsListBox.Items[i]).Name == e.Name) |
|
80 | { |
80 | { |
|
81 | indices.Add(i); |
81 | indices.Add(i); |
|
82 | } |
82 | } |
|
83 | } |
83 | } |
|
84 | |
84 | |
|
85 | foreach (var index in indices) |
85 | foreach (var index in indices) |
|
86 | { |
86 | { |
|
87 | desktopWindowsListBox.Items.RemoveAt(index); |
87 | desktopWindowsListBox.Items.RemoveAt(index); |
|
88 | } |
88 | } |
|
89 | } |
89 | } |
|
90 | |
90 | |
|
91 | private void Form_WindowCreated(object sender, WindowCreatedEventArgs e) |
91 | private void Form_WindowCreated(object sender, WindowCreatedEventArgs e) |
|
92 | { |
92 | { |
|
93 | if (string.IsNullOrEmpty(e.Name)) |
93 | if (string.IsNullOrEmpty(e.Name)) |
|
94 | { |
94 | { |
|
95 | return; |
95 | return; |
|
96 | } |
96 | } |
|
97 | |
97 | |
|
98 | var found = false; |
98 | var found = false; |
|
99 | foreach (var window in desktopWindowsListBox.Items) |
99 | foreach (var window in desktopWindowsListBox.Items) |
|
100 | { |
100 | { |
|
101 | if (((Window) window).Name == e.Name) |
101 | if (((Window) window).Name == e.Name) |
|
102 | { |
102 | { |
|
103 | found = true; |
103 | found = true; |
|
104 | break; |
104 | break; |
|
105 | } |
105 | } |
|
106 | } |
106 | } |
|
107 | |
107 | |
|
108 | if (found) |
108 | if (found) |
|
109 | { |
109 | { |
|
110 | return; |
110 | return; |
|
111 | } |
111 | } |
|
112 | |
112 | |
|
113 | if (Natives.GetWindowRect(e.Handle, out var rect)) |
113 | if (Natives.GetWindowRect(e.Handle, out var rect)) |
|
114 | { |
114 | { |
|
115 | var window = new Window(e.Name, rect.Top, rect.Left, rect.Right - rect.Left, |
115 | var window = new Window(e.Name, rect.Top, rect.Left, rect.Right - rect.Left, |
|
116 | rect.Bottom - rect.Top); |
116 | rect.Bottom - rect.Top); |
|
117 | |
117 | |
|
118 | desktopWindowsListBox.Items.Add(window); |
118 | desktopWindowsListBox.Items.Add(window); |
|
119 | } |
119 | } |
|
120 | } |
120 | } |
|
121 | |
121 | |
|
122 | private void WindowRulesListBox_SelectedIndexChanged(object sender, EventArgs e) |
122 | private void WindowRulesListBox_SelectedIndexChanged(object sender, EventArgs e) |
|
123 | { |
123 | { |
|
124 | var listBox = (ListBox) sender; |
124 | var listBox = (ListBox) sender; |
|
125 | |
125 | |
|
126 | if (listBox.SelectedIndex == -1) |
126 | if (listBox.SelectedIndex == -1) |
|
127 | { |
127 | { |
|
128 | WindowName.Text = string.Empty; |
128 | WindowName.Text = string.Empty; |
|
129 | WindowLeft.Text = string.Empty; |
129 | WindowLeft.Text = string.Empty; |
|
130 | WindowTop.Text = string.Empty; |
130 | WindowTop.Text = string.Empty; |
|
131 | WindowWidth.Text = string.Empty; |
131 | WindowWidth.Text = string.Empty; |
|
132 | WindowHeight.Text = string.Empty; |
132 | WindowHeight.Text = string.Empty; |
|
133 | return; |
133 | return; |
|
134 | } |
134 | } |
|
135 | |
135 | |
|
136 | var window = (Window) listBox.SelectedItem; |
136 | var window = (Window) listBox.SelectedItem; |
|
137 | |
137 | |
|
138 | if (window == null) |
138 | if (window == null) |
|
139 | { |
139 | { |
|
140 | return; |
140 | return; |
|
141 | } |
141 | } |
|
142 | |
142 | |
|
143 | WindowName.Text = window.Name; |
143 | WindowName.Text = window.Name; |
|
144 | WindowLeft.Text = window.Left.ToString(); |
144 | WindowLeft.Text = window.Left.ToString(); |
|
145 | WindowTop.Text = window.Top.ToString(); |
145 | WindowTop.Text = window.Top.ToString(); |
|
146 | WindowWidth.Text = window.Width.ToString(); |
146 | WindowWidth.Text = window.Width.ToString(); |
|
147 | WindowHeight.Text = window.Height.ToString(); |
147 | WindowHeight.Text = window.Height.ToString(); |
|
148 | } |
148 | } |
|
149 | |
149 | |
|
150 | private void RefreshButton_Click(object sender, EventArgs e) |
150 | private void RefreshButton_Click(object sender, EventArgs e) |
|
151 | { |
151 | { |
|
152 | RefreshWindows(); |
152 | RefreshWindows(); |
|
153 | } |
153 | } |
|
154 | |
154 | |
|
155 | private void RemoveButton_Click(object sender, EventArgs e) |
155 | private void RemoveButton_Click(object sender, EventArgs e) |
|
156 | { |
156 | { |
|
157 | var window = (Window) windowRulesListBox.SelectedItem; |
157 | var window = (Window) windowRulesListBox.SelectedItem; |
|
158 | if (window == null) |
158 | if (window == null) |
|
159 | { |
159 | { |
|
160 | return; |
160 | return; |
|
161 | } |
161 | } |
|
162 | |
162 | |
|
163 | var index = -1; |
163 | var index = -1; |
|
164 | for (var i = 0; i < windowRulesListBox.Items.Count; ++i) |
164 | for (var i = 0; i < windowRulesListBox.Items.Count; ++i) |
|
165 | { |
165 | { |
|
166 | if (((Window) windowRulesListBox.Items[i]).Name == window.Name) |
166 | if (((Window) windowRulesListBox.Items[i]).Name == window.Name) |
|
167 | { |
167 | { |
|
168 | index = i; |
168 | index = i; |
|
169 | break; |
169 | break; |
|
170 | } |
170 | } |
|
171 | } |
171 | } |
|
172 | |
172 | |
|
173 | if (index == -1) |
173 | if (index == -1) |
|
174 | { |
174 | { |
|
175 | return; |
175 | return; |
|
176 | } |
176 | } |
|
177 | |
177 | |
|
178 | windowRulesListBox.Items.RemoveAt(index); |
178 | windowRulesListBox.Items.RemoveAt(index); |
|
179 | Windows.Window.Remove(window); |
179 | Windows.Window.Remove(window); |
|
180 | } |
180 | } |
|
181 | |
181 | |
|
182 | private void AddButton_Click(object sender, EventArgs e) |
182 | private void AddButton_Click(object sender, EventArgs e) |
|
183 | { |
183 | { |
|
184 | var window = (Window) desktopWindowsListBox.SelectedItem; |
184 | var window = (Window) desktopWindowsListBox.SelectedItem; |
|
185 | if (window == null) |
185 | if (window == null) |
|
186 | { |
186 | { |
|
187 | return; |
187 | return; |
|
188 | } |
188 | } |
|
189 | |
189 | |
|
190 | var found = false; |
190 | var found = false; |
|
191 | foreach (var windowRule in windowRulesListBox.Items) |
191 | foreach (var windowRule in windowRulesListBox.Items) |
|
192 | { |
192 | { |
|
193 | if (((Window) windowRule).Name == window.Name) |
193 | if (((Window) windowRule).Name == window.Name) |
|
194 | { |
194 | { |
|
195 | found = true; |
195 | found = true; |
|
196 | break; |
196 | break; |
|
197 | } |
197 | } |
|
198 | } |
198 | } |
|
199 | |
199 | |
|
200 | if (found) |
200 | if (found) |
|
201 | { |
201 | { |
|
202 | return; |
202 | return; |
|
203 | } |
203 | } |
|
204 | |
204 | |
|
205 | windowRulesListBox.Items.Add(window); |
205 | windowRulesListBox.Items.Add(window); |
|
206 | Windows.Window.Add(window); |
206 | Windows.Window.Add(window); |
|
207 | } |
207 | } |
|
208 | |
208 | |
|
209 | private void OnWindowSettings_TextChanged(object sender, EventArgs e) |
209 | private void OnWindowSettings_TextChanged(object sender, EventArgs e) |
|
210 | { |
210 | { |
|
211 | var selectedWindow = (Window) windowRulesListBox.SelectedItem; |
211 | var selectedWindow = (Window) windowRulesListBox.SelectedItem; |
|
212 | |
212 | |
|
213 | if (selectedWindow == null) |
213 | if (selectedWindow == null) |
|
214 | { |
214 | { |
|
215 | WindowName.Text = string.Empty; |
215 | WindowName.Text = string.Empty; |
|
216 | WindowLeft.Text = string.Empty; |
216 | WindowLeft.Text = string.Empty; |
|
217 | WindowTop.Text = string.Empty; |
217 | WindowTop.Text = string.Empty; |
|
218 | WindowWidth.Text = string.Empty; |
218 | WindowWidth.Text = string.Empty; |
|
219 | WindowHeight.Text = string.Empty; |
219 | WindowHeight.Text = string.Empty; |
|
220 | |
220 | |
|
221 | return; |
221 | return; |
|
222 | } |
222 | } |
|
223 | |
223 | |
|
224 | var textBox = (TextBox) sender; |
224 | var textBox = (TextBox) sender; |
|
225 | switch (textBox.Name) |
225 | switch (textBox.Name) |
|
226 | { |
226 | { |
|
227 | case nameof(WindowName): |
227 | case nameof(WindowName): |
|
228 | selectedWindow.Name = textBox.Text; |
228 | selectedWindow.Name = textBox.Text; |
|
229 | break; |
229 | break; |
|
230 | case nameof(WindowLeft): |
230 | case nameof(WindowLeft): |
|
231 | if (int.TryParse(textBox.Text, out var left)) |
231 | if (int.TryParse(textBox.Text, out var left)) |
|
232 | { |
232 | { |
|
233 | selectedWindow.Left = left; |
233 | selectedWindow.Left = left; |
|
234 | } |
234 | } |
|
235 | |
235 | |
|
236 | break; |
236 | break; |
|
237 | case nameof(WindowTop): |
237 | case nameof(WindowTop): |
|
238 | if (int.TryParse(textBox.Text, out var top)) |
238 | if (int.TryParse(textBox.Text, out var top)) |
|
239 | { |
239 | { |
|
240 | selectedWindow.Top = top; |
240 | selectedWindow.Top = top; |
|
241 | } |
241 | } |
|
242 | |
242 | |
|
243 | break; |
243 | break; |
|
244 | case nameof(WindowWidth): |
244 | case nameof(WindowWidth): |
|
245 | if (int.TryParse(textBox.Text, out var width)) |
245 | if (int.TryParse(textBox.Text, out var width)) |
|
246 | { |
246 | { |
|
247 | selectedWindow.Width = width; |
247 | selectedWindow.Width = width; |
|
248 | } |
248 | } |
|
249 | |
249 | |
|
250 | break; |
250 | break; |
|
251 | case nameof(WindowHeight): |
251 | case nameof(WindowHeight): |
|
252 | if (int.TryParse(textBox.Text, out var height)) |
252 | if (int.TryParse(textBox.Text, out var height)) |
|
253 | { |
253 | { |
|
254 | selectedWindow.Height = height; |
254 | selectedWindow.Height = height; |
|
255 | } |
255 | } |
|
256 | |
256 | |
|
257 | break; |
257 | break; |
|
258 | } |
258 | } |
|
259 | } |
259 | } |
|
260 | |
260 | |
|
261 | #endregion |
261 | #endregion |
|
262 | |
262 | |
|
263 | #region Private Methods |
263 | #region Private Methods |
|
264 | |
264 | |
|
265 | private void RefreshWindows() |
265 | private void RefreshWindows() |
|
266 | { |
266 | { |
|
267 | foreach (var desktopWindow in Helpers.GetDesktopWindows()) |
267 | foreach (var desktopWindow in Helpers.GetDesktopWindows()) |
|
268 | { |
268 | { |
|
269 | if (string.IsNullOrEmpty(desktopWindow.Title)) |
269 | if (string.IsNullOrEmpty(desktopWindow.Title)) |
|
270 | { |
270 | { |
|
271 | continue; |
271 | continue; |
|
272 | } |
272 | } |
|
273 | |
273 | |
|
274 | if (Natives.GetWindowRect(desktopWindow.Handle, out var rect)) |
274 | if (Natives.GetWindowRect(desktopWindow.Handle, out var rect)) |
|
275 | { |
275 | { |
|
276 | var window = new Window(desktopWindow.Title, rect.Top, rect.Left, rect.Right - rect.Left, |
276 | var window = new Window(desktopWindow.Title, rect.Top, rect.Left, rect.Right - rect.Left, |
|
277 | rect.Bottom - rect.Top); |
277 | rect.Bottom - rect.Top); |
|
278 | |
278 | |
|
279 | var found = false; |
279 | var found = false; |
|
280 | foreach (var item in desktopWindowsListBox.Items) |
280 | foreach (var item in desktopWindowsListBox.Items) |
|
281 | { |
281 | { |
|
282 | if (((Window) item).Name == window.Name) |
282 | if (((Window) item).Name == window.Name) |
|
283 | { |
283 | { |
|
284 | found = true; |
284 | found = true; |
|
285 | break; |
285 | break; |
|
286 | } |
286 | } |
|
287 | } |
287 | } |
|
288 | |
288 | |
|
289 | if (!found) |
289 | if (!found) |
|
290 | { |
290 | { |
|
291 | desktopWindowsListBox.Items.Add(window); |
291 | desktopWindowsListBox.Items.Add(window); |
|
292 | } |
292 | } |
|
293 | } |
293 | } |
|
294 | } |
294 | } |
|
295 | } |
295 | } |
|
296 | |
296 | |
|
297 | #endregion |
297 | #endregion |
|
298 | } |
298 | } |
|
299 | } |
299 | } |
|
300 | |
300 | |
|
301 |
|
301 |
|
|
302 | |
302 | |
|
303 | |
303 | |
|
304 | |
304 | |