-
Notifications
You must be signed in to change notification settings - Fork 4
/
Main.cs
138 lines (129 loc) · 4.67 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
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace sethc
{
public partial class Main : Form
{
private const Int32 SPI_SETSTICKYKEYS = 0x003B;
private const Int32 SPIF_UPDATEINIFILE = 0x01;
private const Int32 SPIF_SENDWININICHANGE = 0x02;
private const UInt32 SKF_STICKYKEYSON = 0x00000001;
private const UInt32 SKF_HOTKEYACTIVE = 0x00000004;
private const Int32 WM_SYSCOMMAND = 0x112;
private const Int32 MF_BYPOSITION = 0x400;
private const Int32 MF_SEPARATOR = 0x800;
private const Int32 CTXMENU1 = 1000;
private const Int32 CTXMENU2 = 1001;
private const Int32 CTXMENU3 = 1002;
private const Int32 CTXMENU4 = 1003;
private const Int32 CTXMENU5 = 1004;
[DllImport("user32.dll")]
private static extern IntPtr GetSystemMenu(IntPtr hWnd, bool bRevert);
[DllImport("user32.dll")]
private static extern bool InsertMenu(IntPtr hMenu, Int32 wPosition, Int32 wFlags, Int32 wIDNewItem, string lpNewItem);
[DllImport("user32.dll")]
private static extern bool SystemParametersInfoA(Int32 uiAction, Int32 uiParam, IntPtr pvParam, Int32 fWinIni);
[StructLayout(LayoutKind.Sequential)]
private struct tagSTICKYKEYS
{
public Int32 cbSize;
public UInt32 dwFlags;
}
public Main(string[] args)
{
InitializeComponent();
Localization.SetLanguage(this, (int.Parse(args[0]) - 211) / 10);
}
protected override void WndProc(ref Message msg)
{
if (msg.Msg == WM_SYSCOMMAND) {
try {
switch (msg.WParam.ToInt32()) {
// Actions for context menu
case CTXMENU1:
Process.Start("cmd.exe");
return;
case CTXMENU2:
try {
Process.Start("powershell.exe");
} catch (Exception) {
MessageBox.Show("Cannot start PowerShell.", AppDomain.CurrentDomain.FriendlyName, MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
return;
case CTXMENU3:
Process.Start("explorer.exe");
return;
case CTXMENU4:
Process.Start("control.exe");
return;
case CTXMENU5:
Process.Start("regedit.exe");
return;
default:
break;
}
} catch (Exception error) {
Console.WriteLine($"Cannot open process: {error}");
}
}
base.WndProc(ref msg);
}
private void Main_Load(object sender, EventArgs e)
{
// Insert new items into context menu
IntPtr MenuHandle = GetSystemMenu(this.Handle, false);
InsertMenu(MenuHandle, 5, MF_BYPOSITION | MF_SEPARATOR, 0, string.Empty);
InsertMenu(MenuHandle, 6, MF_BYPOSITION, CTXMENU1, "Open Command Prompt");
InsertMenu(MenuHandle, 7, MF_BYPOSITION, CTXMENU2, "Open PowerShell");
InsertMenu(MenuHandle, 8, MF_BYPOSITION, CTXMENU3, "Open Explorer");
InsertMenu(MenuHandle, 9, MF_BYPOSITION, CTXMENU4, "Open Control Panel");
InsertMenu(MenuHandle, 10, MF_BYPOSITION, CTXMENU5, "Open Registry");
}
private void labeldeactivatedialog_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
// Open settings
try {
Process.Start("ms-settings:easeofaccess-keyboard");
} catch (Exception) {
try {
ProcessStartInfo skwinxp = new ProcessStartInfo("rundll32.exe");
skwinxp.Arguments = "Shell32.dll,Control_RunDLL access.cpl,,1";
Process.Start(skwinxp);
} catch (Exception) {
try {
Process.Start("control.exe");
} catch (Exception error) {
MessageBox.Show($"Cannot open settings from {AppDomain.CurrentDomain.FriendlyName}:\n{error}", AppDomain.CurrentDomain.FriendlyName + " - Cannot open settings", MessageBoxButtons.OK, MessageBoxIcon.Error);
Environment.Exit(126);
}
}
}
Application.Exit();
}
private void buttonNo_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void buttonYes_Click(object sender, EventArgs e)
{
// Try to enable sticky keys
try {
tagSTICKYKEYS stk;
stk.dwFlags = SKF_STICKYKEYSON | SKF_HOTKEYACTIVE;
stk.cbSize = Marshal.SizeOf(typeof(tagSTICKYKEYS));
// Convert struct to IntPtr
IntPtr pObj = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(tagSTICKYKEYS)));
Marshal.StructureToPtr(stk, pObj, false);
// Set Sticky Keys settings
SystemParametersInfoA(SPI_SETSTICKYKEYS, 0, pObj, SPIF_UPDATEINIFILE | SPIF_SENDWININICHANGE);
Marshal.FreeHGlobal(pObj);
} catch (Exception error) {
MessageBox.Show($"Cannot change settings from {AppDomain.CurrentDomain.FriendlyName}: {error}\nThe current user will have to manually change the settings.", AppDomain.CurrentDomain.FriendlyName + " - Cannot change settings", MessageBoxButtons.OK, MessageBoxIcon.Error);
Environment.Exit(126);
}
Application.Exit();
}
}
}