-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInvisibleForm.cs
More file actions
128 lines (110 loc) · 3.9 KB
/
InvisibleForm.cs
File metadata and controls
128 lines (110 loc) · 3.9 KB
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
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using Microsoft.Win32;
namespace ScreenSwitcher
{
public class InvisibleForm : Form
{
// P/Invoke constants
private const int MOD_ALT = 0x0001;
private const int MOD_CONTROL = 0x0002;
private const int MOD_SHIFT = 0x0004;
private const int WM_HOTKEY = 0x0312;
private const int VK_1 = 0x31;
private const int VK_2 = 0x32;
// Hotkey IDs
private const int HOTKEY_ID_1 = 1;
private const int HOTKEY_ID_2 = 2;
[DllImport("user32.dll")]
private static extern bool RegisterHotKey(IntPtr hWnd, int id, int fsModifiers, int vk);
[DllImport("user32.dll")]
private static extern bool UnregisterHotKey(IntPtr hWnd, int id);
public InvisibleForm()
{
// Configure form to be invisible
this.ShowInTaskbar = false;
this.WindowState = FormWindowState.Minimized;
this.Load += InvisibleForm_Load;
// Check and set startup
SetStartup();
}
protected override bool ShowWithoutActivation => true;
private void InvisibleForm_Load(object sender, EventArgs e)
{
// Hide the form completely
this.Hide();
// Register hotkeys
// Shift + Ctrl + 1
RegisterHotKey(this.Handle, HOTKEY_ID_1, MOD_CONTROL | MOD_SHIFT, VK_1);
// Shift + Ctrl + 2
RegisterHotKey(this.Handle, HOTKEY_ID_2, MOD_CONTROL | MOD_SHIFT, VK_2);
}
protected override void WndProc(ref Message m)
{
base.WndProc(ref m);
if (m.Msg == WM_HOTKEY)
{
int id = m.WParam.ToInt32();
switch (id)
{
case HOTKEY_ID_1:
SwitchScreen("1"); // PC Screen Only
break;
case HOTKEY_ID_2:
SwitchScreen("4"); // Second Screen Only
break;
}
}
}
private void SwitchScreen(string mode)
{
try
{
// If switching to second screen (mode "4"), attempt to wake the TV
if (mode == "4")
{
var config = AppConfig.Instance;
if (config.EnableTvWake)
{
// Fire and forget the wake command to keep the UI responsive
_ = WakeOnLan.WakeAsync(config.TvMacAddress);
}
}
Process.Start("DisplaySwitch.exe", mode);
}
catch (Exception ex)
{
MessageBox.Show($"Error switching screen: {ex.Message}");
}
}
private void SetStartup()
{
try
{
string runKey = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Run";
using (RegistryKey key = Registry.CurrentUser.OpenSubKey(runKey, true))
{
if (key == null) return;
string appName = "ScreenSwitcher";
string appPath = Application.ExecutablePath;
if (key.GetValue(appName) as string != appPath)
{
key.SetValue(appName, appPath);
}
}
}
catch
{
// Ignore errors (e.g. permissions)
}
}
protected override void OnFormClosing(FormClosingEventArgs e)
{
UnregisterHotKey(this.Handle, HOTKEY_ID_1);
UnregisterHotKey(this.Handle, HOTKEY_ID_2);
base.OnFormClosing(e);
}
}
}