/
XMouseSetting.cs
218 lines (202 loc) · 7.27 KB
/
XMouseSetting.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
// [X-Mouse setting tool on Windows 7/Vista/XP with .NET 3.5]
// build:
// csc.exe /t:winexe XMouseSetting.cs /r:PresentationFramework.dll /r:PresentationCore.dll /r:WindowsBase.dll /win32icon:icon.ico
namespace Bellbind.XMouseSetting {
using Microsoft.Win32;
using System;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Documents;
public struct Info {
public bool FollowActivation;
public bool AutoRaise;
public int? RaiseTime;
}
public class XMouseSetting {
// library functions
public static Info GetXMouse() {
using (var regkey = Registry.CurrentUser.OpenSubKey(@"Control Panel\Desktop")) {
var masks = regkey.GetValue("UserPreferencesMask") as byte[];
return new Info {
FollowActivation = (masks[0] & 0x01) != 0,
AutoRaise = (masks[0] & 0x40) != 0,
RaiseTime = regkey.GetValue("ActiveWndTrkTimeout") as int?,
};
}
}
public static void SetXMouse(Info info) {
SetXMouse(info.FollowActivation, info.AutoRaise, info.RaiseTime ?? 0);
}
public static void SetXMouse(bool followActivation, bool autoRaise, int raiseTime) {
using (var regkey = Registry.CurrentUser.OpenSubKey(@"Control Panel\Desktop", true)) {
var masks = regkey.GetValue("UserPreferencesMask") as byte[];
masks[0] = (byte) (followActivation ? masks[0] | 0x01 : masks[0] & ~0x01);
masks[0] = (byte) (followActivation && autoRaise ? masks[0] | 0x40 : masks[0] & ~0x40);
regkey.SetValue("UserPreferencesMask", masks);
if (followActivation && autoRaise) {
regkey.SetValue("ActiveWndTrkTimeout", raiseTime);
} else {
regkey.DeleteValue("ActiveWndTrkTimeout", false);
}
}
}
// application implementation
[STAThread]
static void Main(string[] args) {
if (args.Length == 0) {
LaunchUI();
return;
}
if (!(args[0] == "on" || args[0] == "off")) {
ShowHelp();
Console.WriteLine();
ShowXMouse();
return;
}
var followActivation = false;
var autoRaise = false;
var raiseTime = 0;
if (args[0] == "on") {
followActivation = true;
if (args.Length == 2) {
try {
raiseTime = int.Parse(args[1]);
autoRaise = true;
} catch (FormatException ex) {
Console.WriteLine(ex);
ShowHelp();
ShowXMouse();
return;
}
}
}
ShowXMouse();
Console.WriteLine();
PrintNewXMouse(followActivation, autoRaise, raiseTime);
SetXMouse(followActivation, autoRaise, raiseTime);
Console.WriteLine();
Console.WriteLine("To enable new settings, you must re-login.");
}
static void ShowHelp() {
Console.WriteLine("Usage: XMouseSetting [on [NUM]|off|help]");
Console.WriteLine("Options: ");
Console.WriteLine(" <nothing>: launch GUI window");
Console.WriteLine(" on: follow mouse activation");
Console.WriteLine(" on NUM: follow activation then auto raise after NUM msec");
Console.WriteLine(" off: disable follow activation/auto raise");
Console.WriteLine(" help: print help and current settings");
}
static void ShowXMouse() {
var info = GetXMouse();
Console.WriteLine("Current XMouse Settings:");
PrintXMouse(info.FollowActivation, info.AutoRaise, info.RaiseTime);
}
static void PrintNewXMouse(bool followActivation, bool autoRaise, int raiseTime) {
Console.WriteLine("New XMouse Settings:");
PrintXMouse(followActivation, autoRaise, raiseTime);
}
static void PrintXMouse(bool followActivation, bool autoRaise, int? raiseTime) {
Console.WriteLine(" Follow Activation: {0}", followActivation);
Console.WriteLine(" Auto Raise: {0}", autoRaise);
Console.WriteLine(" Raise Time (msec): {0}", raiseTime);
}
// use WPF
static void LaunchUI() {
// widgets
var followActivation = new CheckBox {
Content = new TextBlock(new Run("Activate a window when mouse entered")),
IsChecked = true,
IsEnabled = true,
};
var autoRaise = new CheckBox {
Content = new TextBlock(new Run("Auto raise")),
IsChecked = true,
IsEnabled = true,
};
var raiseTime = new Slider {
Value = 500,
Minimum = 0,
Maximum = 1000,
AutoToolTipPlacement = AutoToolTipPlacement.BottomRight,
IsMoveToPointEnabled = true,
TickFrequency = 10,
TickPlacement = TickPlacement.BottomRight,
IsSnapToTickEnabled = true,
IsEnabled = true,
};
var panelRaiseTime = new DockPanel {
Children = {
new Label {
Content = new TextBlock(new Run("Raise after (msec): ")),
},
raiseTime,
},
};
var save = new Button {
Content = new TextBlock(new Run("save")),
Width = 150,
};
var window = new Window {
Width = 300,
Height = 150,
Opacity = 0.9,
Title = "X-Mouse Settings",
Content = new StackPanel {
Children = {
new GroupBox {
Header = "X-Mouse Settings",
Content = new StackPanel {
Children = {
followActivation,
autoRaise,
panelRaiseTime,
},
},
},
save,
},
},
};
var application = new Application();
// events
followActivation.Checked += (sender, ev) => {
autoRaise.IsEnabled = true;
};
followActivation.Unchecked += (sender, ev) => {
autoRaise.IsEnabled = false;
autoRaise.IsChecked = false;
};
autoRaise.Checked += (sender, ev) => {
panelRaiseTime.IsEnabled = true;
};
autoRaise.Unchecked += (sender, ev) => {
panelRaiseTime.IsEnabled = false;
};
save.Click += (sender, ev) => {
SetXMouse(followActivation.IsChecked ?? false,
autoRaise.IsChecked ?? false,
(int) raiseTime.Value);
var result = MessageBox.Show(
"To enable new settings, you must re-login.\n\nLog-off now?",
"XMouse Settings Updated",
MessageBoxButton.YesNo,
MessageBoxImage.Question);
application.Shutdown();
if (result == MessageBoxResult.Yes) {
ExitWindowsEx(0, 0); // Log-off
}
};
// initialize
var info = GetXMouse();
followActivation.IsChecked = info.FollowActivation;
autoRaise.IsChecked = info.AutoRaise;
raiseTime.Value = info.RaiseTime ?? 0;
application.Run(window);
}
// from: http://www.eggheadcafe.com/tutorials/aspnet/e5ef4e3e-6f42-4b9b-8834-04366ce32c96/net-lock-logoff-reboot.aspx
[DllImport("user32.dll")]
static extern int ExitWindowsEx(int uFlags, int dwReason);
}
}