This repository has been archived by the owner on Feb 7, 2023. It is now read-only.
/
Main.cs
497 lines (425 loc) · 18.3 KB
/
Main.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
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using PassiveWallpaperF;
namespace PassiveWallpaperF
{
public partial class PassiveWallpaper : Form
{
public PassiveWallpaper()
{
InitializeComponent();
int monId = 0;
string msg = "";
//Fill out monitor info so the user knows which screen is selected when choosing a gif location
foreach (Screen screen in Screen.AllScreens)
{
string str = $"Monitor {monId}: {screen.Bounds.Width} x {screen.Bounds.Height} @ {screen.Bounds.X},{screen.Bounds.Y} Primary: {screen.Primary}\n";
msg += str;
monId++;
}
//Attempt to fetch the current wallpaper path.
string currentWallpaper = new string('\0', 260);
Natives.SystemParametersInfo((uint)Natives.SPIWALL.GET, currentWallpaper.Length, currentWallpaper, 0);
currentWallpaper = currentWallpaper.Substring(0, currentWallpaper.IndexOf('\0'));
monitor_info.Text = msg;
numericUpDown1.Maximum = monId - 1;
//Set closing events to remove spawned forms and moved windows.
if (!File.Exists(currentWallpaper))
{
//If current wallpaper path is not set then warn the user.
var message = $"Your currently set wallpaper {currentWallpaper} doesn't exist on file ie. you have moved it since it was set.\n" +
"It is recommended that you set your wallpaper prior to using this tool.\n" +
"This is because when disabling the overlays the program sets your wallpaper in order to \"refresh\" your desktop back to the original state.";
MessageBox.Show(message);
Closing += (a, b) =>
{
disable_all(false);
RestoreMovedProcesses();
};
}
else
{
Closing += (a, b) => {
disable_all();
RestoreMovedProcesses();
};
}
}
public Dictionary<int, BackgroundForm> Forms = new Dictionary<int, BackgroundForm>();
/// <summary>
/// Gets the window handle for a window below the desktop icons
/// Courtesy https://www.codeproject.com/Articles/856020/Draw-behind-Desktop-Icons-in-Windows
///
/// when you change the desktop wallpaper, a new WorkerW window between the WorkerW instance that holds the Desktop Icons (SysListView32) and the Desktop Manager is created.
/// take the handle of that new WorkerW window in order to be able to draw behind the desktop icons, directly above the wallpaper!
/// </summary>
/// <returns></returns>
public IntPtr GetHandle()
{
//Obtain Program Manager Handle
IntPtr progman = Natives.FindWindow("Progman", null);
// Send 0x052C to Progman. This message directs Progman to spawn a
// WorkerW behind the desktop icons. If it is already there, nothing
// happens.
IntPtr result = IntPtr.Zero;
Natives.SendMessageTimeout(progman,
0x052C,
new IntPtr(0),
IntPtr.Zero,
Natives.SendMessageTimeoutFlags.SMTO_NORMAL,
1000,
out result);
// Spy++ output
// .....
// 0x00010190 "" WorkerW
// ...
// 0x000100EE "" SHELLDLL_DefView
// 0x000100F0 "FolderView" SysListView32
// 0x00100B8A "" WorkerW <-- This is the WorkerW instance we are after!
// 0x000100EC "Program Manager" Progman
IntPtr workerw = IntPtr.Zero;
// We enumerate all Windows, until we find one, that has the SHELLDLL_DefView
// as a child.
// If we found that window, we take its next sibling and assign it to workerw.
Natives.EnumWindows((tophandle, topparamhandle) =>
{
IntPtr p = Natives.FindWindowEx(tophandle,
IntPtr.Zero,
"SHELLDLL_DefView",
null);
if (p != IntPtr.Zero)
{
// Gets the WorkerW Window after the current one.
workerw = Natives.FindWindowEx(IntPtr.Zero,
tophandle,
"WorkerW",
null);
}
return true;
}, IntPtr.Zero);
return workerw;
}
private void button1_Click(object sender, EventArgs e)
{
var workerw = GetHandle();
string gifPath = "";
//Select a gif file to be placed in the picture box.
using (OpenFileDialog openFileDialog = new OpenFileDialog
{
Filter = "Gif files (*.gif)|*.gif",
Title = "Open gif file",
AutoUpgradeEnabled = false
})
{
if (openFileDialog.ShowDialog() != DialogResult.OK)
{
return;
}
try
{
//Set variables to match the data returned by the selected image
gifPath = openFileDialog.FileName;
//Set the value in the picturebox as a preview of the selected image
pictureBox1.ImageLocation = gifPath;
pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
return;
}
}
// Create a new background form
var form = new BackgroundForm();
//Fetch the selected monitor
int monitorId = int.Parse(numericUpDown1.Value.ToString());
//Attempt to remove previously allocated items from that monitor
if (Forms.TryGetValue(monitorId, out var currentForm))
{
currentForm.Form.Close();
Forms.Remove(monitorId);
}
Forms.Add(monitorId, form);
// Set the background forum to be borderless
form.Form.FormBorderStyle = FormBorderStyle.None;
form.Form.Text = "PassiveWallpaper";
Screen selectedScreen = Screen.AllScreens[monitorId];
//On the loading of the background form set the new properties
form.Form.Load += (s, e2) =>
{
//Ensure the form takes up the entire screen size of the selected monitor
form.Form.Width = selectedScreen.Bounds.Width;
form.Form.Height = selectedScreen.Bounds.Height;
form.Form.Left = selectedScreen.Bounds.X;
form.Form.Top = selectedScreen.Bounds.Y;
//Create a new picturebox that fills the form and set it's image contents
PictureBox pic = new PictureBox
{
Width = form.Form.Width,
Height = form.Form.Height,
Left = 0,
Top = 0,
Name = "PictureBox",
ImageLocation = gifPath,
SizeMode = PictureBoxSizeMode.StretchImage
};
if (transparent.Checked)
{
//Set the image to be transparent backgrounded and set it's background image to be the current desktop background.
pic.BackColor = Color.Transparent;
pic.BackgroundImage = new Bitmap(GetCurrentWallpaperPath());
}
else
{
pic.BackColor = Color.White;
pic.BackgroundImage = null;
}
//Add the picturebox to the form
form.Form.Controls.Add(pic);
//Ensure the parent of the new form is the WorkerW process that was spawned between the desktop wallpaper and icons
Natives.SetParent(form.Form.Handle, workerw);
};
//Ensure that the closing event of the form is set in order to re-set the current desktop wallpaper and 'refresh' it, removing any artifacts
form.Form.Closing += (s2, a2) => SetImage();
//Finally display the new form to the user.
form.Form.Show();
}
private void disable_button_Click(object sender, EventArgs e)
{
//Attempt to find a background window allocated to the selected monitor
int monitorId = int.Parse(numericUpDown1.Value.ToString());
if (Forms.TryGetValue(monitorId, out var form))
{
//Close and dispose of any content related to the form
form.Form.Close();
form.Form.Dispose();
Forms.Remove(monitorId);
//Set the image to the wallpaper again in order to remove leftover artifacts
SetImage();
}
}
public string GetCurrentWallpaperPath()
{
//Attempts the return the currently set wallpaper path
string currentWallpaper = new string('\0', 260);
Natives.SystemParametersInfo((uint)Natives.SPIWALL.GET, currentWallpaper.Length, currentWallpaper, 0);
currentWallpaper = currentWallpaper.Substring(0, currentWallpaper.IndexOf('\0'));
return currentWallpaper;
}
/// <summary>
/// Sets the current wallpaper with the image at it's designated path. Can cause issues/black wallpaper if path incorrect/not set.
/// </summary>
public void SetImage()
{
Natives.SystemParametersInfo((uint)Natives.SPIWALL.SET, 0, GetCurrentWallpaperPath(), Natives.SPIF.SPIF_UPDATEINIFILE | Natives.SPIF.SPIF_SENDCHANGE);
}
private void disable_all_button_Click(object sender, EventArgs e)
{
disable_all();
}
public void disable_all(bool wallpaper = true)
{
//Iterate through forms and close them.
foreach (var form in Forms)
{
form.Value.Form.Close();
}
Forms.Clear();
if (MovedProcesses.Count > 0)
{
//Restore all process windows to above desktop icons.
RestoreMovedProcesses();
}
//Attempt to refresh the wallpaper
if (wallpaper) SetImage();
}
private void button1_Click_1(object sender, EventArgs e)
{
foreach (var form in Forms)
{
//Reset images in each picturebox by removing and then setting it's image location quickly.
//This is method of syncing the windows
if (form.Value.Form.Controls.Find("PictureBox", false).FirstOrDefault() is PictureBox pic)
{
var loc = pic.ImageLocation;
pic.ImageLocation = "";
pic.ImageLocation = loc;
}
}
}
private void transparent_CheckedChanged(object sender, EventArgs e)
{
//Iterate through each window and either set or remove transparent background settings
foreach (var form in Forms)
{
if (form.Value.Form.Controls.Find("PictureBox", false).FirstOrDefault() is PictureBox pic)
{
if (transparent.Checked)
{
pic.BackColor = Color.Transparent;
pic.BackgroundImage = new Bitmap(GetCurrentWallpaperPath());
}
else
{
pic.BackColor = Color.White;
pic.BackgroundImage = null;
}
}
}
}
private void minimise_to_tray_Click(object sender, EventArgs e)
{
//Places the program in the tray icons
Visible = false;
notifyIcon1.Visible = true;
ShowInTaskbar = false;
}
private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
{
//Restores the program back to a normal window
WindowState = FormWindowState.Normal;
ShowInTaskbar = true;
notifyIcon1.Visible = false;
Show();
}
private void minimise_to_tray_MouseClick(object sender, MouseEventArgs e)
{
}
private void notifyIcon1_MouseClick(object sender, MouseEventArgs e)
{
//Restores the program back to a normal window
WindowState = FormWindowState.Normal;
ShowInTaskbar = true;
notifyIcon1.Visible = false;
Visible = true;
Activate();
}
// External window stuff.
public void ListProcesses()
{
//Get all other parent window processes ie. those which are displayed to the user and not hidden.
Process[] processes = Process.GetProcesses();
Processes.Clear();
var current = Process.GetCurrentProcess();
foreach (Process p in processes)
{
//Ensure the window has a title and is not the current program
if (!string.IsNullOrEmpty(p.MainWindowTitle) && p.MainWindowTitle != current.MainWindowTitle)
{
Processes[p.Id] = p;
}
}
//Clear old processes from the combobox
processCombo.Items.Clear();
//Format each process and display the process id and it's title
var windows = Processes.OrderBy(x => x.Key).Select(x => $"{x.Key} - {x.Value.MainWindowTitle}");
foreach (var window in windows)
{
processCombo.Items.Add(window);
}
}
public Dictionary<int, Process> Processes = new Dictionary<int, Process>();
private void loadProc_Click(object sender, EventArgs e)
{
ListProcesses();
}
private void processCombo_SelectedIndexChanged(object sender, EventArgs e)
{
}
//Format: Process ID - process info.
public Dictionary<int, MovedProcess> MovedProcesses = new Dictionary<int, MovedProcess>();
public class MovedProcess
{
//This is an intermittent class used to hold information about processes moved below the desktop icons
public MovedProcess(IntPtr originalParent, Process proc)
{
OriginalParent = originalParent;
Process = proc;
}
//Store the original parent of the process so it can be restored later
public IntPtr OriginalParent;
public Process Process;
}
private void moveToDesktop_Click(object sender, EventArgs e)
{
var line = processCombo.SelectedItem;
if (line == null) return;
if (line is string val)
{
//Parse the combobox item for it's process id.
var id = val.Split(' ').First();
int idValue = int.Parse(id);
var process = Processes.FirstOrDefault(x => x.Key == idValue);
if (process.Value == null)
{
//Show error if program was closed after being added to list but prior to being added to desktop.
MessageBox.Show("Program with the given ID was not found.");
return;
}
if (MovedProcesses.ContainsKey(idValue))
{
//Ensure the process is not already on the desktop.
MessageBox.Show("Program is already below the desktop.");
return;
}
//Set the processes parent to be the workerW process
var originalParent = Natives.SetParent(process.Value.MainWindowHandle, GetHandle());
belowProcesses.Items.Add(val);
MovedProcesses.Add(idValue, new MovedProcess(originalParent, process.Value));
}
}
public static void bringToFront(IntPtr handle)
{
if (handle == IntPtr.Zero)
{
return;
}
Natives.SetForegroundWindow(handle);
}
private void moveForeground_Click(object sender, EventArgs e)
{
var line = belowProcesses.SelectedItem;
if (line == null) return;
if (line is string val)
{
//Parse for the process id.
var id = val.Split(' ').First();
int idValue = int.Parse(id);
if (MovedProcesses.TryGetValue(idValue, out var processMatch))
{
//Reset the process parent, moving it back above the desktop.
Natives.SetParent(processMatch.Process.MainWindowHandle, processMatch.OriginalParent);
//Focus the window
bringToFront(processMatch.Process.MainWindowHandle);
//Reset wallpaper and remove stored process info.
belowProcesses.Items.Remove(val);
SetImage();
MovedProcesses.Remove(idValue);
}
}
}
public void RestoreMovedProcesses()
{
//Iterate through each moved process and restore it.
foreach (var pair in MovedProcesses)
{
Natives.SetParent(pair.Value.Process.MainWindowHandle, pair.Value.OriginalParent);
bringToFront(pair.Value.Process.MainWindowHandle);
}
//Clear stored info and reset wallpaper
MovedProcesses.Clear();
belowProcesses.Items.Clear();
SetImage();
}
private void PassiveWallpaper_Load(object sender, EventArgs e)
{
}
}
}