-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathYeeter.cs
146 lines (127 loc) · 4.75 KB
/
Yeeter.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
using Microsoft.Toolkit.Uwp.Notifications;
using System.Diagnostics;
using System.Globalization;
using System.Runtime.InteropServices;
using static tray_yeeter_sharp.HotkeysLoad;
namespace tray_yeeter_sharp
{
public partial class Yeeter : Form
{
public Yeeter()
{
Load += Yeeter_Load;
InitializeComponent();
}
private void Yeeter_Load(object? sender, EventArgs e)
{
notifyIcon1.ContextMenuStrip = new ContextMenuStrip();
notifyIcon1.ContextMenuStrip.Items.Add("Reload", null, ReloadEvent);
notifyIcon1.ContextMenuStrip.Items.Add("Quit", null, QuitEvent);
notifyIcon1.ContextMenuStrip.Items.Add("Quit all", null, QuitAll);
RegisterHotkeys(HotkeyEvent);
}
private static void ReloadEvent(object? sender, EventArgs e)
{
RegisterHotkeys(HotkeyEvent);
}
private static void QuitEvent(object? sender, EventArgs e)
{
if (yeetedWindows.Count > 0)
{
DialogResult result = MessageBox.Show(
"Some yeeted windows are still... yeeted. Do you want to unyeet it? (This program can't recover yeeted windows from previous session)",
"Tray Yeeter",
MessageBoxButtons.YesNoCancel,
MessageBoxIcon.Warning);
if (result == DialogResult.Yes)
{
foreach (IntPtr hWnd in yeetedWindows)
{
try { ShowWindow(hWnd, SW_SHOW); } catch { }
}
}
if (result == DialogResult.Cancel)
{
return;
}
}
Application.Exit();
}
private static void QuitAll(object? sender, EventArgs e)
{
if (yeetedWindows.Count > 0) {
foreach (IntPtr hWnd in yeetedWindows)
{
try {
GetWindowThreadProcessId(hWnd, out uint processId);
if (processId == 0) continue;
Process process = Process.GetProcessById((int)processId);
process.Kill();
} catch { }
}
}
Application.Exit();
}
private static void HotkeyEvent(int Id)
{
if (Id == 0)
{
IntPtr hWnd = GetForegroundWindow();
try { ShowWindow(hWnd, SW_HIDE); } catch { }
yeetedWindows.Add(hWnd);
}
if (Id == 1)
{
if (yeetedWindows.Count > 0)
{
try { ShowWindow(yeetedWindows[^1], SW_SHOW); } catch { }
yeetedWindows.RemoveAt(yeetedWindows.Count - 1);
}
}
if (Id == 2)
{
EnumWindows(new EnumWindowsProc(EnumWindowCallback), IntPtr.Zero);
yeetedWindows.AddRange(enumWindowsResult);
foreach (IntPtr hWnd in enumWindowsResult)
{
try { ShowWindow(hWnd, SW_HIDE); } catch { }
}
enumWindowsResult.Clear();
}
if (Id == 3)
{
foreach (IntPtr hWnd in yeetedWindows)
{
try { ShowWindow(hWnd, SW_SHOW); } catch { }
}
yeetedWindows.Clear();
}
}
private static bool EnumWindowCallback(IntPtr hWnd, IntPtr lParam)
{
if (IsWindowVisible(hWnd))
{
enumWindowsResult.Add(hWnd);
}
return true;
}
private static readonly List<IntPtr> yeetedWindows = [];
private static readonly List<IntPtr> enumWindowsResult = [];
const int SW_HIDE = 0;
const int SW_SHOW = 5;
[LibraryImport("user32.dll")]
private static partial IntPtr GetForegroundWindow();
[LibraryImport("user32.dll")]
private static partial uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId);
[LibraryImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
private static partial bool ShowWindow(IntPtr hWnd, int nCmdShow);
private delegate bool EnumWindowsProc(IntPtr hWnd, IntPtr lParam);
[LibraryImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
private static partial bool EnumWindows(EnumWindowsProc lpEnumFunc, IntPtr lParam);
[LibraryImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
private static partial bool IsWindowVisible(IntPtr hWnd);
}
}