-
Notifications
You must be signed in to change notification settings - Fork 0
/
Win32Util.cs
184 lines (157 loc) · 6.76 KB
/
Win32Util.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
using System;
using System.Runtime.InteropServices;
namespace WoWCyclotron
{
public struct RECT
{
public int Left; // x position of upper-left corner
public int Top; // y position of upper-left corner
public int Right; // x position of lower-right corner
public int Bottom; // y position of lower-right corner
}
enum KeyModifier
{
None = 0,
Alt = 1,
Control = 2,
Shift = 4,
WinKey = 8
}
public static class Win32Util
{
[DllImport("user32.dll")]
public static extern bool RegisterHotKey(IntPtr hWnd, int id, int fsModifiers, int vk);
[DllImport("user32.dll")]
public static extern bool UnregisterHotKey(IntPtr hWnd, int id);
[DllImport("user32.dll")]
public static extern short GetAsyncKeyState(int vKey); // returns flag 0x8000000 if key currently down, flag 0x1 if key was pressed since last call
[DllImport("kernel32.dll")]
public static extern bool IsDebuggerPresent();
public const int GWL_STYLE = -16;
public const int WS_BORDER = 0x00800000;
public const int WS_CAPTION = 0x00C00000;
public const int WS_SIZEBOX = 0x00040000;
[DllImport("user32.dll", SetLastError = true)]
public static extern int GetWindowLong(IntPtr hWnd, int nIndex);
[DllImport("user32.dll")]
public static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
public static void setBorderless(IntPtr wowHandle, bool borderless)
{
int style = GetWindowLong(wowHandle, GWL_STYLE);
int newStyle = borderless ? (style & ~WS_CAPTION & ~WS_SIZEBOX) : (style | WS_CAPTION | WS_SIZEBOX);
if (newStyle != style)
Win32Util.SetWindowLong(wowHandle, Win32Util.GWL_STYLE, newStyle);
}
[DllImport("user32.dll")]
public static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll")]
public static extern bool SetForegroundWindow(IntPtr handle);
[DllImport("user32.dll")]
public static extern bool SetActiveWindow(IntPtr handle);
[DllImport("user32.dll")]
public static extern IntPtr SetFocus(IntPtr handle);
public const int ASFW_ANY = -1;
[DllImport("user32.dll")]
public static extern bool AllowSetForegroundWindow(int dwProcessId);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool GetWindowRect(IntPtr hWnd, out RECT lpRect);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool GetClientRect(IntPtr hWnd, out RECT lpRect);
[DllImport("User32.dll")]
public extern static bool MoveWindow(IntPtr handle, int x, int y, int width, int height, bool redraw);
[DllImport("User32.dll")]
public extern static int GetSystemMetrics(int nIndex);
[DllImport("user32.dll")]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
public static IntPtr FindWindow(string windowName) { return FindWindow(null, windowName); }
[DllImport("user32.dll")]
public static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);
[DllImport("user32.dll")]
public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
static readonly IntPtr HWND_TOP = new IntPtr(0);
static readonly IntPtr HWND_TOPMOST = new IntPtr(-1);
static readonly IntPtr HWND_NOTOPMOST = new IntPtr(-2);
const UInt32 SWP_NOSIZE = 0x0001;
const UInt32 SWP_NOMOVE = 0x0002;
public static void PositionWindow(IntPtr fHandle, int x, int y, int w, int h, bool alwaysOnTop)
{
SetWindowPos(fHandle, HWND_TOP, x, y, w, h, 0); // SWP_FRAMECHANGED
}
public static void setAlwaysOnTop(IntPtr fHandle, bool alwaysOnTop)
{
SetWindowPos(fHandle, alwaysOnTop ? HWND_TOPMOST : HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
}
public static void MakeChild(IntPtr aHandle, IntPtr bHandle)
{
SetWindowPos(aHandle, bHandle, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
}
public static void SetOwner(IntPtr child, IntPtr parent)
{ // GWL_HWNDPARENT = -8
SetWindowLong(child, -8, (int)parent);
}
[DllImport("shell32.dll")]
public static extern UInt32 SHAppBarMessage(UInt32 dwMessage, ref APPBARDATA pData);
public const int ABM_SETAUTOHIDEBAR = 0x0a;
[StructLayout(LayoutKind.Sequential)]
public struct APPBARDATA
{
public int cbSize; // initialize this field using: Marshal.SizeOf(typeof(APPBARDATA));
public IntPtr hWnd;
public uint uCallbackMessage;
public uint uEdge;
public RECT rc;
public int lParam;
}
public static void setTaskbarAutohide(bool taskbarAutohide)
{
APPBARDATA msgData = new APPBARDATA();
msgData.cbSize = Marshal.SizeOf(msgData);
msgData.hWnd = FindWindow("System_TrayWnd", null);
msgData.lParam = taskbarAutohide ? 1 : 2;
SHAppBarMessage(ABM_SETAUTOHIDEBAR, ref msgData);
}
public const int SPI_SETACTIVEWINDOWTRACKING = 0x1001;
public const int SPI_SETACTIVEWNDTRKZORDER = 0x100D;
public const int SPI_SETACTIVEWNDTRKTIMEOUT = 0x2003;
public const int SPI_GETFOREGROUNDLOCKTIMEOUT = 0x2000;
public const int SPI_SETFOREGROUNDLOCKTIMEOUT = 0x2001;
public const int SPI_SETFOREGROUNDFLASHCOUNT = 0x2005;
public const int SPIF_SENDWININICHANGE = 2;
[DllImport("user32.dll", SetLastError = true)]
public static extern bool SystemParametersInfo(int uiAction, int uiParam, IntPtr pvParam, int fWinIni);
public static void setMouseFocusTracking(bool doMouseFocusTracking)
{
if (doMouseFocusTracking)
{
SystemParametersInfo(SPI_SETACTIVEWINDOWTRACKING, 0, new IntPtr(1), SPIF_SENDWININICHANGE);
SystemParametersInfo(SPI_SETACTIVEWNDTRKZORDER, 0, new IntPtr(0), SPIF_SENDWININICHANGE);
SystemParametersInfo(SPI_SETACTIVEWNDTRKTIMEOUT, 0, new IntPtr(0), SPIF_SENDWININICHANGE);
}
else
{
SystemParametersInfo(SPI_SETACTIVEWINDOWTRACKING, 0, new IntPtr(0), SPIF_SENDWININICHANGE);
}
}
[DllImport("Shell32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern IntPtr ShellExecute(IntPtr hwnd, string lpOperation, string lpFile,
string lpParameters, string lpDirectory, int nShowCmd);
[DllImport("user32.dll", SetLastError = true)]
public static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId);
// When you don't want the ProcessId, use this overload and pass
// IntPtr.Zero for the second parameter
[DllImport("user32.dll")]
public static extern uint GetWindowThreadProcessId(IntPtr hWnd, IntPtr ProcessId);
[DllImport("kernel32.dll")]
public static extern uint GetCurrentThreadId();
[DllImport("user32.dll")]
public static extern bool AttachThreadInput(uint idAttach, uint idAttachTo, bool fAttach);
[DllImport("user32.dll", SetLastError = true)]
public static extern bool BringWindowToTop(IntPtr hWnd);
[DllImport("user32.dll", SetLastError = true)]
public static extern bool BringWindowToTop(HandleRef hWnd);
[DllImport("user32.dll")]
public static extern bool ShowWindow(IntPtr hWnd, uint nCmdShow);
}
}