Widow – Diff between revs 9 and 11
?pathlinks?
Rev 9 | Rev 11 | |||
---|---|---|---|---|
1 | using System; |
1 | using System; |
|
2 | using System.ComponentModel; |
2 | using System.ComponentModel; |
|
3 | using System.Configuration; |
3 | using System.Configuration; |
|
4 | using System.Globalization; |
4 | using System.Globalization; |
|
5 | using System.Threading.Tasks; |
5 | using System.Threading.Tasks; |
|
6 | using System.Windows.Forms; |
6 | using System.Windows.Forms; |
|
7 | using Widow.Properties; |
7 | using Widow.Properties; |
|
8 | using Widow.Serialization; |
8 | using Widow.Serialization; |
|
9 | |
9 | |
|
10 | namespace Widow |
10 | namespace Widow |
|
11 | { |
11 | { |
|
12 | public partial class MainForm : Form |
12 | public partial class MainForm : Form |
|
13 | { |
13 | { |
|
14 | #region Public Events & Delegates |
14 | #region Public Events & Delegates |
|
15 | |
15 | |
|
16 | public event EventHandler<WindowCreatedEventArgs> WindowCreated; |
16 | public event EventHandler<WindowCreatedEventArgs> WindowCreated; |
|
17 | |
17 | |
|
18 | public event EventHandler<WindowDestroyedEventArgs> WindowDestroyed; |
18 | public event EventHandler<WindowDestroyedEventArgs> WindowDestroyed; |
|
19 | |
19 | |
|
20 | #endregion |
20 | #endregion |
|
21 | |
21 | |
|
22 | #region Public Enums, Properties and Fields |
22 | #region Public Enums, Properties and Fields |
|
23 | |
23 | |
|
24 | public Windows.Windows Windows { get; set; } |
24 | public Windows.Windows Windows { get; set; } |
|
25 | |
25 | |
|
26 | public RuleEditForm RuleEditForm { get; set; } |
26 | public RuleEditForm RuleEditForm { get; set; } |
|
27 | |
27 | |
|
28 | public WindowManipulation WindowManipulation { get; set; } |
28 | public WindowManipulation WindowManipulation { get; set; } |
|
29 | |
29 | |
|
30 | public AboutForm AboutForm { get; set; } |
30 | public AboutForm AboutForm { get; set; } |
|
31 | |
31 | |
|
32 | #endregion |
32 | #endregion |
|
33 | |
33 | |
|
34 | #region Constructors, Destructors and Finalizers |
34 | #region Constructors, Destructors and Finalizers |
|
35 | |
35 | |
|
36 | public MainForm() |
36 | public MainForm() |
|
37 | { |
37 | { |
|
38 | InitializeComponent(); |
38 | InitializeComponent(); |
|
39 | |
39 | |
|
40 | // Bind to settings changed event. |
40 | // Bind to settings changed event. |
|
41 | Settings.Default.SettingsLoaded += Default_SettingsLoaded; |
41 | Settings.Default.SettingsLoaded += Default_SettingsLoaded; |
|
42 | Settings.Default.SettingsSaving += Default_SettingsSaving; |
42 | Settings.Default.SettingsSaving += Default_SettingsSaving; |
|
43 | Settings.Default.PropertyChanged += Default_PropertyChanged; |
43 | Settings.Default.PropertyChanged += Default_PropertyChanged; |
|
44 | |
44 | |
|
45 | Natives.RegisterShellHookWindow(Handle); |
45 | Natives.RegisterShellHookWindow(Handle); |
|
46 | |
46 | |
|
47 | LoadWindows().ContinueWith(task => |
47 | LoadWindows().ContinueWith(task => |
|
48 | { |
48 | { |
|
49 | WindowManipulation = new WindowManipulation(this, Windows, Settings.Default); |
49 | WindowManipulation = new WindowManipulation(this, Windows, Settings.Default); |
|
50 | }); |
50 | }); |
|
51 | } |
51 | } |
|
52 | |
52 | |
|
53 | /// <summary> |
53 | /// <summary> |
|
54 | /// Clean up any resources being used. |
54 | /// Clean up any resources being used. |
|
55 | /// </summary> |
55 | /// </summary> |
|
56 | /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> |
56 | /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> |
|
57 | protected override void Dispose(bool disposing) |
57 | protected override void Dispose(bool disposing) |
|
58 | { |
58 | { |
|
59 | if (disposing && components != null) |
59 | if (disposing && components != null) |
|
60 | { |
60 | { |
|
61 | Settings.Default.SettingsLoaded -= Default_SettingsLoaded; |
61 | Settings.Default.SettingsLoaded -= Default_SettingsLoaded; |
|
62 | Settings.Default.SettingsSaving -= Default_SettingsSaving; |
62 | Settings.Default.SettingsSaving -= Default_SettingsSaving; |
|
63 | Settings.Default.PropertyChanged -= Default_PropertyChanged; |
63 | Settings.Default.PropertyChanged -= Default_PropertyChanged; |
|
64 | |
64 | |
|
65 | components.Dispose(); |
65 | components.Dispose(); |
|
66 | } |
66 | } |
|
67 | |
67 | |
|
68 | base.Dispose(disposing); |
68 | base.Dispose(disposing); |
|
69 | } |
69 | } |
|
70 | |
70 | |
|
71 | #endregion |
71 | #endregion |
|
72 | |
72 | |
|
73 | #region Private Overrides |
73 | #region Private Overrides |
|
74 | |
74 | |
|
75 | protected override void WndProc(ref Message m) |
75 | protected override void WndProc(ref Message m) |
|
76 | { |
76 | { |
|
77 | var handle = m.LParam; |
77 | var handle = m.LParam; |
|
78 | var windowName = Helpers.GetWindowTitle(handle); |
78 | var windowName = Helpers.GetWindowTitle(handle); |
|
79 | |
79 | |
|
80 | switch (m.WParam.ToInt32()) |
80 | switch (m.WParam.ToInt32()) |
|
81 | { |
81 | { |
|
82 | case (int) Natives.WM.HSHELL_WINDOWCREATED: |
82 | case (int) Natives.WM.HSHELL_WINDOWCREATED: |
|
83 | WindowCreated?.Invoke(this, new WindowCreatedEventArgs(windowName, handle)); |
83 | WindowCreated?.Invoke(this, new WindowCreatedEventArgs(windowName, handle)); |
|
84 | break; |
84 | break; |
|
85 | case (int) Natives.WM.HSHELL_WINDOWDESTROYED: |
85 | case (int) Natives.WM.HSHELL_WINDOWDESTROYED: |
|
86 | WindowDestroyed?.Invoke(this, new WindowDestroyedEventArgs(windowName, handle)); |
86 | WindowDestroyed?.Invoke(this, new WindowDestroyedEventArgs(windowName, handle)); |
|
87 | break; |
87 | break; |
|
88 | } |
88 | } |
|
89 | |
89 | |
|
90 | base.WndProc(ref m); |
90 | base.WndProc(ref m); |
|
91 | } |
91 | } |
|
92 | |
92 | |
|
93 | #endregion |
93 | #endregion |
|
94 | |
94 | |
|
95 | #region Event Handlers |
95 | #region Event Handlers |
|
96 | |
96 | |
|
97 | private static void Default_PropertyChanged(object sender, PropertyChangedEventArgs e) |
97 | private static void Default_PropertyChanged(object sender, PropertyChangedEventArgs e) |
|
98 | { |
98 | { |
|
99 | Settings.Default.Save(); |
99 | Settings.Default.Save(); |
|
100 | } |
100 | } |
|
101 | |
101 | |
|
102 | private static void Default_SettingsSaving(object sender, CancelEventArgs e) |
102 | private static void Default_SettingsSaving(object sender, CancelEventArgs e) |
|
103 | { |
103 | { |
|
104 | } |
104 | } |
|
105 | |
105 | |
|
106 | private static void Default_SettingsLoaded(object sender, SettingsLoadedEventArgs e) |
106 | private static void Default_SettingsLoaded(object sender, SettingsLoadedEventArgs e) |
|
107 | { |
107 | { |
|
108 | } |
108 | } |
|
109 | |
109 | |
|
110 | private void NewToolStripMenuItem_Click(object sender, EventArgs e) |
110 | private void NewToolStripMenuItem_Click(object sender, EventArgs e) |
|
111 | { |
111 | { |
|
- | 112 | if (RuleEditForm != null) |
||
- | 113 | { |
||
- | 114 | return; |
||
- | 115 | } |
||
- | 116 | |
||
112 | RuleEditForm = new RuleEditForm(this, Windows); |
117 | RuleEditForm = new RuleEditForm(this, Windows); |
|
113 | RuleEditForm.Closed += RuleEditForm_Closed; |
118 | RuleEditForm.Closed += RuleEditForm_Closed; |
|
114 | RuleEditForm.Show(); |
119 | RuleEditForm.Show(); |
|
115 | } |
120 | } |
|
116 | |
121 | |
|
117 | private async void RuleEditForm_Closed(object sender, EventArgs e) |
122 | private async void RuleEditForm_Closed(object sender, EventArgs e) |
|
118 | { |
123 | { |
|
119 | RuleEditForm.Closed -= RuleEditForm_Closed; |
124 | RuleEditForm.Closed -= RuleEditForm_Closed; |
|
120 | RuleEditForm.Dispose(); |
125 | RuleEditForm.Dispose(); |
|
121 | RuleEditForm = null; |
126 | RuleEditForm = null; |
|
122 | |
127 | |
|
123 | switch (await WindowsSerialization.Serialize(Windows, "Windows.xml")) |
128 | switch (await WindowsSerialization.Serialize(Windows, "Windows.xml")) |
|
124 | { |
129 | { |
|
125 | case SerializationSuccess serializationSuccess: |
130 | case SerializationSuccess serializationSuccess: |
|
126 | break; |
131 | break; |
|
127 | case SerializationFailure serializationFailure: |
132 | case SerializationFailure serializationFailure: |
|
128 | break; |
133 | break; |
|
129 | } |
134 | } |
|
130 | } |
135 | } |
|
131 | |
136 | |
|
132 | private void ExitToolStripMenuItem_Click(object sender, EventArgs e) |
137 | private void ExitToolStripMenuItem_Click(object sender, EventArgs e) |
|
133 | { |
138 | { |
|
134 | Application.Exit(); |
139 | Application.Exit(); |
|
135 | } |
140 | } |
|
136 | |
141 | |
|
137 | private void LaunchOnBootToolStripMenuItem_CheckedChanged(object sender, EventArgs e) |
142 | private void LaunchOnBootToolStripMenuItem_CheckedChanged(object sender, EventArgs e) |
|
138 | { |
143 | { |
|
139 | Settings.Default.LaunchOnBoot = ((ToolStripMenuItem) sender).Checked; |
144 | Settings.Default.LaunchOnBoot = ((ToolStripMenuItem) sender).Checked; |
|
140 | |
145 | |
|
141 | LaunchOnBoot.Set(Settings.Default.LaunchOnBoot); |
146 | LaunchOnBoot.Set(Settings.Default.LaunchOnBoot); |
|
142 | } |
147 | } |
|
143 | |
148 | |
|
144 | private void OnWindowCreateToolStripMenuItem_CheckedChanged(object sender, EventArgs e) |
149 | private void OnWindowCreateToolStripMenuItem_CheckedChanged(object sender, EventArgs e) |
|
145 | { |
150 | { |
|
146 | Settings.Default.OnWindowCreate = ((ToolStripMenuItem) sender).Checked; |
151 | Settings.Default.OnWindowCreate = ((ToolStripMenuItem) sender).Checked; |
|
147 | |
152 | |
|
148 | WindowManipulation.OnWindowCreate = Settings.Default.OnWindowCreate; |
153 | WindowManipulation.OnWindowCreate = Settings.Default.OnWindowCreate; |
|
149 | } |
154 | } |
|
150 | |
155 | |
|
151 | private void AboutToolStripMenuItem_Click(object sender, EventArgs e) |
156 | private void AboutToolStripMenuItem_Click(object sender, EventArgs e) |
|
152 | { |
157 | { |
|
153 | // Show the about form. |
158 | // Show the about form. |
|
154 | AboutForm = new AboutForm(); |
159 | AboutForm = new AboutForm(); |
|
155 | AboutForm.Show(); |
160 | AboutForm.Show(); |
|
156 | } |
161 | } |
|
157 | |
162 | |
|
158 | private void ApplyNowToolStripMenuItem_Click(object sender, EventArgs e) |
163 | private async void ApplyNowToolStripMenuItem_Click(object sender, EventArgs e) |
|
159 | { |
164 | { |
|
160 | WindowManipulation.Apply(); |
165 | await WindowManipulation.Apply(); |
|
161 | } |
166 | } |
|
162 | |
167 | |
|
163 | private void OnEveryToolStripMenuItem_CheckedChanged(object sender, EventArgs e) |
168 | private void OnEveryToolStripMenuItem_CheckedChanged(object sender, EventArgs e) |
|
164 | { |
169 | { |
|
165 | var toolStripMenuItem = (ToolStripMenuItem) sender; |
170 | var toolStripMenuItem = (ToolStripMenuItem) sender; |
|
166 | |
171 | |
|
167 | if (!int.TryParse(toolStripTextBox1.Text, out var time)) |
172 | if (!int.TryParse(toolStripTextBox1.Text, out var time)) |
|
168 | { |
173 | { |
|
169 | return; |
174 | return; |
|
170 | } |
175 | } |
|
171 | |
176 | |
|
172 | Settings.Default.ApplyEveryTimeEnabled = toolStripMenuItem.Checked; |
177 | Settings.Default.ApplyEveryTimeEnabled = toolStripMenuItem.Checked; |
|
173 | Settings.Default.ApplyEveryTime = time.ToString(CultureInfo.InvariantCulture); |
178 | Settings.Default.ApplyEveryTime = time.ToString(CultureInfo.InvariantCulture); |
|
174 | |
179 | |
|
175 | WindowManipulation.ApplyEveryTime = time; |
180 | WindowManipulation.ApplyEveryTime = time; |
|
176 | WindowManipulation.ApplyEveryTimeEnabled = toolStripMenuItem.Checked; |
181 | WindowManipulation.ApplyEveryTimeEnabled = toolStripMenuItem.Checked; |
|
177 | } |
182 | } |
|
178 | |
183 | |
|
179 | #endregion |
184 | #endregion |
|
180 | |
185 | |
|
181 | #region Private Methods |
186 | #region Private Methods |
|
182 | |
187 | |
|
183 | private async Task LoadWindows() |
188 | private async Task LoadWindows() |
|
184 | { |
189 | { |
|
185 | switch (await WindowsSerialization.Deserialize("Windows.xml")) |
190 | switch (await WindowsSerialization.Deserialize("Windows.xml")) |
|
186 | { |
191 | { |
|
187 | case SerializationSuccess serializationSuccess: |
192 | case SerializationSuccess serializationSuccess: |
|
188 | Windows = serializationSuccess.Windows; |
193 | Windows = serializationSuccess.Windows; |
|
189 | break; |
194 | break; |
|
190 | case SerializationFailure serializationFailure: |
195 | case SerializationFailure serializationFailure: |
|
191 | Windows = new Windows.Windows(); |
196 | Windows = new Windows.Windows(); |
|
192 | break; |
197 | break; |
|
193 | } |
198 | } |
|
194 | } |
199 | } |
|
195 | |
200 | |
|
196 | #endregion |
201 | #endregion |
|
- | 202 | |
||
- | 203 | private void NotifyIcon1_DoubleClick(object sender, EventArgs e) |
||
- | 204 | { |
||
- | 205 | if (RuleEditForm != null) |
||
- | 206 | { |
||
- | 207 | return; |
||
- | 208 | } |
||
- | 209 | |
||
- | 210 | RuleEditForm = new RuleEditForm(this, Windows); |
||
- | 211 | RuleEditForm.Closed += RuleEditForm_Closed; |
||
- | 212 | RuleEditForm.Show(); |
||
- | 213 | } |
||
- | 214 | |
||
- | 215 | private async void NotifyIcon1_Click(object sender, EventArgs e) |
||
- | 216 | { |
||
- | 217 | await WindowManipulation.Apply(); |
||
- | 218 | } |
||
197 | } |
219 | } |
|
198 | } |
220 | } |
|
199 | |
221 | |
|
200 |
|
222 |
|
|
201 | |
223 | |
|
202 | |
224 | |
|
203 | |
225 | |