-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.cs
330 lines (286 loc) · 13 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
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Text.Json;
using static Neodymium.WindowScreenInfo;
namespace Neodymium
{
public partial class Main : Form
{
[DllImport("user32.dll")]
public static extern bool RegisterHotKey(IntPtr hWnd, int id, int fsModifiers, int vlc);
[DllImport("user32.dll")]
public static extern bool UnregisterHotKey(IntPtr hWnd, int id);
[DllImport("user32.dll")]
static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll", SetLastError = true)]
static extern bool MoveWindow(IntPtr hWnd, int X, int Y, int Width, int Height, bool Repaint);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool GetWindowPlacement(IntPtr hWnd, ref WINDOWPLACEMENT lpwndpl);
[DllImport("user32.dll")]
private static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
[Serializable]
[StructLayout(LayoutKind.Sequential)]
private struct WINDOWPLACEMENT
{
public int length;
public int flags;
public int showCmd;
public System.Drawing.Point ptMinPosition;
public System.Drawing.Point ptMaxPosition;
public System.Drawing.Rectangle rcNormalPosition;
}
public static void DeMaximizeActiveWindow(nint hWnd)
{
if (hWnd != IntPtr.Zero)
{
WINDOWPLACEMENT placement = new WINDOWPLACEMENT();
placement.length = Marshal.SizeOf(placement);
if (GetWindowPlacement(hWnd, ref placement))
{
if (placement.showCmd == SW_SHOWMAXIMIZED)
{
ShowWindow(hWnd, SW_SHOWNORMAL);
}
}
}
}
WindowScreenInfo windowScreenInfo = new WindowScreenInfo();
private const int SW_SHOWNORMAL = 1;
private const int SW_SHOWMINIMIZED = 2;
private const int SW_SHOWMAXIMIZED = 3;
private Settings CurrentSettings;
private string ConfigPath;
public class Settings
{
// Define default modifier and keys for each shortcut
public bool ModifierAlt { get; set; } = true;
public bool ModifierCtrl { get; set; } = true;
public bool ModifierShift { get; set; } = false;
public bool ModifierWin { get; set; } = true;
public UInt16 ResizeStep { get; set; } = 50;
public int TopLeft { get; set; } = (int)Keys.U;
public int TopRight { get; set; } = (int)Keys.I;
public int BottomLeft { get; set; } = (int)Keys.J;
public int BottomRight { get; set; } = (int)Keys.K;
public int Left { get; set; } = (int)Keys.Left;
public int Right { get; set; } = (int)Keys.Right;
public int Top { get; set; } = (int)Keys.Up;
public int Bottom { get; set; } = (int)Keys.Down;
public int Center { get; set; } = (int)Keys.C;
public int Full { get; set; } = (int)Keys.Enter;
public int Wider { get; set; } = (int)Keys.W;
public int Taller { get; set; } = (int)Keys.E;
public int Narrower { get; set; } = (int)Keys.S;
public int Shorter { get; set; } = (int)Keys.D;
public int NextDisplay { get; set; } = (int)Keys.X;
}
enum Shortcuts
{
TOP_LEFT, TOP_RIGHT, BOTTOM_LEFT, BOTTOM_RIGHT, LEFT, RIGHT, CENTER, TOP, BOTTOM, FULL, WIDER, TALLER, NARROWER, SHORTER, NEXT_DISPLAY
}
public Main()
{
InitializeComponent();
string homePath = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
ConfigPath = Path.Combine(homePath, ".neodymium.json");
var fromConfig = HotkeysFromConfig();
if (fromConfig != null)
{
CurrentSettings = fromConfig;
}
else
{
CurrentSettings = new Settings();
string jsonString = JsonSerializer.Serialize(CurrentSettings, new JsonSerializerOptions
{
WriteIndented = true
});
File.WriteAllText(ConfigPath, jsonString);
}
}
private Settings? HotkeysFromConfig()
{
if (File.Exists(ConfigPath))
{
try
{
string jsonString = File.ReadAllText(ConfigPath);
return JsonSerializer.Deserialize<Settings>(jsonString);
}
catch { return null; }
}
return null;
}
private async void InitializeMinimizeAfterDelay()
{
await Task.Delay(2000);
this.WindowState = FormWindowState.Minimized;
this.appStatusLabel.Text = "Running!\nYou can minimize me now!";
}
private void Main_Load(object sender, EventArgs e)
{
RegisterHotkeys();
InitializeMinimizeAfterDelay();
}
private void RegisterHotkeys()
{
// Alt = 1, Ctrl = 2, Shift = 4, Win = 8
int modifier = CurrentSettings.ModifierAlt ? 1 : 0;
modifier = CurrentSettings.ModifierCtrl ? modifier + 2 : modifier;
modifier = CurrentSettings.ModifierShift ? modifier + 4 : modifier;
modifier = CurrentSettings.ModifierWin ? modifier + 8 : modifier;
RegisterHotKey(this.Handle, (int)Shortcuts.TOP_LEFT, modifier, CurrentSettings.TopLeft);
RegisterHotKey(this.Handle, (int)Shortcuts.TOP_RIGHT, modifier, CurrentSettings.TopRight);
RegisterHotKey(this.Handle, (int)Shortcuts.BOTTOM_LEFT, modifier, CurrentSettings.BottomLeft);
RegisterHotKey(this.Handle, (int)Shortcuts.BOTTOM_RIGHT, modifier, CurrentSettings.BottomRight);
RegisterHotKey(this.Handle, (int)Shortcuts.LEFT, modifier, CurrentSettings.Left);
RegisterHotKey(this.Handle, (int)Shortcuts.RIGHT, modifier, CurrentSettings.Right);
RegisterHotKey(this.Handle, (int)Shortcuts.TOP, modifier, CurrentSettings.Top);
RegisterHotKey(this.Handle, (int)Shortcuts.BOTTOM, modifier, CurrentSettings.Bottom);
RegisterHotKey(this.Handle, (int)Shortcuts.CENTER, modifier, CurrentSettings.Center);
RegisterHotKey(this.Handle, (int)Shortcuts.FULL, modifier, CurrentSettings.Full);
RegisterHotKey(this.Handle, (int)Shortcuts.WIDER, modifier, CurrentSettings.Wider);
RegisterHotKey(this.Handle, (int)Shortcuts.TALLER, modifier, CurrentSettings.Taller);
RegisterHotKey(this.Handle, (int)Shortcuts.NARROWER, modifier, CurrentSettings.Narrower);
RegisterHotKey(this.Handle, (int)Shortcuts.SHORTER, modifier, CurrentSettings.Shorter);
RegisterHotKey(this.Handle, (int)Shortcuts.NEXT_DISPLAY, modifier, CurrentSettings.NextDisplay);
}
protected override void WndProc(ref Message m)
{
if (m.Msg == 0x0312)
{
DoResizeWindow((Shortcuts)m.WParam.ToInt32());
}
base.WndProc(ref m);
}
private Screen NextScreen(Screen screen)
{
Screen[] allScreens = Screen.AllScreens;
int currentIndex = Array.IndexOf(allScreens, screen);
if (currentIndex == -1)
{
throw new ArgumentException("The provided screen is not part of the AllScreens collection.");
}
int nextIndex = (currentIndex + 1) % allScreens.Length;
return allScreens[nextIndex];
}
private void DoResizeWindow(Shortcuts s)
{
IntPtr handle = GetForegroundWindow();
ScreenInfo info = windowScreenInfo.GetScreenInfo(handle);
int width = info.screen.WorkingArea.Width;
int height = info.screen.WorkingArea.Height;
int halfWidth = width / 2;
int halfHeight = height / 2;
int X = info.screen.WorkingArea.X;
int Y = info.screen.WorkingArea.Y;
int inc = CurrentSettings.ResizeStep;
DeMaximizeActiveWindow(handle);
switch (s)
{
case Shortcuts.TOP_LEFT:
MoveWindow(handle, X, Y, halfWidth, halfHeight, true);
break;
case Shortcuts.TOP_RIGHT:
MoveWindow(handle, X + halfWidth, Y, halfWidth, halfHeight, true);
break;
case Shortcuts.BOTTOM_LEFT:
MoveWindow(handle, X, Y + halfHeight, halfWidth, halfHeight, true);
break;
case Shortcuts.BOTTOM_RIGHT:
MoveWindow(handle, X + halfWidth, Y + halfHeight, halfWidth, halfHeight, true);
break;
case Shortcuts.LEFT:
MoveWindow(handle, X, Y, halfWidth, height, true);
break;
case Shortcuts.RIGHT:
MoveWindow(handle, X + halfWidth, Y, halfWidth, height, true);
break;
case Shortcuts.TOP:
MoveWindow(handle, X, Y, width, halfHeight, true);
break;
case Shortcuts.BOTTOM:
MoveWindow(handle, X, Y + halfHeight, width, halfHeight, true);
break;
case Shortcuts.CENTER:
MoveWindow(handle, (X + halfWidth) - (info.window.Width / 2), (Y + halfHeight) - (info.window.Height / 2), info.window.Width, info.window.Height, true);
break;
case Shortcuts.FULL:
MoveWindow(handle, X, Y, width, height, true);
break;
case Shortcuts.WIDER:
MakeWider(handle, info, width, inc);
break;
case Shortcuts.TALLER:
MoveWindow(handle, info.window.Left, info.window.Top - (inc / 2), info.window.Width, info.window.Height + inc, true);
break;
case Shortcuts.NARROWER:
MoveWindow(handle, info.window.Left + (inc / 2), info.window.Top, info.window.Width - inc, info.window.Height, true);
break;
case Shortcuts.SHORTER:
MoveWindow(handle, info.window.Left, info.window.Top + (inc / 2), info.window.Width, info.window.Height - inc, true);
break;
case Shortcuts.NEXT_DISPLAY:
NextDisplay(handle, info);
break;
default:
break;
}
}
private void NextDisplay(nint handle, ScreenInfo info)
{
if (Screen.AllScreens.Length == 1)
{
return; // Single screen, nowhere to move the window
}
var next = NextScreen(info.screen);
MoveWindow(handle, next.WorkingArea.X, next.WorkingArea.Y, info.window.Width, info.window.Height, true);
DoResizeWindow(Shortcuts.TOP_LEFT);
DoResizeWindow(Shortcuts.CENTER);
}
private static void MakeWider(nint handle, ScreenInfo info, int width, int inc)
{
var newX = (info.window.Left - (inc / 2) >= info.screen.WorkingArea.X ? info.window.Left - (inc / 2) : info.screen.WorkingArea.Left);
var newWidth = info.window.Width + inc;
if (info.window.Right + inc > width)
{
newWidth = width - info.window.Left + (inc / 2);
}
MoveWindow(handle, newX, info.window.Top, newWidth, info.window.Height, true);
}
private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
{
this.Show();
this.WindowState = FormWindowState.Normal;
}
private void Form1_Resize(object sender, EventArgs e)
{
if (FormWindowState.Minimized == this.WindowState)
{
notifyIcon1.Visible = true;
this.Hide();
}
else if (FormWindowState.Normal == this.WindowState)
{
notifyIcon1.Visible = false;
}
}
private void OpenLink(string link)
{
Process.Start("explorer.exe", link);
}
private void openConfig_Click(object sender, EventArgs e)
{
Process.Start(new ProcessStartInfo("explorer.exe", " /select, " + ConfigPath));
}
private void keyReference_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
OpenLink("https://learn.microsoft.com/en-us/windows/win32/inputdev/virtual-key-codes");
}
private void gitHub_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
OpenLink("https://github.com/richardstefun/Neodymium");
}
}
}