-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNativeMethods2.cs
466 lines (409 loc) · 14.2 KB
/
NativeMethods2.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
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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
using System;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.ComTypes;
using DirectShowLib;
namespace babgvant.EVRPlay
{
public sealed class NativeMethods2
{
#region Constructors
private NativeMethods2()
{
}
#endregion Constructors
#region Struct
public struct POINTAPI
{
public int x;
public int y;
}
public struct RECT
{
public int left;
public int top;
public int right;
public int bottom;
}
public struct WINDOWPLACEMENT
{
public int length;
public int flags;
public int showCmd;
public POINTAPI ptMinPosition;
public POINTAPI ptMaxPosition;
public RECT rcNormalPosition;
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public struct WNDCLASS
{
public uint style;
public WndProc lpfnWndProc;
public int cbClsExtra;
public int cbWndExtra;
public IntPtr hInstance;
public IntPtr hIcon;
public IntPtr hCursor;
public IntPtr hbrBackground;
public string lpszMenuName;
public string lpszClassName;
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public struct WNDCLASSEX
{
[MarshalAs(UnmanagedType.U4)]
public int cbSize;
[MarshalAs(UnmanagedType.U4)]
public int style;
public WndProc lpfnWndProc;
public int cbClsExtra;
public int cbWndExtra;
public IntPtr hInstance;
public IntPtr hIcon;
public IntPtr hCursor;
public IntPtr hbrBackground;
public string lpszMenuName;
public string lpszClassName;
public IntPtr hIconSm;
}
#endregion
#region Enums
public enum ShowWindowFlags
{
Hide = 0,
ShowNormal = 1,
Normal = 1,
ShowMinimized = 2,
ShowMaximized = 3,
Maximize = 3,
ShowNoActivate = 4,
Show = 5,
Minimize = 6,
ShowMinNoActive = 7,
ShowNA = 8,
Restore = 9,
ShowDefault = 10,
ForceMinimize = 11,
Max = 11,
}
//public const int WM_COMMAND = 0x0111;
//public const int WM_SYSCOMMAND = 0x0112;
public enum SystemMenuCommands
{
SIZE = 0xF000,
MOVE = 0xF010,
MINIMIZE = 0xF020,
MAXIMIZE = 0xF030,
NEXTWINDOW = 0xF040,
PREVWINDOW = 0xF050,
CLOSE = 0xF060,
VSCROLL = 0xF070,
HSCROLL = 0xF080,
MOUSEMENU = 0xF090,
KEYMENU = 0xF100,
ARRANGE = 0xF110,
RESTORE = 0xF120,
TASKLIST = 0xF130,
SCREENSAVE = 0xF140,
HOTKEY = 0xF150,
DEFAULT = 0xF160,
MONITORPOWER = 0xF170,
CONTEXTHELP = 0xF180,
SEPARATOR = 0xF00F,
SCF_ISSECURE = 0x00000001
}
public enum VirtualKeys : int
{
LBUTTON = 0x01,
RBUTTON = 0x02,
CANCEL = 0x03,
MBUTTON = 0x04,
//
XBUTTON1 = 0x05,
XBUTTON2 = 0x06,
//
BACK = 0x08,
TAB = 0x09,
//
CLEAR = 0x0C,
RETURN = 0x0D,
//
SHIFT = 0x10,
CONTROL = 0x11,
MENU = 0x12,
PAUSE = 0x13,
CAPITAL = 0x14,
//
KANA = 0x15,
HANGEUL = 0x15, /* old name - should be here for compatibility */
HANGUL = 0x15,
JUNJA = 0x17,
FINAL = 0x18,
HANJA = 0x19,
KANJI = 0x19,
//
ESCAPE = 0x1B,
//
CONVERT = 0x1C,
NONCONVERT = 0x1D,
ACCEPT = 0x1E,
MODECHANGE = 0x1F,
//
SPACE = 0x20,
PRIOR = 0x21,
NEXT = 0x22,
END = 0x23,
HOME = 0x24,
LEFT = 0x25,
UP = 0x26,
RIGHT = 0x27,
DOWN = 0x28,
SELECT = 0x29,
PRINT = 0x2A,
EXECUTE = 0x2B,
SNAPSHOT = 0x2C,
INSERT = 0x2D,
DELETE = 0x2E,
HELP = 0x2F,
//
LWIN = 0x5B,
RWIN = 0x5C,
APPS = 0x5D,
//
SLEEP = 0x5F,
//
NUMPAD0 = 0x60,
NUMPAD1 = 0x61,
NUMPAD2 = 0x62,
NUMPAD3 = 0x63,
NUMPAD4 = 0x64,
NUMPAD5 = 0x65,
NUMPAD6 = 0x66,
NUMPAD7 = 0x67,
NUMPAD8 = 0x68,
NUMPAD9 = 0x69,
MULTIPLY = 0x6A,
ADD = 0x6B,
SEPARATOR = 0x6C,
SUBTRACT = 0x6D,
DECIMAL = 0x6E,
DIVIDE = 0x6F,
F1 = 0x70,
F2 = 0x71,
F3 = 0x72,
F4 = 0x73,
F5 = 0x74,
F6 = 0x75,
F7 = 0x76,
F8 = 0x77,
F9 = 0x78,
F10 = 0x79,
F11 = 0x7A,
F12 = 0x7B,
F13 = 0x7C,
F14 = 0x7D,
F15 = 0x7E,
F16 = 0x7F,
F17 = 0x80,
F18 = 0x81,
F19 = 0x82,
F20 = 0x83,
F21 = 0x84,
F22 = 0x85,
F23 = 0x86,
F24 = 0x87,
//
NUMLOCK = 0x90,
SCROLL = 0x91,
//
OEM_NEC_EQUAL = 0x92, // '=' key on numpad
//
OEM_FJ_JISHO = 0x92, // 'Dictionary' key
OEM_FJ_MASSHOU = 0x93, // 'Unregister word' key
OEM_FJ_TOUROKU = 0x94, // 'Register word' key
OEM_FJ_LOYA = 0x95, // 'Left OYAYUBI' key
OEM_FJ_ROYA = 0x96, // 'Right OYAYUBI' key
//
LSHIFT = 0xA0,
RSHIFT = 0xA1,
LCONTROL = 0xA2,
RCONTROL = 0xA3,
LMENU = 0xA4,
RMENU = 0xA5,
//
BROWSER_BACK = 0xA6,
BROWSER_FORWARD = 0xA7,
BROWSER_REFRESH = 0xA8,
BROWSER_STOP = 0xA9,
BROWSER_SEARCH = 0xAA,
BROWSER_FAVORITES = 0xAB,
BROWSER_HOME = 0xAC,
//
VOLUME_MUTE = 0xAD,
VOLUME_DOWN = 0xAE,
VOLUME_UP = 0xAF,
MEDIA_NEXT_TRACK = 0xB0,
MEDIA_PREV_TRACK = 0xB1,
MEDIA_STOP = 0xB2,
MEDIA_PLAY_PAUSE = 0xB3,
LAUNCH_MAIL = 0xB4,
LAUNCH_MEDIA_SELECT = 0xB5,
LAUNCH_APP1 = 0xB6,
LAUNCH_APP2 = 0xB7,
//
OEM_1 = 0xBA, // ';:' for US
OEM_PLUS = 0xBB, // '+' any country
OEM_COMMA = 0xBC, // ',' any country
OEM_MINUS = 0xBD, // '-' any country
OEM_PERIOD = 0xBE, // '.' any country
OEM_2 = 0xBF, // '/?' for US
OEM_3 = 0xC0, // '`~' for US
//
OEM_4 = 0xDB, // '[{' for US
OEM_5 = 0xDC, // '\|' for US
OEM_6 = 0xDD, // ']}' for US
OEM_7 = 0xDE, // ''"' for US
OEM_8 = 0xDF,
//
OEM_AX = 0xE1, // 'AX' key on Japanese AX kbd
OEM_102 = 0xE2, // "<>" or "\|" on RT 102-key kbd.
ICO_HELP = 0xE3, // Help key on ICO
ICO_00 = 0xE4, // 00 key on ICO
//
PROCESSKEY = 0xE5,
//
ICO_CLEAR = 0xE6,
//
PACKET = 0xE7,
//
OEM_RESET = 0xE9,
OEM_JUMP = 0xEA,
OEM_PA1 = 0xEB,
OEM_PA2 = 0xEC,
OEM_PA3 = 0xED,
OEM_WSCTRL = 0xEE,
OEM_CUSEL = 0xEF,
OEM_ATTN = 0xF0,
OEM_FINISH = 0xF1,
OEM_COPY = 0xF2,
OEM_AUTO = 0xF3,
OEM_ENLW = 0xF4,
OEM_BACKTAB = 0xF5,
//
ATTN = 0xF6,
CRSEL = 0xF7,
EXSEL = 0xF8,
EREOF = 0xF9,
PLAY = 0xFA,
ZOOM = 0xFB,
NONAME = 0xFC,
PA1 = 0xFD,
OEM_CLEAR = 0xFE
}
#endregion Enums
public delegate IntPtr WndProc(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam);
#region Methods
[DllImport("user32.dll")]
public static extern VirtualKeys GetKeyState(int nVirtKey);
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern EXECUTION_STATE SetThreadExecutionState(EXECUTION_STATE esFlags);
[DllImport("ole32.dll")]
public static extern int OleLoadFromStream(IStream pStm, [In] ref Guid riid, [MarshalAs(UnmanagedType.IUnknown)] out object ppvObj);
[DllImport("ole32.dll")]
public static extern int OleSaveToStream(IPersistStream pPStm, IStream pStm);
[DllImport("user32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool SystemParametersInfo(SPI uiAction, uint uiParam, IntPtr pvParam, SPIF fWinIni);
[DllImport("kernel32.dll", SetLastError = true)]
public static extern IntPtr CreateEvent(IntPtr securityAttributes, bool manualReset, bool initialState, string name);
[DllImport("user32.dll")]
public static extern bool EnableWindow(IntPtr handle, bool enable);
[DllImport("user32.dll")]
public static extern IntPtr FindWindow(string className, string windowName);
[DllImport("user32.dll")]
public static extern bool IsWindowVisible(IntPtr handle);
[DllImport("user32.dll")]
public static extern bool IsWindowEnabled(IntPtr handle);
[DllImport("user32")]
public static extern bool ShowWindow(IntPtr handle, ShowWindowFlags showCommand);
[DllImport("user32.dll", SetLastError = true)]
public static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId);
[DllImport("user32.dll", SetLastError = true)]
public static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)]
public static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)]
public static extern IntPtr SendMessage(IntPtr hWnd, WM Msg, IntPtr wParam, IntPtr lParam);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool GetWindowPlacement(IntPtr hWnd, ref WINDOWPLACEMENT lpwndpl);
[DllImport("user32")]
private static extern bool SetFocus(IntPtr hWnd);
[DllImport("user32")]
private static extern bool SetForegroundWindow(IntPtr hWnd);
[DllImport("user32")]
private static extern bool BringWindowToTop(IntPtr hWnd);
[DllImport("user32")]
private static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);
[DllImport("user32")]
private static extern bool AttachThreadInput(int nThreadId, int nThreadIdTo, bool bAttach);
[DllImport("user32")]
private static extern int GetWindowThreadProcessId(IntPtr hWnd, int unused);
[DllImport("user32")]
static extern bool IsIconic(IntPtr hWnd);
[DllImport("user32.dll")]
public static extern IntPtr CreateWindowEx(
uint dwExStyle,
string lpClassName,
string lpWindowName,
uint dwStyle,
int x,
int y,
int nWidth,
int nHeight,
IntPtr hWndParent,
IntPtr hMenu,
IntPtr hInstance,
IntPtr lpParam);
[DllImport("coredll.dll", SetLastError = true, CallingConvention = CallingConvention.Winapi, CharSet = CharSet.Auto)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool CloseHandle(IntPtr hObject);
[DllImport("user32.dll")]
public static extern bool DestroyWindow(IntPtr hWnd);
[DllImport("user32.dll")]
public static extern ushort RegisterClass([In] ref WNDCLASS lpWndClass);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.U2)]
public static extern short RegisterClassEx([In] ref WNDCLASSEX lpwcx);
[DllImport("user32.dll", SetLastError = true)]
public static extern IntPtr DefWindowProcW(
IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam
);
public static WINDOWPLACEMENT GetPlacement(IntPtr hWnd)
{
WINDOWPLACEMENT placement = new WINDOWPLACEMENT();
placement.length = Marshal.SizeOf(placement);
GetWindowPlacement(hWnd, ref placement);
return placement;
}
public static bool SetForegroundWindow(IntPtr window, bool force)
{
IntPtr windowForeground = GetForegroundWindow();
if (window == windowForeground || SetForegroundWindow(window))
return true;
if (force == false)
return false;
if (windowForeground == IntPtr.Zero)
return false;
// if we don't attach successfully to the windows thread then we're out of options
if (!AttachThreadInput(AppDomain.GetCurrentThreadId(), GetWindowThreadProcessId(windowForeground, 0), true))
return false;
SetForegroundWindow(window);
BringWindowToTop(window);
SetFocus(window);
AttachThreadInput(AppDomain.GetCurrentThreadId(), GetWindowThreadProcessId(windowForeground, 0), false);
// we've done all that we can so base our return value on whether we have succeeded or not
return (GetForegroundWindow() == window);
}
#endregion Methods
}
}