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