-
Notifications
You must be signed in to change notification settings - Fork 1
/
Win32Util.cs
142 lines (124 loc) · 5.09 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
using System;
using System.Runtime.InteropServices;
namespace WoWStarter
{
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);
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")]
[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 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);
}
}
}
}