forked from goupviet/ManagedWin32
-
Notifications
You must be signed in to change notification settings - Fork 0
/
WindowHandler.cs
218 lines (179 loc) · 6.9 KB
/
WindowHandler.cs
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
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Interop;
using ManagedWin32.Api;
using Point = System.Drawing.Point;
using Size = System.Drawing.Size;
namespace ManagedWin32
{
public class WindowHandler
{
#region Constructors
/// <summary>
/// Constructs a Window Object
/// </summary>
/// <param name="hWnd">Handle</param>
public WindowHandler(IntPtr hWnd)
{
if (!User32.IsWindow(hWnd)) throw new ArgumentException("The Specified Handle is not a Window.", nameof(hWnd));
this.Handle = hWnd;
}
public WindowHandler(Window Window) : this(new WindowInteropHelper(Window).Handle) { }
#endregion
#region Properties
public IntPtr Handle { get; }
public string Title
{
get { return Handle == DesktopWindow.Handle ? "Desktop" : User32.GetWindowText(Handle); }
set { User32.SetWindowText(Handle, value); }
}
public string Module
{
get
{
var module = new StringBuilder(256);
User32.GetWindowModuleFileName(Handle, module, 256);
return module.ToString();
}
}
public bool IsMaximized => User32.IsZoomed(Handle);
public bool IsMinimized => User32.IsIconic(Handle);
public bool IsForegroundWindow => Handle == User32.GetForegroundWindow();
public bool IsUntitled => string.IsNullOrEmpty(Title);
/// <summary>
/// Sets this Window Object's visibility
/// </summary>
public bool IsVisible
{
get { return User32.IsWindowVisible(Handle); }
set
{
//show the window
if (value)
User32.ShowWindowAsync(Handle, ShowWindowFlags.Normal);
//hide the window
else Hide();
}
}
public WindowHandler Parent => new WindowHandler(User32.GetParent(Handle));
public bool IsEnabled
{
get { return User32.IsWindowEnabled(Handle); }
set { User32.EnableWindow(Handle, value); }
}
public Process Process
{
get
{
int pid;
User32.GetWindowThreadProcessId(Handle, out pid);
return Process.GetProcessById(pid);
}
}
public ProcessThread Thread
{
get
{
int pid;
var tid = User32.GetWindowThreadProcessId(Handle, out pid);
foreach (var t in from ProcessThread t in Process.GetProcessById(pid).Threads where t.Id == tid select t)
return t;
throw new Exception("Thread not found");
}
}
/// <summary>
/// Get the window that is below this window in the Z order,
/// or null if this is the lowest window.
/// </summary>
public WindowHandler WindowBelow
{
get
{
var res = User32.GetWindow(Handle, GetWindowEnum.Next);
return res == IntPtr.Zero ? null : new WindowHandler(res);
}
}
/// <summary>
/// Get the window that is above this window in the Z order,
/// or null, if this is the foreground window.
/// </summary>
public WindowHandler WindowAbove
{
get
{
var res = User32.GetWindow(Handle, GetWindowEnum.Previous);
return res == IntPtr.Zero ? null : new WindowHandler(res);
}
}
#endregion
public static WindowHandler ForegroundWindow
{
get { return new WindowHandler(User32.GetForegroundWindow()); }
set
{
if (value.IsForegroundWindow) return;
IntPtr ThreadID1 = User32.GetWindowThreadProcessId(User32.GetForegroundWindow(), IntPtr.Zero),
ThreadID2 = User32.GetWindowThreadProcessId(value.Handle, IntPtr.Zero);
if (ThreadID1 != ThreadID2)
{
User32.AttachThreadInput(ThreadID1, ThreadID2, 1);
User32.SetForegroundWindow(value.Handle);
User32.AttachThreadInput(ThreadID1, ThreadID2, 0);
}
else User32.SetForegroundWindow(value.Handle);
User32.ShowWindowAsync(value.Handle, value.IsMinimized ? ShowWindowFlags.Restore : ShowWindowFlags.Normal);
}
}
public static WindowHandler DesktopWindow => new WindowHandler(User32.GetDesktopWindow());
public static IEnumerable<WindowHandler> Enumerate()
{
var list = new List<WindowHandler>();
User32.EnumWindows((hWnd, lParam) =>
{
list.Add(new WindowHandler(hWnd));
return true;
}, IntPtr.Zero);
return list;
}
public bool PlaceBelow(WindowHandler wnd)
{
return User32.SetWindowPos(Handle, wnd.Handle, 0, 0, 0, 0, SetWindowPositionFlags.NoMove | SetWindowPositionFlags.NoSize);
}
public Point Location
{
set { User32.SetWindowPos(Handle, IntPtr.Zero, value.X, value.Y, 0, 0, SetWindowPositionFlags.NoSize | SetWindowPositionFlags.NoZOrder); }
}
//Override ToString()
public override string ToString()
{
//return the title if it has one, if not return the process name
return Title.Length > 0 ? Title : Process.ProcessName;
}
public override bool Equals(object obj) => (obj is WindowHandler) && Handle == ((WindowHandler) obj).Handle;
public Size Size
{
get
{
var rect = new RECT();
User32.GetWindowRect(Handle, ref rect);
return new Size
{
Width = rect.Right - rect.Left,
Height = rect.Bottom - rect.Top
};
}
set { User32.SetWindowPos(Handle, IntPtr.Zero, 0, 0, value.Width, value.Height, SetWindowPositionFlags.NoMove | SetWindowPositionFlags.NoZOrder | SetWindowPositionFlags.ShowWindow); }
}
#region Window States
public void Minimize() => User32.ShowWindowAsync(Handle, ShowWindowFlags.Minimize);
public void Restore() => User32.ShowWindowAsync(Handle, ShowWindowFlags.Restore);
public void Maximize() => User32.ShowWindowAsync(Handle, ShowWindowFlags.Maximize);
public void Hide() => User32.ShowWindowAsync(Handle, ShowWindowFlags.Hide);
public void Close() => User32.SendMessage(Handle, WindowsMessage.Close, 0, 0);
#endregion
}
}