-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathClsCurrentWindows.cs
More file actions
330 lines (292 loc) · 12.6 KB
/
ClsCurrentWindows.cs
File metadata and controls
330 lines (292 loc) · 12.6 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
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Text;
namespace WinSize4
{
public class ClsCurrentWindows
{
public List<ClsWindows> Windows = new();
public struct RECT
{
public Int32 left;
public Int32 top;
public Int32 right;
public Int32 bottom;
}
public struct WINDOWINFO
{
public Int32 cbSize;
public RECT rcWindow;
public RECT rcClient;
public Int32 dwStyle;
public Int32 dwExStyle;
public Int32 dwWindowStatus;
public Int32 cxWindowBorders;
public Int32 cyWindowBorders;
public Int16 atomWindowType;
public Int16 wCreatorVersion;
}
//**********************************************
/// <summary> Adds hWnd </summary>
/// <param name="hWnd"></param>
//**********************************************
public void Add(long hWnd)
{
this.Windows.Add(new ClsWindows());
this.Windows[this.Windows.Count - 1].hWnd = hWnd;
GetWindowThreadProcessId((IntPtr)hWnd, out int Pid);
this.Windows[this.Windows.Count - 1].Pid = Pid;
UpdateWindowProperties(this.Windows.Count - 1);
}
//**********************************************
/// <summary> Finds index to the first window with the supplied hWnd, or -1 </summary>
/// <param name="hWnd"></param>
/// <returns>currentWindowIndex</returns>
//**********************************************
public int GetCurrentWindowsIndexForhWnd(long hWnd)
{
try
{
int Index = -1;
for (int i = 0; i < this.Windows.Count; i++)
{
if (this.Windows[i].hWnd == hWnd)
{
Index = i;
break;
}
}
if (Index == -1)
{
this.Add(hWnd);
Index = this.Windows.Count - 1;
}
ClsDebug.AddText("GetCurrentWindowsIndexForhWnd: " + Index);
return Index;
}
catch
(Exception ex)
{
ClsDebug.LogToEvent(ex, EventLogEntryType.Error, "");
return -1;
}
}
//**********************************************
/// <summary> Removes closed this.Windows </summary>
//**********************************************
public void CleanWindowsList()
{
var windowHandles = new List<IntPtr>();
foreach (KeyValuePair<IntPtr, string> window in ClsWindowGetter.GetOpenWindows())
{
windowHandles.Add(window.Key);
}
for (int i = 0; i < this.Windows.Count; i++)
{
if (!windowHandles.Contains((IntPtr)this.Windows[i].hWnd))
{
this.Windows.RemoveAt(i);
}
}
}
//**********************************************
/// <summary>Updates properties for the supplied window i</summary>
/// <param name="Index"></param>
//**********************************************
public void UpdateWindowProperties(int Index)
{
try
{
var Props = new ClsWindowProps();
Props = GetWindowProperties(this.Windows[Index].hWnd);
Props.Present = true;
this.Windows[Index].Props = Props;
//this.Windows[Index].Moved = false;
//if (this.Windows[Index].Props.Exe == "explorer" && this.Windows[Index].Props.Exe == "msedge")
// this.Windows[Index].Props.IgnoreChildWindows = false;
}
catch
(Exception ex)
{
EventLog.WriteEntry("WinSize4", ex.Message, EventLogEntryType.Error, 1);
}
}
//**********************************************
/// <summary>Updates properties for the supplied window i</summary>
/// <param name="hWnd"></param>
/// <returns>ClsWindowProps</returns>
//**********************************************
public ClsWindowProps GetWindowProperties(long hWnd)
{
ClsWindowProps Props = new();
try
{
StringBuilder ClassName = new(256);
GetClassName((IntPtr)hWnd, ClassName, ClassName.Capacity);
Props.WindowClass = ClassName.ToString();
Screen screen = Screen.FromHandle((IntPtr)hWnd);
Props.MonitorBoundsWidth = screen.Bounds.Width;
Props.MonitorBoundsHeight = screen.Bounds.Height;
Props.Primary = screen.Primary;
int length = GetWindowTextLength((IntPtr)hWnd);
var builder = new StringBuilder(length);
GetWindowText((IntPtr)hWnd, builder, length + 1);
Props.Title = builder.ToString();
//Props.Name = screen.Bounds.Width + "x" + screen.Bounds.Height + ((screen.Primary) ? "P" : "") + " - " + Props.Title;
Props.Name = Props.Title;
Props.TitleInclude = Props.Title;
Props.TitleExclude = Props.Title;
var rect = new Rectangle();
GetWindowRect((IntPtr)hWnd, ref rect);
const int GWL_STYLE = -16;
const int WS_SIZEBOX = 0x00040000;
int style = GetWindowLong((IntPtr)hWnd, GWL_STYLE);
Props.CanResize = (style & WS_SIZEBOX) != 0;
Props.Top = rect.Top;
Props.Left = rect.Left;
Props.Width = rect.Width - rect.Left;
Props.Height = rect.Height - rect.Top;
GetWindowThreadProcessId((IntPtr)hWnd, out int ProcIdXL);
Process xproc = Process.GetProcessById(ProcIdXL);
if (xproc is null)
{
Props.Exe = "";
}
else
{
Props.Exe = xproc.ProcessName;
}
//int a = 0;
//int b = 1 / a;
}
catch
(Exception ex)
{
ClsDebug.LogToEvent(ex, EventLogEntryType.Error, "");
}
return Props;
}
//**********************************************
/// <summary> Moves window to the matching saved window, run this method on ClsSavedWindows </summary>
/// <param name="SavedWindow">The window to use for location</param>
/// <param name=""></param>
//**********************************************
public void MoveCurrentWindow(int currentWindowIndex, ClsWindowProps savedWindowProps, int savedWindowIndex, ClsScreenList savedScreenProps, ClsScreenList currentScreenProps, string dataPath)
{
try
{
ClsDebug.AddText("MoveCurrentWindow: " + savedWindowProps.Name + ": Current window i: " + currentWindowIndex + ": Saved window i: " + savedWindowIndex);
ClsDebug.AddText(" Screen: " + savedScreenProps.BoundsWidth + " " + savedScreenProps.BoundsHeight + " " + savedScreenProps.Primary);
//ClsDebug.AddText(" Window: " + )
int Left, Top, Width, Height;
bool Return;
WINDOWPLACEMENT wp = new();
GetWindowPlacement((IntPtr)this.Windows[currentWindowIndex].hWnd, ref wp);
wp.showCmd = 1; // 1- Normal; 2 - Minimize; 3 - Maximize;
if (savedWindowProps.FullScreen)
{
wp.showCmd = 3; // 1- Normal; 2 - Minimize; 3 - Maximize;
ClsDebug.AddText(" Setting full screen");
SetWindowPlacement((IntPtr)this.Windows[currentWindowIndex].hWnd, ref wp);
}
else
{
if (savedWindowProps.MaxWidth)
{
Width = savedScreenProps.CustomWidth;
Left = 0;
}
else
{
Width = savedWindowProps.Width;
Left = savedWindowProps.Left;
}
if (savedWindowProps.MaxHeight)
{
Height = savedScreenProps.CustomHeight;
Top = 0;
}
else
{
Height = savedWindowProps.Height;
Top = savedWindowProps.Top;
}
SetWindowPlacement((IntPtr)this.Windows[currentWindowIndex].hWnd, ref wp);
ClsDebug.AddText(" Moving to: " + Left + " " + Top + " " + Width + " " + Height);
if (savedWindowProps.CanResize)
Return = MoveWindow((IntPtr)this.Windows[currentWindowIndex].hWnd, Left, Top, Width, Height, true);
else
Return = SetWindowPos(hWnd: (IntPtr)this.Windows[currentWindowIndex].hWnd, X: Left, Y: Top, nWidth: 0, nHeight: 0, uFlags: 1);
if (Return == false)
{
ClsDebug.LogNow(dataPath, "Window could not be moved: " + savedWindowProps.Name);
ClsDebug.LogToEvent(new Exception(""), EventLogEntryType.Error, " Window could not be moved: " + savedWindowProps.Name);
}
}
}
catch
(Exception ex)
{
ClsDebug.LogToEvent(ex, EventLogEntryType.Error, "");
}
}
[StructLayout(LayoutKind.Sequential)]
public struct WINDOWPLACEMENT
{
public uint length;
public uint flags;
public uint showCmd;
public POINT ptMinPosition;
public POINT ptMaxPosition;
public Rectangle rcNormalPosition;
public Rectangle rcDevice;
}
[StructLayout(LayoutKind.Sequential)]
public struct POINT
{
public int x;
public int y;
}
public void ResetMoved(string dataPath)
{
foreach (ClsWindows Win in this.Windows)
{
Win.Moved = false;
}
ClsDebug.LogNow(dataPath, "ResetMoved\n");
}
//**********************************************
// API calls
//**********************************************
[DllImport("user32.dll")]
static extern bool SetWindowPlacement(IntPtr hWnd, [In] ref WINDOWPLACEMENT lpwndpl);
[DllImport("USER32.DLL")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool GetWindowPlacement(IntPtr hWnd, ref WINDOWPLACEMENT lpwndpl);
[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")]
public static extern bool GetWindowInfo(IntPtr hwnd, out WINDOWINFO wi);
[DllImport("user32.dll", SetLastError = true)]
static extern uint GetWindowThreadProcessId(IntPtr hWnd, out int processId);
[DllImport("user32.dll")]
public static extern bool MoveWindow(IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint);
[DllImport("user32.dll")]
public static extern bool SetWindowPos(IntPtr hWnd, [In, Optional] IntPtr hWndInsertAfter, int X, int Y, int nWidth, int nHeight, uint uFlags);
[DllImport("user32.dll", EntryPoint = "SetWindowPos")]
public static extern IntPtr SetWindowPos(IntPtr hWnd, int hWndInsertAfter, int x, int Y, int cx, int cy, int wFlags);
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
static extern int GetClassName(IntPtr hWnd, StringBuilder lpClassName, int nMaxCount);
[DllImport("user32.dll", ExactSpelling = true, CharSet = CharSet.Auto)]
static extern IntPtr GetParent(IntPtr hWnd);
[DllImport("user32.dll", SetLastError = true)]
static extern int GetWindowLong(IntPtr hWnd, int nIndex);
const short SWP_NOSIZE = 1;
const short SWP_NOZORDER = 0X4;
const int SWP_NOACTIVATE = 0x0010;
}
}