-
Notifications
You must be signed in to change notification settings - Fork 12
/
WindowsAPI.cs
253 lines (203 loc) · 8.89 KB
/
WindowsAPI.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
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
/* AeroShot - Transparent screenshot utility for Windows
Copyright (C) 2015 toe_head2001
Copyright (C) 2012 Caleb Joseph
AeroShot is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
AeroShot is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */
using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Text;
namespace AeroShot
{
internal delegate bool CallBackPtr(IntPtr hwnd, int lParam);
[StructLayout(LayoutKind.Sequential)]
internal struct WindowsRect
{
internal int Left;
internal int Top;
internal int Right;
internal int Bottom;
internal WindowsRect(int x)
{
Left = x;
Top = x;
Right = x;
Bottom = x;
}
}
[StructLayout(LayoutKind.Sequential)]
internal struct WindowsMargins
{
internal int LeftWidth;
internal int RightWidth;
internal int TopHeight;
internal int BottomHeight;
internal WindowsMargins(int left, int right, int top, int bottom)
{
LeftWidth = left;
RightWidth = right;
TopHeight = top;
BottomHeight = bottom;
}
}
[StructLayout(LayoutKind.Sequential)]
internal struct BitmapInfo
{
public Int32 biSize;
public Int32 biWidth;
public Int32 biHeight;
public Int16 biPlanes;
public Int16 biBitCount;
public Int32 biCompression;
public Int32 biSizeImage;
public Int32 biXPelsPerMeter;
public Int32 biYPelsPerMeter;
public Int32 biClrUsed;
public Int32 biClrImportant;
}
[StructLayout(LayoutKind.Sequential)]
internal struct IconInfoStruct
{
internal bool fIcon;
internal Int32 xHotspot;
internal Int32 yHotspot;
internal IntPtr hbmMask;
internal IntPtr hbmColor;
}
[StructLayout(LayoutKind.Sequential)]
internal struct CursorInfoStruct
{
internal Int32 cbSize;
internal Int32 flags;
internal IntPtr hCursor;
internal PointStruct ptScreenPos;
}
[StructLayout(LayoutKind.Sequential)]
internal struct PointStruct
{
internal int X;
internal int Y;
}
internal enum DwmWindowAttribute
{
NonClientRenderingEnabled = 1,
NonClientRenderingPolicy,
TransitionsForceDisabled,
AllowNonClientPaint,
CaptionButtonBounds,
NonClientRtlLayout,
ForceIconicRepresentation,
Flip3DPolicy,
ExtendedFrameBounds,
HasIconicBitmap,
DisallowPeek,
ExcludedFromPeek,
Last
}
internal static class WindowsApi
{
// Safe method of calling dwmapi.dll, for versions of Windows lower than Vista
internal static int DwmGetWindowAttribute(IntPtr hWnd, DwmWindowAttribute dwAttribute, ref WindowsRect pvAttribute, int cbAttribute)
{
IntPtr dwmDll = LoadLibrary("dwmapi.dll");
if (dwmDll == IntPtr.Zero)
return Marshal.GetLastWin32Error();
IntPtr dwmFunction = GetProcAddress(dwmDll, "DwmGetWindowAttribute");
if (dwmFunction == IntPtr.Zero)
return Marshal.GetLastWin32Error();
var call = (DwmGetWindowAttributeDelegate)Marshal.GetDelegateForFunctionPointer(dwmFunction, typeof(DwmGetWindowAttributeDelegate));
int result = call(hWnd, dwAttribute, ref pvAttribute, cbAttribute);
FreeLibrary(dwmDll);
return result;
}
internal struct DWM_COLORIZATION_PARAMS
{
public uint clrColor;
public uint clrAfterGlow;
public uint nIntensity;
public uint clrAfterGlowBalance;
public uint clrBlurBalance;
public uint clrGlassReflectionIntensity;
public bool fOpaque;
}
[DllImport("user32.dll")]
internal static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll")]
internal static extern bool SetForegroundWindow(IntPtr hWnd);
[DllImport("user32.dll")]
internal static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
[DllImport("user32.dll")]
internal static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int x, int y, int width, int height, uint uFlags);
[DllImport("user32.dll")]
internal static extern bool GetWindowRect(IntPtr hWnd, ref WindowsRect rect);
[DllImport("user32.dll")]
internal static extern long GetWindowLong(IntPtr hWnd, int nIndex);
[DllImport("user32.dll")]
internal static extern IntPtr GetWindow(IntPtr hWnd, uint uCmd);
[DllImport("user32.dll")]
internal static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);
[DllImport("user32.dll")]
internal static extern int GetWindowTextLength(IntPtr hWnd);
[DllImport("user32.dll")]
internal static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll")]
public static extern bool GetCursorInfo(out CursorInfoStruct pci);
[DllImport("user32.dll")]
public static extern IntPtr DestroyIcon(IntPtr hIcon);
[DllImport("user32.dll")]
public static extern IntPtr CopyIcon(IntPtr hIcon);
[DllImport("user32.dll")]
public static extern bool GetIconInfo(IntPtr hIcon, out IconInfoStruct piconinfo);
[DllImport("user32.dll")]
internal static extern bool RegisterHotKey(IntPtr hWnd, int id, int fsModifiers, int vlc);
[DllImport("user32.dll")]
internal static extern bool UnregisterHotKey(IntPtr hWnd, int id);
[DllImport("user32.dll")]
internal static extern bool SystemParametersInfo(uint uiAction, uint uiParam, ref int pvParam, uint fWinIni);
[DllImport("user32.dll")]
internal static extern bool SystemParametersInfo(uint uiAction, uint uiParam, uint pvParam, uint fWinIni);
[DllImport("user32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool SystemParametersInfo(uint uiAction, uint uiParam, bool pvParam, uint fWinIni);
[DllImport("user32.dll")]
internal static extern bool RedrawWindow(IntPtr hWnd, IntPtr lpRectUpdate, IntPtr hrgnUpdate, UInt32 flags);
[DllImport("dwmapi.dll")]
internal static extern int DwmIsCompositionEnabled(out bool enabled);
[DllImport("dwmapi.dll", EntryPoint = "#127", PreserveSig = false)]
internal static extern void DwmGetColorizationParameters(out DWM_COLORIZATION_PARAMS parameters);
[DllImport("dwmapi.dll", EntryPoint = "#131", PreserveSig = false)]
internal static extern void DwmSetColorizationParameters(ref DWM_COLORIZATION_PARAMS parameters, bool unknown);
[DllImport("gdi32.dll")]
internal static extern bool BitBlt(IntPtr hdcDest, int xDest, int yDest, int wDest, int hDest, IntPtr hdcSource, int xSrc, int ySrc, CopyPixelOperation rop);
[DllImport("gdi32.dll")]
internal static extern IntPtr CreateDC(string lpszDriver, string lpszDevice, string lpszOutput, int lpInitData);
[DllImport("gdi32.dll")]
internal static extern IntPtr DeleteDC(IntPtr hDc);
[DllImport("gdi32.dll")]
internal static extern int SaveDC(IntPtr hdc);
[DllImport("gdi32.dll")]
internal static extern IntPtr DeleteObject(IntPtr hDc);
[DllImport("gdi32.dll")]
internal static extern IntPtr CreateCompatibleBitmap(IntPtr hdc, int nWidth, int nHeight);
[DllImport("gdi32.dll")]
internal static extern IntPtr CreateCompatibleDC(IntPtr hdc);
[DllImport("gdi32.dll")]
internal static extern IntPtr SelectObject(IntPtr hdc, IntPtr bmp);
[DllImport("kernel32.dll", SetLastError = true)]
private static extern bool FreeLibrary(IntPtr hModule);
[DllImport("kernel32.dll", SetLastError = true)]
private static extern IntPtr LoadLibrary(string dllToLoad);
[DllImport("kernel32.dll", SetLastError = true)]
private static extern IntPtr GetProcAddress(IntPtr hModule, string procedureName);
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
private delegate int DwmGetWindowAttributeDelegate(IntPtr hWnd, DwmWindowAttribute dwAttribute, ref WindowsRect pvAttribute, int cbAttribute);
}
}