-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainWindow.xaml.cs
336 lines (321 loc) · 13 KB
/
MainWindow.xaml.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
331
332
333
334
335
336
using System.ComponentModel;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows;
using System.Windows.Input;
using System.Windows.Interop;
namespace BossKey
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private delegate bool WndEnumProc(IntPtr hwnd, IntPtr lParam);
[LibraryImport("user32.dll")]
private static partial int EnumWindows(WndEnumProc lpEnumFunc, IntPtr lParam);
[DllImport("user32.dll")]
private static extern int GetWindowText(IntPtr hWnd, StringBuilder title, int size);
[LibraryImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
private static partial bool IsWindowVisible(IntPtr hWnd);
[LibraryImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
private static partial bool IsWindow(IntPtr hWnd);
[LibraryImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
private static partial bool IsWindowEnabled(IntPtr hWnd);
[DllImport("user32.dll")]
private static extern int GetWindowTextLength(IntPtr hWnd);
[LibraryImport("user32.dll")]
private static partial int GetWindowThreadProcessId(IntPtr hWnd, out int lpdwProcessId);
[DllImport("user32.dll")]
private static extern uint MapVirtualKey(uint uCode, uint uMapType);
// 注册全局热键的常量
private const int MOD_ALT = 0x0001;
private const int MOD_CONTROL = 0x0002;
private const int MOD_SHIFT = 0x0004;
private const int MOD_WIN = 0x0008;
//private const int WM_GETTRAYWND = 0x0000;
//private const int WM_GETTRAYPOS = 0x0005;
//private const int TB_BUTTONCOUNT = 0x0418;
//private const int TB_GETBUTTON = 0x417;
private int _hotKeyId = 0;
private IntPtr Handle= IntPtr.Zero;
private SortDescription _sortDescriptionByVisible;
private SortDescription _sortDescriptionByName;
// 按压检测热键时
private bool _isTextBoxAltPressed = false;
private bool _isTextBoxCtrlPressed = false;
private bool _isTextBoxShiftPressed = false;
private bool _isTextBoxWinPressed = false;
private HotkeyWindowToggler? _hotkeyWindowToggler = null;
private MouseWindowToggler? _mouseWindowToggler = null;
public MainWindow()
{
InitializeComponent();
_sortDescriptionByVisible = new SortDescription("Visible", ListSortDirection.Descending);
_sortDescriptionByName = new SortDescription("Name", ListSortDirection.Ascending);
}
private bool EnumWindowsProc(IntPtr hwnd, IntPtr lParam)
{
if (!IsWindow(hwnd) || !IsWindowEnabled(hwnd)) { return true;}
int cTextLen = GetWindowTextLength(hwnd);
string Title;
if (cTextLen != 0)
{
StringBuilder text = new(cTextLen + 1);
_ = GetWindowText(hwnd, text, cTextLen + 1);
Title = text.ToString();
}
else return true;
bool visible;
string visibleText;
if (IsWindowVisible(hwnd))
{
visible = true;
visibleText= "✔"; //✓✔
}
else
{
visible = false;
visibleText = "✘"; //✗✘
}
_ = GetWindowThreadProcessId(hwnd, out int pid);
string filePath;
try
{
filePath = Process.GetProcessById(pid).MainModule?.FileName ?? "无法获取";
}
catch (System.ComponentModel.Win32Exception)
{
filePath = "没有权限";
}
WindowListView.Items.Add(new WindowInfo { Hwnd = hwnd, Name = Title, Visible = visible, FilePath = filePath, Pid = pid, VisibleText=visibleText });
return true;
}
private void RefreshWindowListView()
{
WindowListView.Items.Clear();
_ = EnumWindows(EnumWindowsProc, IntPtr.Zero);
WindowListView.Items.SortDescriptions.Add(_sortDescriptionByVisible);
//WindowsListView.Items.SortDescriptions.Add(_sortDescriptionByName);
}
private void RefreshWindowListViewButton_Click(object sender, RoutedEventArgs e) => RefreshWindowListView();
private void AddHotKeyButton_Click(object sender, RoutedEventArgs e)
{
if (WindowListView.SelectedItems.Count == 0) { return; }
string[] keys = HotKeyTextBox.Text.Split('+');
uint fsModifiers = 0;
uint vk = 0;
foreach (string key in keys)
{
switch (key)
{
case "Alt":
fsModifiers |= MOD_ALT;
break;
case "Ctrl":
fsModifiers |= MOD_CONTROL;
break;
case "Shift":
fsModifiers |= MOD_SHIFT;
break;
case "Win":
fsModifiers |= MOD_WIN;
break;
default:
uint scanCode = MapVirtualKey(Convert.ToUInt32(key[0]), 0); // 将字符串的第一个字符转换为对应的扫描码
vk = MapVirtualKey(scanCode, 1); // 将扫描码转换为虚拟键值
break;
}
}
StringBuilder text = new();
if (_hotkeyWindowToggler != null)
{
_hotkeyWindowToggler.Dispose();
_hotkeyWindowToggler = null;
}
_hotkeyWindowToggler = new(Handle, _hotKeyId, fsModifiers, vk);
foreach (var item in WindowListView.SelectedItems)
{
if (item is WindowInfo windowInfo)
{
_hotkeyWindowToggler.HWNDList.Add(windowInfo.Hwnd);
text.Append(windowInfo.Hwnd);
text.Append(':');
text.Append(windowInfo.Name);
text.Append("; ");
}
}
if (!_hotkeyWindowToggler.Register())
{
MessageBox.Show("注册热键失败!不能添加重复的热键");
_hotkeyWindowToggler?.Dispose();
_hotkeyWindowToggler = null;
HotKekWindowListTextBlock.Text = "";
return;
}
HotKekWindowListTextBlock.Text = text.ToString();
}
private void DelHotKeyButton_Click(object sender, RoutedEventArgs e)
{
_hotkeyWindowToggler?.Dispose();
_hotkeyWindowToggler = null;
HotKekWindowListTextBlock.Text = "";
}
private void AddMouseButton_Click(object sender, RoutedEventArgs e)
{
int selectedIndex = MouseComboBox.SelectedIndex;
StringBuilder text = new();
if (_mouseWindowToggler != null)
{
_mouseWindowToggler.Dispose();
_mouseWindowToggler = null;
}
if (selectedIndex == 0)
{
_mouseWindowToggler = new(MouseWindowToggler.Mode.ToggleOnMiddleClick);
}
else if(selectedIndex == 1)
{
_mouseWindowToggler = new(MouseWindowToggler.Mode.ToggleOnSimultaneousLeftRightClick);
}
else if (selectedIndex == 2)
{
_mouseWindowToggler = new(MouseWindowToggler.Mode.ToggleOnCursorInWindow);
}
else
{
MessageBox.Show("错误,没有对应的模式");
return;
}
foreach (var item in WindowListView.SelectedItems)
{
if (item is WindowInfo windowInfo)
{
if (selectedIndex == 2)
_mouseWindowToggler.HWNDList_CurserInWindow.Add((windowInfo.Hwnd, false));
else
_mouseWindowToggler.HWNDList.Add(windowInfo.Hwnd);
text.Append(windowInfo.Hwnd);
text.Append(':');
text.Append(windowInfo.Name);
text.Append("; ");
}
}
if (!_mouseWindowToggler.SetHook())
{
MessageBox.Show("注册鼠标钩子失败!");
_mouseWindowToggler?.Dispose();
_mouseWindowToggler = null;
MouseWindowListTextBlock.Text = "";
return;
}
MouseWindowListTextBlock.Text = text.ToString();
}
private void DelMouseButton_Click(object sender, RoutedEventArgs e)
{
_mouseWindowToggler?.Dispose();
_mouseWindowToggler = null;
MouseWindowListTextBlock.Text = "";
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
Handle = new WindowInteropHelper(this).Handle;
RefreshWindowListView();
HotkeyWindowToggler mainWindowHotkey = new(Handle, 0xBFFF, MOD_ALT, (uint)KeyInterop.VirtualKeyFromKey(Key.P));
mainWindowHotkey.HWNDList.Add(Handle);
mainWindowHotkey.Register();
}
class WindowInfo
{
public bool Visible { set; get; }
public string VisibleText { set; get; } = string.Empty;
public string Name { set; get; } = string.Empty;
public IntPtr Hwnd { set; get; }
public IntPtr Pid { set; get; }
public string FilePath { set; get; } = string.Empty;
}
private void HotKeyTextBox_PreviewKeyDown(object sender, KeyEventArgs e)
{
Key pressedKey;
// 按住alt时会变成Key.System
if (e.Key == Key.System) { pressedKey = e.SystemKey; }
else { pressedKey = e.Key; }
if ((pressedKey >= Key.A && pressedKey <= Key.Z) ||
(pressedKey >= Key.NumPad0 && pressedKey <= Key.NumPad9))
{
if (HotKeyTextBox.Text.Length != 0)
{
string convertedKey = pressedKey.ToString().ToUpper();
HotKeyTextBox.Text += "+";
HotKeyTextBox.Text += convertedKey;
AddHotKeyButton.Focus();
}
}
else
{
switch (pressedKey)
{
case Key.LeftAlt:
case Key.RightAlt:
if (_isTextBoxAltPressed)
break;
_isTextBoxAltPressed = true;
if (HotKeyTextBox.Text.Length != 0)
HotKeyTextBox.Text += "+";
HotKeyTextBox.Text += "Alt";
break;
case Key.LeftShift:
case Key.RightShift:
if (_isTextBoxShiftPressed)
break;
_isTextBoxShiftPressed = true;
if (HotKeyTextBox.Text.Length != 0)
HotKeyTextBox.Text += "+";
HotKeyTextBox.Text += "Shift";
break;
case Key.LeftCtrl:
case Key.RightCtrl:
if (_isTextBoxCtrlPressed)
break;
_isTextBoxCtrlPressed = true;
if (HotKeyTextBox.Text.Length != 0)
HotKeyTextBox.Text += "+";
HotKeyTextBox.Text += "Ctrl";
break;
case Key.LWin:
case Key.RWin:
if (_isTextBoxWinPressed)
break;
_isTextBoxWinPressed = true;
if (HotKeyTextBox.Text.Length != 0)
HotKeyTextBox.Text += "+";
HotKeyTextBox.Text += "Win";
break;
}
}
e.Handled = true; //阻止了原始按键事件的继续传播
}
private void HotKeyTextBox_PreviewMouseDown(object sender, MouseButtonEventArgs e)
{
// 切换为英文输入法
InputMethod.SetPreferredImeState(HotKeyTextBox, InputMethodState.Off);
HotKeyTextBox.Clear();
_isTextBoxAltPressed = false;
_isTextBoxCtrlPressed = false;
_isTextBoxShiftPressed = false;
_isTextBoxWinPressed = false;
}
private void OpenAboutWindow_Click(object sender, RoutedEventArgs e)
{
AboutWindow w = new()
{
Owner = this
};
w.Show();
}
}
}