/
HideDesktop.cs
531 lines (448 loc) · 13.8 KB
/
HideDesktop.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
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
// Copyright (c) 2015, Ben Baker
// All rights reserved.
//
// This source code is licensed under the BSD-style license found in the
// LICENSE file in the root directory of this source tree.
using System;
using System.Drawing;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.IO;
using Microsoft.Win32;
namespace CPWizard
{
class HideDesktop
{
public static IntPtr OldCursorArrow = IntPtr.Zero;
public static bool ScreenSaverActive = false;
public static Color OldDesktopColor = Color.Empty;
public static frmBlank[] BlankForms;
public static bool ForceForegroundWindow(IntPtr hWnd)
{
bool ret = false;
try
{
IntPtr hWndForeground = Win32.GetForegroundWindow();
if (hWnd == hWndForeground)
{
LogFile.VerboseWriteLine("ForceForegroundWindow", "HideDesktop", "Already Foreground hWnd: " + String.Format("0x{0:x8}", hWndForeground));
return true;
}
int foregroundThreadID = Win32.GetWindowThreadProcessId(hWndForeground, 0);
int hWndThreadID = Win32.GetWindowThreadProcessId(hWnd, 0);
if (Win32.AttachThreadInput(foregroundThreadID, hWndThreadID, 1))
{
ret = Win32.BringWindowToTop(hWnd);
ret = Win32.SetForegroundWindow(hWnd);
Win32.AttachThreadInput(foregroundThreadID, hWndThreadID, 0);
Win32.InvalidateRect(hWnd, IntPtr.Zero, true);
ret = (Win32.GetForegroundWindow() == hWnd);
if (!ret)
{
int timeout = 0; int dummy = 0;
Win32.SystemParametersInfo(Win32.SPI_GETFOREGROUNDLOCKTIMEOUT, 0, ref timeout, 0);
Win32.SystemParametersInfo(Win32.SPI_SETFOREGROUNDLOCKTIMEOUT, 0, ref dummy, Win32.SPIF_SENDCHANGE);
Win32.BringWindowToTop(hWnd); // IE 5.5 related hack
Win32.SetForegroundWindow(hWnd);
Win32.SystemParametersInfo(Win32.SPI_SETFOREGROUNDLOCKTIMEOUT, 0, ref timeout, Win32.SPIF_SENDCHANGE);
}
}
else
{
ret = Win32.BringWindowToTop(hWnd);
ret = Win32.SetForegroundWindow(hWnd);
Win32.InvalidateRect(hWnd, IntPtr.Zero, true);
}
if (Win32.IsIconic(hWnd))
Win32.ShowWindow(hWnd, (int)Win32.WindowShowStyle.Restore);
else
Win32.ShowWindow(hWnd, (int)Win32.WindowShowStyle.Show);
ret = (Win32.GetForegroundWindow() == hWnd);
if (!ret)
Win32.SwitchToThisWindow(hWnd, true);
hWndForeground = Win32.GetForegroundWindow();
LogFile.VerboseWriteLine("ForceForegroundWindow", "HideDesktop", "Foreground hWnd: " + String.Format("0x{0:x8}", hWndForeground));
}
catch (Exception ex)
{
LogFile.WriteLine("ForceForegroundWindow", "HideDesktop", ex.Message, ex.StackTrace);
}
return ret;
}
public static void AllowForceForegroundWindow()
{
try
{
Win32.AllowSetForegroundWindow(System.Diagnostics.Process.GetCurrentProcess().Id);
}
catch (Exception ex)
{
LogFile.WriteLine("AllowForceForegroundWindow", "HideDesktop", ex.Message, ex.StackTrace);
}
}
public static void ForceForegroundWindow(System.Windows.Forms.Form form)
{
try
{
if (form == null)
return;
bool topMost = form.TopMost;
form.TopMost = true;
form.Focus();
ForceForegroundWindow(form.Handle);
if (topMost == false)
form.TopMost = false;
}
catch (Exception ex)
{
LogFile.WriteLine("ForceForegroundWindow", "HideDesktop", ex.Message, ex.StackTrace);
}
}
public static void HideDesktopIcons()
{
try
{
IntPtr hWnd = IntPtr.Zero;
hWnd = Win32.FindWindow("Progman", null);
if (hWnd != IntPtr.Zero)
Win32.ShowWindow(hWnd, (int)Win32.WindowShowStyle.Hide);
}
catch (Exception ex)
{
LogFile.WriteLine("HideDesktopIcons", "HideDesktop", ex.Message, ex.StackTrace);
}
}
public static void ShowDesktopIcons()
{
try
{
IntPtr hWnd = IntPtr.Zero;
hWnd = Win32.FindWindow("Progman", null);
if (hWnd != IntPtr.Zero)
Win32.ShowWindow(hWnd, (int)Win32.WindowShowStyle.Show);
}
catch (Exception ex)
{
LogFile.WriteLine("ShowDesktopIcons", "HideDesktop", ex.Message, ex.StackTrace);
}
}
public static void HideTaskBar()
{
try
{
IntPtr hWnd = IntPtr.Zero;
hWnd = Win32.FindWindow("Shell_TrayWnd", null);
if (hWnd != IntPtr.Zero)
Win32.ShowWindow(hWnd, (int)Win32.WindowShowStyle.Hide);
}
catch (Exception ex)
{
LogFile.WriteLine("HideTaskBar", "HideDesktop", ex.Message, ex.StackTrace);
}
}
public static void ShowTaskBar()
{
try
{
IntPtr hWnd = IntPtr.Zero;
hWnd = Win32.FindWindow("Shell_TrayWnd", null);
if (hWnd != IntPtr.Zero)
Win32.ShowWindow(hWnd, (int)Win32.WindowShowStyle.Show);
}
catch (Exception ex)
{
LogFile.WriteLine("ShowTaskBar", "HideDesktop", ex.Message, ex.StackTrace);
}
}
public static void MoveMouseOffscreen()
{
try
{
Size Screen = System.Windows.Forms.Screen.AllScreens[0].Bounds.Size;
Win32.SetCursorPos(Screen.Width, Screen.Height);
}
catch (Exception ex)
{
LogFile.WriteLine("MoveMouseOffscreen", "HideDesktop", ex.Message, ex.StackTrace);
}
}
public static void SetDesktopColor(Color color)
{
try
{
OldDesktopColor = System.Drawing.ColorTranslator.FromWin32(Win32.GetSysColor(Win32.COLOR_DESKTOP));
int[] elements = { Win32.COLOR_DESKTOP };
int[] colors = { System.Drawing.ColorTranslator.ToWin32(color) };
Win32.SetSysColors(elements.Length, elements, colors);
}
catch (Exception ex)
{
LogFile.WriteLine("SetDesktopColor", "HideDesktop", ex.Message, ex.StackTrace);
}
}
public static void RestoreDesktopColor()
{
try
{
SetDesktopColor(OldDesktopColor);
}
catch (Exception ex)
{
LogFile.WriteLine("RestoreDesktopColor", "HideDesktop", ex.Message, ex.StackTrace);
}
}
public static void SetBlackWallpaper()
{
try
{
SetDesktopColor(Color.Black);
Win32.SystemParametersInfo(Win32.SPI_SETDESKWALLPAPER, 0, "", 0);
}
catch (Exception ex)
{
LogFile.WriteLine("SetBlackWallpaper", "HideDesktop", ex.Message, ex.StackTrace);
}
}
public static void RestoreWallpaper()
{
try
{
RestoreDesktopColor();
Win32.SystemParametersInfo(Win32.SPI_SETDESKWALLPAPER, 0, 0, 0);
}
catch (Exception ex)
{
LogFile.WriteLine("RestoreWallpaper", "HideDesktop", ex.Message, ex.StackTrace);
}
}
public static bool IsScreenSaverActive()
{
try
{
bool Active = false;
Win32.SystemParametersInfo(Win32.SPI_GETSCREENSAVEACTIVE, 0, ref Active, 0);
return Active;
}
catch (Exception ex)
{
LogFile.WriteLine("IsScreenSaverActive", "HideDesktop", ex.Message, ex.StackTrace);
}
return false;
}
public static bool EnableScreenSaver(bool Status)
{
try
{
return Win32.SystemParametersInfo(Win32.SPI_SETSCREENSAVEACTIVE, 0, ref Status, 0);
}
catch (Exception ex)
{
LogFile.WriteLine("EnableScreenSaver", "HideDesktop", ex.Message, ex.StackTrace);
}
return false;
}
public static void HideMouseCursor()
{
try
{
IntPtr hCursor = Win32.LoadCursorFromFile(Path.Combine(Settings.Folders.Media, "Dot.cur"));
Win32.SetSystemCursor(hCursor, Win32.OCR_NORMAL);
Win32.DestroyCursor(hCursor);
}
catch (Exception ex)
{
LogFile.WriteLine("HideMouseCursor", "HideDesktop", ex.Message, ex.StackTrace);
}
}
public static void RestoreMouseCursor()
{
try
{
Win32.SystemParametersInfo(Win32.SPI_SETCURSORS, 0, null, Win32.SPIF_UPDATEINIFILE | Win32.SPIF_SENDWININICHANGE);
}
catch (Exception ex)
{
LogFile.WriteLine("RestoreMouseCursor", "HideDesktop", ex.Message, ex.StackTrace);
}
}
public static void HideAllDesktop()
{
try
{
BlankForms = new frmBlank[System.Windows.Forms.Screen.AllScreens.Length];
for (int i = 0; i < BlankForms.Length; i++)
{
BlankForms[i] = new frmBlank();
BlankForms[i].Left = System.Windows.Forms.Screen.AllScreens[i].Bounds.Left;
BlankForms[i].Top = System.Windows.Forms.Screen.AllScreens[i].Bounds.Top;
BlankForms[i].Width = System.Windows.Forms.Screen.AllScreens[i].Bounds.Width;
BlankForms[i].Height = System.Windows.Forms.Screen.AllScreens[i].Bounds.Height;
BlankForms[i].Show();
}
}
catch (Exception ex)
{
LogFile.WriteLine("HideAllDesktop", "HideDesktop", ex.Message, ex.StackTrace);
}
}
public static void ShowAllDesktop()
{
try
{
if (BlankForms == null)
return;
for (int i = 0; i < BlankForms.Length; i++)
{
BlankForms[i].Close();
BlankForms[i].Dispose();
BlankForms[i] = null;
}
}
catch (Exception ex)
{
LogFile.WriteLine("ShowAllDesktop", "HideDesktop", ex.Message, ex.StackTrace);
}
}
public static void ShowAll()
{
try
{
if (Settings.HideDesktop.DisableScreenSaver)
EnableScreenSaver(ScreenSaverActive);
if (Settings.HideDesktop.SetWallpaperBlack)
RestoreWallpaper();
if (Settings.HideDesktop.HideDesktopIcons)
ShowDesktopIcons();
if (Settings.HideDesktop.HideTaskbar)
ShowTaskBar();
if (Settings.HideDesktop.HideDesktopUsingForms)
ShowAllDesktop();
if (Settings.HideDesktop.HideMouseCursor)
RestoreMouseCursor();
}
catch (Exception ex)
{
LogFile.WriteLine("ShowAll", "HideDesktop", ex.Message, ex.StackTrace);
}
}
public static void HideAll()
{
try
{
if (Settings.HideDesktop.DisableScreenSaver)
{
ScreenSaverActive = IsScreenSaverActive();
EnableScreenSaver(false);
}
if (Settings.HideDesktop.HideMouseCursor)
HideMouseCursor();
if (Settings.HideDesktop.HideDesktopUsingForms)
HideAllDesktop();
if (Settings.HideDesktop.SetWallpaperBlack)
SetBlackWallpaper();
if (Settings.HideDesktop.HideDesktopIcons)
HideDesktopIcons();
if (Settings.HideDesktop.HideTaskbar)
HideTaskBar();
if (Settings.HideDesktop.MoveMouseOffscreen)
MoveMouseOffscreen();
}
catch (Exception ex)
{
LogFile.WriteLine("HideAll", "HideDesktop", ex.Message, ex.StackTrace);
}
}
}
partial class Win32
{
public const int COLOR_DESKTOP = 1;
public const int IDC_ARROW = 32512;
public const int OCR_NORMAL = 32512;
public const int SPIF_UPDATEINIFILE = 0x1;
public const int SPIF_SENDWININICHANGE = 0x2;
public const int SPI_SETCURSORS = 0x57;
public const int SPI_SETDESKWALLPAPER = 0x14;
public const int SPI_SETSCREENSAVEACTIVE = 0x11;
public const int SPI_GETSCREENSAVEACTIVE = 0x10;
public const int SPI_GETSCREENSAVERRUNNING = 0x72;
public enum WallpaperStyle
{
Center = 0,
Tile = 1,
Stretch = 2
}
[DllImport("user32.dll")]
public static extern int GetSysColor(int nIndex);
[DllImport("user32.dll")]
public static extern bool SetSysColors(int cElements, int[] lpaElements, int[] lpaRgbValues);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool SetSystemCursor(IntPtr hCursor, int id);
[DllImport("user32.dll")]
public static extern IntPtr LoadCursorFromFile(string lpFileName);
[DllImport("user32.dll")]
public static extern bool DestroyCursor(IntPtr hCursor);
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern int SystemParametersInfo(int uAction, int uParam, int lpvParam, int fuWinIni);
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern int SystemParametersInfo(int uAction, int uParam, string lpvParam, int fuWinIni);
[DllImport("User32.dll", CharSet = CharSet.Auto)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool SystemParametersInfo(int uAction, int uParam, [MarshalAs(UnmanagedType.Bool)] ref bool lpvParam, int fuWinIni);
//[DllImport("user32.dll")]
//public static extern bool SetCursorPos(int X, int Y);
//[DllImport("user32.dll", SetLastError = true)]
//public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
//[DllImport("user32.dll")]
//public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
[DllImport("user32.dll")]
public static extern IntPtr GetForegroundWindow();
//[DllImport("user32.dll")]
//public static extern bool AllowSetForegroundWindow(int dwProcessId);
//[DllImport("user32.dll")]
//[return: MarshalAs(UnmanagedType.Bool)]
//public static extern bool SetForegroundWindow(IntPtr hWnd);
//[DllImport("user32.dll", SetLastError = true)]
//public static extern int GetWindowThreadProcessId(IntPtr hWnd, int lpdwProcessId);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool IsIconic(IntPtr hWnd);
//[DllImport("user32.dll")]
//public static extern bool AttachThreadInput(int idAttach, int idAttachTo, int fAttach);
[DllImport("user32.dll")]
public static extern bool SetSystemCursor(IntPtr hcur, uint id);
/* [Flags]
public enum WindowShowStyle : int
{
Hide = 0,
ShowNormal = 1,
ShowMinimized = 2,
ShowMaximized = 3,
Maximize = 3,
ShowNormalNoActivate = 4,
Show = 5,
Minimize = 6,
ShowMinNoActivate = 7,
ShowNoActivate = 8,
Restore = 9,
ShowDefault = 10,
ForceMinimized = 11
} */
public const int SPI_GETFOREGROUNDLOCKTIMEOUT = 0x2000;
public const int SPI_SETFOREGROUNDLOCKTIMEOUT = 0x2001;
public const int SPIF_SENDCHANGE = 0x2;
[DllImport("user32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool SystemParametersInfo(int uiAction, uint uiParam, IntPtr pvParam, int fWinIni);
[DllImport("user32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool SystemParametersInfo(int uiAction, uint uiParam, ref int pvParam, int fWinIni);
//[DllImport("user32.dll", SetLastError = true)]
//public static extern bool BringWindowToTop(IntPtr hWnd);
//[DllImport("user32.dll", SetLastError = true)]
//public static extern void SwitchToThisWindow(IntPtr hWnd, bool fAltTab);
///[DllImport("user32.dll")]
//public static extern bool UpdateWindow(IntPtr hWnd);
}
}