forked from tomasrudh/WinSize4
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClsSavedWindows.cs
More file actions
385 lines (358 loc) · 15.5 KB
/
ClsSavedWindows.cs
File metadata and controls
385 lines (358 loc) · 15.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.Json;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using WinSize4;
using static System.Net.Mime.MediaTypeNames;
public class ClsSavedWindows
{
public List<ClsWindowProps> Props = new List<ClsWindowProps>();
private int _nextTag = 0;
//**********************************************
/// <summary> Adds a new window </summary>
/// <param name="Props"></param>
/// <returns>Tag</returns>
//**********************************************
public int AddWindow(ClsWindowProps Props)
{
this.Props.Add(Props);
this.Props[this.Props.Count - 1].Tag = _nextTag++;
if (this.Props[this.Props.Count - 1].Exe == "explorer")
this.Props[this.Props.Count - 1].IgnoreChildWindows = false;
return _nextTag;
}
//**********************************************
/// <summary> Duplicates a window </summary>
/// <param name="Props"></param>
/// <returns>Tag</returns>
//**********************************************
public void DuplicateWindow(int Index)
{
this.Props.Add(this.Props[Index]);
this.Props[this.Props.Count - 1].Tag = _nextTag++;
}
//**********************************************
/// <summary>Updates properties for the supplied window properties</summary>
/// <param name="ClsSavedWindowProps"></param>
/// <returns>True if a window is updated, false if window is not found</returns>
//**********************************************
public bool UpdateWindowProperties(ClsWindowProps Props, List<ClsScreenList> CurrentScreenList, int screenCurrentIndex)
{
bool Found = false;
int Index = GetIndexCurrentScreen(Props, CurrentScreenList, screenCurrentIndex);
if (Index > -1)
{
Found = true;
this.Props[Index].Left = Props.Left;
this.Props[Index].Top = Props.Top;
this.Props[Index].Width = Props.Width;
this.Props[Index].Height = Props.Height;
//this.Props[Index].MaxWidth = false;
//this.Props[Index].MaxHeight = false;
//this.Props[Index].FullScreen = false;
}
return Found;
}
//**********************************************
/// <summary> Finds the window with the supplied data in the current screen, or -1 </summary>
/// <param name="ClsWindowProps, ClsScreenList"></param>
/// <returns>Index</returns>
//**********************************************
//public int GetIndexCurrentScreen(ClsWindowProps CurrentWindowProps)
public int GetIndexCurrentScreen(ClsWindowProps CurrentWindowProps, List<ClsScreenList> CurrentScreenList, int screenCurrentIndex)
{
int result = -1;
// Get list of matching saved windows
List<ClsWindowProps> matchingSavedWindows = new List<ClsWindowProps>();
for (int i = 0; i < this.Props.Count; i++)
{
bool FoundTitle = true;
if (this.Props[i].SearchTitleInclude)
{
switch (this.Props[i].SearchTypeInclude)
{
case ClsWindowProps.Full:
FoundTitle = (CurrentWindowProps.TitleInclude == this.Props[i].TitleInclude);
break;
case ClsWindowProps.Contains:
FoundTitle = CurrentWindowProps.TitleInclude.Contains(this.Props[i].TitleInclude);
break;
case ClsWindowProps.StartsWith:
FoundTitle = CurrentWindowProps.TitleInclude.StartsWith(this.Props[i].TitleInclude);
break;
}
}
bool FoundExe = true;
if (this.Props[i].SearchExe)
FoundExe = (CurrentWindowProps.Exe == this.Props[i].Exe);
bool FoundWindowClass = true;
if (this.Props[i].ConsiderWindowClass)
FoundWindowClass = (CurrentWindowProps.WindowClass == this.Props[i].WindowClass);
if (FoundTitle && FoundExe && FoundWindowClass)
{
matchingSavedWindows.Add(this.Props[i]);
}
}
for (int i = 0; i < matchingSavedWindows.Count; i++)
{
if (matchingSavedWindows[i].MonitorBoundsWidth == CurrentScreenList[screenCurrentIndex].BoundsWidth &&
matchingSavedWindows[i].MonitorBoundsHeight == CurrentScreenList[screenCurrentIndex].BoundsHeight &&
matchingSavedWindows[i].Primary == CurrentScreenList[screenCurrentIndex].Primary)
{
result = GetWindowIndexByTag(matchingSavedWindows[i].Tag);
break;
}
}
return result;
}
//**********************************************
/// <summary> Finds the first window with the supplied data, or -1 </summary>
/// <param name="ClsWindowProps, ClsScreenList"></param>
/// <returns>Index</returns>
//**********************************************
//public int GetIndexAllScreens(ClsWindowProps CurrentWindowProps)
public int GetIndexAllScreens(ClsWindowProps WindowProps, List<ClsScreenList> ScreenList, int ScreenIndex)
{
int result = -1;
ClsDebug.AddText("GetIndexAllScreens: " + WindowProps.Title);
// Get list of matching saved windows
List<ClsWindowProps> matchingSavedWindows = new List<ClsWindowProps>();
for (int i = 0; i < this.Props.Count; i++)
{
bool FoundTitleInclude = true;
if (this.Props[i].SearchTitleInclude)
{
FoundTitleInclude = false;
switch (this.Props[i].SearchTypeInclude)
{
case ClsWindowProps.Full:
FoundTitleInclude = (WindowProps.Title == this.Props[i].TitleInclude);
break;
case ClsWindowProps.Contains:
if (WindowProps.Title.Contains("Loading"))
Console.WriteLine(WindowProps.Title);
foreach (string titleInclude in (this.Props[i].TitleInclude).Split('|'))
{
if (WindowProps.Title.Contains(titleInclude))
{
FoundTitleInclude = true;
break;
}
}
break;
case ClsWindowProps.StartsWith:
foreach (string titleInclude in (this.Props[i].TitleInclude).Split('|'))
{
if (WindowProps.Title.StartsWith(titleInclude)) {
FoundTitleInclude = true;
break;
}
}
break;
}
}
bool FoundTitleExclude = false;
if (this.Props[i].SearchTitleExclude)
{
switch (this.Props[i].SearchTypeExclude)
{
case ClsWindowProps.Full:
FoundTitleExclude = (WindowProps.Title == this.Props[i].TitleExclude);
break;
case ClsWindowProps.Contains:
//FoundTitleExclude = WindowProps.Title.Contains(this.Props[i].TitleExclude);
foreach (string titleExclude in (this.Props[i].TitleExclude).Split('|'))
{
if (WindowProps.Title.Contains(titleExclude)) {
FoundTitleExclude = true;
break;
}
}
break;
case ClsWindowProps.StartsWith:
//FoundTitleExclude = WindowProps.Title.StartsWith(this.Props[i].TitleExclude);
foreach (string titleExclude in (this.Props[i].TitleExclude).Split('|'))
{
if (WindowProps.Title.StartsWith(titleExclude)) {
FoundTitleExclude = true;
break;
}
}
break;
}
}
bool FoundExe = true;
if (this.Props[i].SearchExe)
FoundExe = (WindowProps.Exe == this.Props[i].Exe);
bool FoundWindowClass = true;
if (this.Props[i].ConsiderWindowClass)
FoundWindowClass = (WindowProps.WindowClass == this.Props[i].WindowClass);
if (FoundTitleInclude && !FoundTitleExclude && FoundExe && FoundWindowClass)
{
matchingSavedWindows.Add(this.Props[i]);
}
}
for (int i = 0; i < matchingSavedWindows.Count; i++)
{
for (int j = 0; j < ScreenList.Count; j++)
{
// Another screen than current and not primary
if (matchingSavedWindows[i].MonitorBoundsWidth == ScreenList[j].BoundsWidth &&
matchingSavedWindows[i].MonitorBoundsHeight == ScreenList[j].BoundsHeight &&
matchingSavedWindows[i].Primary == ScreenList[j].Primary &&
j != ScreenIndex &&
ScreenList[j].Primary == false &&
ScreenList[j].Present)
{
ClsDebug.AddText(" Another screen than current and not primary (" + j + ")");
result = GetWindowIndexByTag(matchingSavedWindows[i].Tag);
break;
}
}
}
for (int i = 0; i < matchingSavedWindows.Count; i++)
{
for (int j = 0; j < ScreenList.Count; j++)
{
// Another screen than current and primary
if (matchingSavedWindows[i].MonitorBoundsWidth == ScreenList[j].BoundsWidth &&
matchingSavedWindows[i].MonitorBoundsHeight == ScreenList[j].BoundsHeight &&
matchingSavedWindows[i].Primary == ScreenList[j].Primary &&
j != ScreenIndex &&
ScreenList[j].Primary == true &&
ScreenList[j].Present)
{
ClsDebug.AddText(" Another screen than current and primary (" + j + ")");
result = GetWindowIndexByTag(matchingSavedWindows[i].Tag);
break;
}
}
}
for (int i = 0; i < matchingSavedWindows.Count; i++)
{
for (int j = 0; j < ScreenList.Count; j++)
{
// This screen matches
if (matchingSavedWindows[i].MonitorBoundsWidth == ScreenList[j].BoundsWidth &&
matchingSavedWindows[i].MonitorBoundsHeight == ScreenList[j].BoundsHeight &&
matchingSavedWindows[i].Primary == ScreenList[j].Primary &&
ScreenList[j].Present &&
j == ScreenIndex)
{
ClsDebug.AddText(" This screen matches (" + j + ")");
result = GetWindowIndexByTag(matchingSavedWindows[i].Tag);
break;
}
}
}
ClsDebug.AddText(" Saved window index chosen: " + result);
return result;
}
//**********************************************
/// <summary> Get the index of the window with the supplied Tag </summary>
/// <param name="Tag"></param>
/// <returns>i</returns>
//**********************************************
public int GetWindowIndexByTag(int Tag)
{
for (int i = 0; i < Props.Count; i++)
{
if (Props[i].Tag == Tag)
return i;
}
return -1;
}
//**********************************************
/// <summary> Saves data to the specified path </summary>
//**********************************************
public void Save(string activePath)
{
var options = new JsonSerializerOptions() { WriteIndented = true };
var saveProps = new List<List<ClsWindowProps>>();
Directory.CreateDirectory(activePath);
// Clean out only old window position files from the target directory
var fileEntries = Directory.GetFiles(activePath, "*.json");
foreach (string fileName in fileEntries)
{
if (Regex.IsMatch(Path.GetFileName(fileName), @"^\d{3,5}x\d{3,5}P?\.json$"))
{
File.Delete(fileName);
}
}
// Group window properties by screen configuration
foreach (var prop in this.Props)
{
var group = saveProps.FirstOrDefault(g =>
g[0].MonitorBoundsWidth == prop.MonitorBoundsWidth &&
g[0].MonitorBoundsHeight == prop.MonitorBoundsHeight &&
g[0].Primary == prop.Primary);
if (group != null)
{
group.Add(prop);
}
else
{
saveProps.Add(new List<ClsWindowProps> { prop });
}
}
// Write the new files
foreach (var group in saveProps)
{
string fileNameWindows = $"{group[0].MonitorBoundsWidth}x{group[0].MonitorBoundsHeight}";
fileNameWindows += group[0].Primary ? "P.json" : ".json";
string fullPath = Path.Combine(activePath, fileNameWindows);
using (var writer = new StreamWriter(fullPath))
{
String json = JsonSerializer.Serialize(group, options);
writer.Write(json);
}
}
}
//**********************************************
/// <summary> Loads data from the specified path </summary>
//**********************************************
public void Load(string activePath)
{
this.Props.Clear();
_nextTag = 0; // Reset tag counter on load
Directory.CreateDirectory(activePath);
var fileEntries = Directory.GetFiles(activePath, "*.json");
foreach (string fileName in fileEntries)
{
if (Regex.IsMatch(Path.GetFileName(fileName), @"^\d{3,5}x\d{3,5}P?\.json$"))
{
using (StreamReader r = new StreamReader(fileName))
{
String json = r.ReadToEnd();
if (json.Length > 0)
{
var loadedProps = JsonSerializer.Deserialize<List<ClsWindowProps>>(json);
foreach (var item in loadedProps)
{
// AddWindow will assign the correct, unique tag
AddWindow(item);
}
}
}
}
}
this.Props.Sort((o1, o2) => o1.Name.CompareTo(o2.Name));
}
//**********************************************
// API calls
//**********************************************
[DllImport("USER32.DLL")]
private static extern int GetWindowTextLength(IntPtr hWnd);
[DllImport("USER32.DLL")]
private static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);
[DllImport("user32.dll")]
public static extern bool GetWindowRect(IntPtr hwnd, ref Rectangle rectangle);
[DllImport("user32.dll")]
static extern uint GetWindowThreadProcessId(IntPtr hWnd, out int lpdwProcessId);
}