dsh-cursor-whale 0.2.0 → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +87 -17
- package/app/DSH-Hotkey.exe +0 -0
- package/assets/whale-alpha.png +0 -0
- package/build.ps1 +29 -0
- package/package.json +15 -3
- package/src/DshHudHotkey.cs +1309 -0
|
@@ -0,0 +1,1309 @@
|
|
|
1
|
+
// DSH 折叠热键 —— 让 dsh-mini 的小窗能"一键切换 + 跟随鼠标"
|
|
2
|
+
//
|
|
3
|
+
// 托盘图标:双击 = 切换主窗/小窗;右键菜单 = 设置热键 / 打开配置目录 / 退出。
|
|
4
|
+
// 热键与偏移存在同目录 hotkey.ini,改完约 2 秒自动生效,也可在「设置热键…」里改。
|
|
5
|
+
//
|
|
6
|
+
// 命令行(调试用):
|
|
7
|
+
// DSH-Hotkey.exe 常驻
|
|
8
|
+
// DSH-Hotkey.exe --status 打印找到的窗口后退出
|
|
9
|
+
// DSH-Hotkey.exe --toggle 执行一次切换后退出
|
|
10
|
+
// DSH-Hotkey.exe --follow 执行一次跟随后退出
|
|
11
|
+
//
|
|
12
|
+
// 日志在同目录 hotkey.log。
|
|
13
|
+
|
|
14
|
+
using System;
|
|
15
|
+
using System.Collections.Generic;
|
|
16
|
+
using System.Diagnostics;
|
|
17
|
+
using System.Drawing;
|
|
18
|
+
using System.Drawing.Imaging;
|
|
19
|
+
using System.IO;
|
|
20
|
+
using System.Runtime.InteropServices;
|
|
21
|
+
using System.Text;
|
|
22
|
+
using System.Threading;
|
|
23
|
+
using System.Windows.Forms;
|
|
24
|
+
|
|
25
|
+
static class Native
|
|
26
|
+
{
|
|
27
|
+
public const int WM_HOTKEY = 0x0312;
|
|
28
|
+
public const int SW_HIDE = 0;
|
|
29
|
+
public const int SW_SHOW = 5;
|
|
30
|
+
public const int SW_RESTORE = 9;
|
|
31
|
+
public const uint MOD_ALT = 0x0001;
|
|
32
|
+
public const uint MOD_CTRL = 0x0002;
|
|
33
|
+
public const uint MOD_SHIFT = 0x0004;
|
|
34
|
+
public const uint MOD_WIN = 0x0008;
|
|
35
|
+
public const uint MOD_NOREPEAT = 0x4000;
|
|
36
|
+
public static readonly IntPtr HWND_TOPMOST = new IntPtr(-1);
|
|
37
|
+
public const uint SWP_NOSIZE = 0x0001;
|
|
38
|
+
public const uint SWP_SHOWWINDOW = 0x0040;
|
|
39
|
+
|
|
40
|
+
[DllImport("user32.dll")] public static extern bool RegisterHotKey(IntPtr hWnd, int id, uint fsModifiers, uint vk);
|
|
41
|
+
[DllImport("user32.dll")] public static extern bool UnregisterHotKey(IntPtr hWnd, int id);
|
|
42
|
+
[DllImport("user32.dll")] public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
|
|
43
|
+
[DllImport("user32.dll")] public static extern bool SetForegroundWindow(IntPtr hWnd);
|
|
44
|
+
[DllImport("user32.dll")] public static extern IntPtr GetForegroundWindow();
|
|
45
|
+
[DllImport("user32.dll")] public static extern bool IsWindow(IntPtr hWnd);
|
|
46
|
+
[DllImport("user32.dll")] public static extern bool IsWindowVisible(IntPtr hWnd);
|
|
47
|
+
[DllImport("user32.dll")] public static extern int GetSystemMetrics(int index);
|
|
48
|
+
[DllImport("user32.dll")] public static extern int GetWindowLong(IntPtr hWnd, int index);
|
|
49
|
+
[DllImport("user32.dll")] public static extern int SetWindowLong(IntPtr hWnd, int index, int newStyle);
|
|
50
|
+
|
|
51
|
+
public const int SM_CXCURSOR = 13;
|
|
52
|
+
public const int GWL_EXSTYLE = -20;
|
|
53
|
+
public const int WS_EX_LAYERED = 0x00080000;
|
|
54
|
+
public const int WS_EX_TRANSPARENT = 0x00000020;
|
|
55
|
+
public const int WS_EX_TOOLWINDOW = 0x00000080;
|
|
56
|
+
public const int WS_EX_NOACTIVATE = 0x08000000;
|
|
57
|
+
|
|
58
|
+
[StructLayout(LayoutKind.Sequential)] public struct SIZE { public int cx; public int cy; }
|
|
59
|
+
[StructLayout(LayoutKind.Sequential, Pack = 1)] public struct BLENDFUNCTION { public byte BlendOp; public byte BlendFlags; public byte SourceConstantAlpha; public byte AlphaFormat; }
|
|
60
|
+
[StructLayout(LayoutKind.Sequential)]
|
|
61
|
+
public struct BITMAPINFOHEADER
|
|
62
|
+
{
|
|
63
|
+
public uint biSize;
|
|
64
|
+
public int biWidth;
|
|
65
|
+
public int biHeight;
|
|
66
|
+
public ushort biPlanes;
|
|
67
|
+
public ushort biBitCount;
|
|
68
|
+
public uint biCompression;
|
|
69
|
+
public uint biSizeImage;
|
|
70
|
+
public int biXPelsPerMeter;
|
|
71
|
+
public int biYPelsPerMeter;
|
|
72
|
+
public uint biClrUsed;
|
|
73
|
+
public uint biClrImportant;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
[StructLayout(LayoutKind.Sequential)]
|
|
77
|
+
public struct BITMAPINFO
|
|
78
|
+
{
|
|
79
|
+
public BITMAPINFOHEADER bmiHeader;
|
|
80
|
+
public uint bmiColors;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
[DllImport("gdi32.dll")] public static extern IntPtr CreateDIBSection(IntPtr hdc, ref BITMAPINFO pbmi, uint usage, out IntPtr bits, IntPtr section, uint offset);
|
|
84
|
+
|
|
85
|
+
[DllImport("user32.dll")] public static extern bool UpdateLayeredWindow(IntPtr hwnd, IntPtr hdcDst, ref POINT pptDst, ref SIZE psize, IntPtr hdcSrc, ref POINT pprSrc, int crKey, ref BLENDFUNCTION pblend, int dwFlags);
|
|
86
|
+
[DllImport("gdi32.dll")] public static extern IntPtr CreateCompatibleDC(IntPtr hdc);
|
|
87
|
+
[DllImport("gdi32.dll")] public static extern IntPtr SelectObject(IntPtr hdc, IntPtr obj);
|
|
88
|
+
[DllImport("gdi32.dll")] public static extern bool DeleteObject(IntPtr obj);
|
|
89
|
+
[DllImport("gdi32.dll")] public static extern bool DeleteDC(IntPtr hdc);
|
|
90
|
+
[DllImport("user32.dll")] public static extern IntPtr GetDC(IntPtr hwnd);
|
|
91
|
+
[DllImport("user32.dll")] public static extern int ReleaseDC(IntPtr hwnd, IntPtr hdc);
|
|
92
|
+
[DllImport("user32.dll")] public static extern bool SetProcessDPIAware();
|
|
93
|
+
[DllImport("user32.dll")] public static extern bool SetProcessDpiAwarenessContext(IntPtr ctx);
|
|
94
|
+
[DllImport("user32.dll")] public static extern bool IsIconic(IntPtr hWnd);
|
|
95
|
+
[DllImport("user32.dll")] public static extern bool GetWindowRect(IntPtr hWnd, out RECT rect);
|
|
96
|
+
[DllImport("user32.dll")] public static extern bool SetWindowPos(IntPtr hWnd, IntPtr insertAfter, int x, int y, int cx, int cy, uint flags);
|
|
97
|
+
[DllImport("user32.dll")] public static extern bool GetCursorPos(out POINT point);
|
|
98
|
+
[DllImport("user32.dll")] public static extern int GetWindowText(IntPtr hWnd, StringBuilder text, int count);
|
|
99
|
+
[DllImport("user32.dll")] public static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint pid);
|
|
100
|
+
|
|
101
|
+
public delegate bool EnumProc(IntPtr hWnd, IntPtr lParam);
|
|
102
|
+
[DllImport("user32.dll")] public static extern bool EnumWindows(EnumProc callback, IntPtr lParam);
|
|
103
|
+
|
|
104
|
+
// ---- 鼠标侧键:RegisterHotKey 只管键盘,鼠标必须走低级钩子 ----
|
|
105
|
+
public const int WH_MOUSE_LL = 14;
|
|
106
|
+
public const int WM_XBUTTONDOWN = 0x020B;
|
|
107
|
+
public const int WM_MBUTTONDOWN = 0x0207;
|
|
108
|
+
public delegate IntPtr MouseProc(int nCode, IntPtr wParam, IntPtr lParam);
|
|
109
|
+
[DllImport("user32.dll", SetLastError = true)] public static extern IntPtr SetWindowsHookEx(int idHook, MouseProc callback, IntPtr hMod, uint threadId);
|
|
110
|
+
[DllImport("user32.dll")] public static extern bool UnhookWindowsHookEx(IntPtr hook);
|
|
111
|
+
[DllImport("user32.dll")] public static extern IntPtr CallNextHookEx(IntPtr hook, int nCode, IntPtr wParam, IntPtr lParam);
|
|
112
|
+
[DllImport("kernel32.dll")] public static extern IntPtr GetModuleHandle(string moduleName);
|
|
113
|
+
|
|
114
|
+
[StructLayout(LayoutKind.Sequential)]
|
|
115
|
+
public struct MSLLHOOKSTRUCT
|
|
116
|
+
{
|
|
117
|
+
public POINT pt;
|
|
118
|
+
public uint mouseData;
|
|
119
|
+
public uint flags;
|
|
120
|
+
public uint time;
|
|
121
|
+
public IntPtr dwExtraInfo;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
// ---- 进程调度:Windows 11 会把"后台无窗口进程"降频(EcoQoS),
|
|
125
|
+
// 表现就是热键收到了、动作要过好几秒才跑。这里显式关掉节流并抬高优先级 ----
|
|
126
|
+
[DllImport("kernel32.dll")] public static extern IntPtr GetCurrentProcess();
|
|
127
|
+
[DllImport("kernel32.dll")] public static extern bool SetPriorityClass(IntPtr hProcess, uint dwPriorityClass);
|
|
128
|
+
[DllImport("kernel32.dll", SetLastError = true)] public static extern bool SetProcessInformation(IntPtr hProcess, int infoClass, ref PROCESS_POWER_THROTTLING_STATE info, uint size);
|
|
129
|
+
|
|
130
|
+
[StructLayout(LayoutKind.Sequential)]
|
|
131
|
+
public struct PROCESS_POWER_THROTTLING_STATE
|
|
132
|
+
{
|
|
133
|
+
public uint Version;
|
|
134
|
+
public uint ControlMask;
|
|
135
|
+
public uint StateMask;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
[StructLayout(LayoutKind.Sequential)]
|
|
139
|
+
public struct RECT { public int Left, Top, Right, Bottom; }
|
|
140
|
+
|
|
141
|
+
[StructLayout(LayoutKind.Sequential)]
|
|
142
|
+
public struct POINT { public int X, Y; }
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
static class Program
|
|
146
|
+
{
|
|
147
|
+
const string MainProcess = "DeepSeek Harness";
|
|
148
|
+
const string MiniProcess = "DSH小窗";
|
|
149
|
+
|
|
150
|
+
const string DefaultToggle = "Alt+`";
|
|
151
|
+
const string DefaultFollow = "Ctrl+Alt+`";
|
|
152
|
+
const string DefaultQuit = "Ctrl+Alt+Shift+Q";
|
|
153
|
+
|
|
154
|
+
static readonly string BaseDir = AppDomain.CurrentDomain.BaseDirectory;
|
|
155
|
+
static readonly string ConfigPath = Path.Combine(BaseDir, "hotkey.ini");
|
|
156
|
+
static readonly string LogPath = Path.Combine(BaseDir, "hotkey.log");
|
|
157
|
+
static readonly object LogLock = new object();
|
|
158
|
+
|
|
159
|
+
static string cfgToggle = DefaultToggle;
|
|
160
|
+
static string cfgFollow = DefaultFollow;
|
|
161
|
+
static string cfgQuit = DefaultQuit;
|
|
162
|
+
static string cfgOpenMini = "XButton2";
|
|
163
|
+
static int cfgOffsetY = 24;
|
|
164
|
+
|
|
165
|
+
static NotifyIcon tray;
|
|
166
|
+
static HotkeyWindow window;
|
|
167
|
+
static Native.MouseProc mouseProc;
|
|
168
|
+
static IntPtr mouseHook = IntPtr.Zero;
|
|
169
|
+
static readonly Dictionary<int, string> mouseActions = new Dictionary<int, string>();
|
|
170
|
+
static bool blockMouseButton = true;
|
|
171
|
+
static Control dispatcher;
|
|
172
|
+
static volatile bool opening;
|
|
173
|
+
|
|
174
|
+
// ---- 跟随鼠标的鲸鱼 ------------------------------------------------
|
|
175
|
+
static Form whaleForm;
|
|
176
|
+
static System.Windows.Forms.Timer followTimer;
|
|
177
|
+
static volatile bool followActive;
|
|
178
|
+
static int whaleScale = 6;
|
|
179
|
+
static int whaleOffsetX = 16;
|
|
180
|
+
static int whaleOffsetY = 16;
|
|
181
|
+
static string whaleImage = "whale.png";
|
|
182
|
+
static Point lastCursor = Point.Empty;
|
|
183
|
+
static bool collapseArmed;
|
|
184
|
+
|
|
185
|
+
static void Post(Action action)
|
|
186
|
+
{
|
|
187
|
+
try
|
|
188
|
+
{
|
|
189
|
+
if (dispatcher != null && dispatcher.IsHandleCreated) dispatcher.BeginInvoke(action);
|
|
190
|
+
else action();
|
|
191
|
+
}
|
|
192
|
+
catch (Exception ex) { Log("post failed: " + ex.Message); }
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
static void Log(string message)
|
|
196
|
+
{
|
|
197
|
+
try
|
|
198
|
+
{
|
|
199
|
+
lock (LogLock)
|
|
200
|
+
File.AppendAllText(LogPath, DateTime.Now.ToString("HH:mm:ss.fff") + " " + message + Environment.NewLine, Encoding.UTF8);
|
|
201
|
+
}
|
|
202
|
+
catch { }
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
// ---------------------------------------------------------------- config
|
|
206
|
+
|
|
207
|
+
static void EnsureConfig()
|
|
208
|
+
{
|
|
209
|
+
try
|
|
210
|
+
{
|
|
211
|
+
if (File.Exists(ConfigPath)) return;
|
|
212
|
+
File.WriteAllText(ConfigPath, BuildConfigText(), Encoding.UTF8);
|
|
213
|
+
Log("wrote default config: " + ConfigPath);
|
|
214
|
+
}
|
|
215
|
+
catch (Exception ex) { Log("config write failed: " + ex.Message); }
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
static string BuildConfigText()
|
|
219
|
+
{
|
|
220
|
+
var text = new StringBuilder();
|
|
221
|
+
text.AppendLine("# DSH 折叠热键配置。改完保存,约 2 秒内自动生效。");
|
|
222
|
+
text.AppendLine("# 写法:修饰键(Ctrl/Alt/Shift/Win,可组合)+ 一个按键,至少带一个修饰键。");
|
|
223
|
+
text.AppendLine("# 按键:A-Z 0-9 F1-F24 ` - = [ ] \\ ; ' , . / Space Enter Esc Tab Delete Home End PageUp PageDown Up Down Left Right PrintScreen");
|
|
224
|
+
text.AppendLine("# 鼠标侧键:写 XButton1 / XButton2(或 Mouse4 / Mouse5 / Middle),不能和键盘修饰键混写。");
|
|
225
|
+
text.AppendLine("# 留空 = 关掉这个热键。");
|
|
226
|
+
text.AppendLine();
|
|
227
|
+
text.AppendLine("toggle = " + cfgToggle);
|
|
228
|
+
text.AppendLine("follow = " + cfgFollow);
|
|
229
|
+
text.AppendLine("quit = " + cfgQuit);
|
|
230
|
+
text.AppendLine("openMini = " + cfgOpenMini);
|
|
231
|
+
text.AppendLine();
|
|
232
|
+
text.AppendLine("# 鼠标侧键被绑定时,是否吞掉它原本的「后退/前进」动作(true = 只给 DSH 用)");
|
|
233
|
+
text.AppendLine("blockMouseButton = " + (blockMouseButton ? "true" : "false"));
|
|
234
|
+
text.AppendLine();
|
|
235
|
+
text.AppendLine("# 跟随鼠标的鲸鱼:宽度 = 系统指针宽度 × whaleScale;位置相对光标右下");
|
|
236
|
+
text.AppendLine("whaleScale = " + whaleScale);
|
|
237
|
+
text.AppendLine("whaleOffsetX = " + whaleOffsetX);
|
|
238
|
+
text.AppendLine("whaleOffsetY = " + whaleOffsetY);
|
|
239
|
+
text.AppendLine("whaleImage = " + whaleImage);
|
|
240
|
+
text.AppendLine();
|
|
241
|
+
text.AppendLine("# 跟随鼠标时窗口中心相对光标的纵向偏移(正数往下)");
|
|
242
|
+
text.AppendLine("followOffsetY = " + cfgOffsetY);
|
|
243
|
+
return text.ToString();
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
static void LoadConfig()
|
|
247
|
+
{
|
|
248
|
+
cfgToggle = DefaultToggle;
|
|
249
|
+
cfgFollow = DefaultFollow;
|
|
250
|
+
cfgQuit = DefaultQuit;
|
|
251
|
+
cfgOpenMini = "XButton2";
|
|
252
|
+
cfgOffsetY = 24;
|
|
253
|
+
try
|
|
254
|
+
{
|
|
255
|
+
if (!File.Exists(ConfigPath)) return;
|
|
256
|
+
foreach (var rawLine in File.ReadAllLines(ConfigPath, Encoding.UTF8))
|
|
257
|
+
{
|
|
258
|
+
var line = rawLine.Trim();
|
|
259
|
+
if (line.Length == 0 || line.StartsWith("#") || line.StartsWith(";")) continue;
|
|
260
|
+
var eq = line.IndexOf('=');
|
|
261
|
+
if (eq <= 0) continue;
|
|
262
|
+
var key = line.Substring(0, eq).Trim().ToLowerInvariant();
|
|
263
|
+
var value = line.Substring(eq + 1).Trim();
|
|
264
|
+
if (key == "toggle") cfgToggle = value;
|
|
265
|
+
else if (key == "follow") cfgFollow = value;
|
|
266
|
+
else if (key == "quit") cfgQuit = value;
|
|
267
|
+
else if (key == "followoffsety")
|
|
268
|
+
{
|
|
269
|
+
int parsed;
|
|
270
|
+
if (int.TryParse(value, out parsed)) cfgOffsetY = parsed;
|
|
271
|
+
}
|
|
272
|
+
else if (key == "blockmousebutton")
|
|
273
|
+
{
|
|
274
|
+
blockMouseButton = value != "false" && value != "0" && value != "no";
|
|
275
|
+
}
|
|
276
|
+
else if (key == "openmini") cfgOpenMini = value;
|
|
277
|
+
else if (key == "whalescale")
|
|
278
|
+
{
|
|
279
|
+
int parsed;
|
|
280
|
+
if (int.TryParse(value, out parsed) && parsed >= 2 && parsed <= 20) whaleScale = parsed;
|
|
281
|
+
}
|
|
282
|
+
else if (key == "whaleoffsetx")
|
|
283
|
+
{
|
|
284
|
+
int parsed;
|
|
285
|
+
if (int.TryParse(value, out parsed)) whaleOffsetX = parsed;
|
|
286
|
+
}
|
|
287
|
+
else if (key == "whaleoffsety")
|
|
288
|
+
{
|
|
289
|
+
int parsed;
|
|
290
|
+
if (int.TryParse(value, out parsed)) whaleOffsetY = parsed;
|
|
291
|
+
}
|
|
292
|
+
else if (key == "whaleimage")
|
|
293
|
+
{
|
|
294
|
+
if (value.Length > 0) whaleImage = value;
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
catch (Exception ex) { Log("config read failed: " + ex.Message); }
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
static void SaveConfig(string toggle, string follow, string quit, int offsetY)
|
|
302
|
+
{
|
|
303
|
+
cfgToggle = toggle;
|
|
304
|
+
cfgFollow = follow;
|
|
305
|
+
cfgQuit = quit;
|
|
306
|
+
cfgOffsetY = offsetY;
|
|
307
|
+
File.WriteAllText(ConfigPath, BuildConfigText(), Encoding.UTF8);
|
|
308
|
+
Log("config saved: toggle=" + cfgToggle + ", follow=" + cfgFollow + ", quit=" + cfgQuit);
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
// ---------------------------------------------------------------- hotkeys
|
|
312
|
+
|
|
313
|
+
static bool TryVk(string key, out uint vk)
|
|
314
|
+
{
|
|
315
|
+
vk = 0;
|
|
316
|
+
if (key.Length == 0) return false;
|
|
317
|
+
if (key.Length == 1)
|
|
318
|
+
{
|
|
319
|
+
var c = char.ToUpperInvariant(key[0]);
|
|
320
|
+
if (c >= 'A' && c <= 'Z') { vk = (uint)c; return true; }
|
|
321
|
+
if (c >= '0' && c <= '9') { vk = (uint)c; return true; }
|
|
322
|
+
switch (c)
|
|
323
|
+
{
|
|
324
|
+
case '`': case '~': vk = 0xC0; return true;
|
|
325
|
+
case '-': case '_': vk = 0xBD; return true;
|
|
326
|
+
case '=': case '+': vk = 0xBB; return true;
|
|
327
|
+
case '[': case '{': vk = 0xDB; return true;
|
|
328
|
+
case ']': case '}': vk = 0xDD; return true;
|
|
329
|
+
case '\\': case '|': vk = 0xDC; return true;
|
|
330
|
+
case ';': case ':': vk = 0xBA; return true;
|
|
331
|
+
case '\'': case '"': vk = 0xDE; return true;
|
|
332
|
+
case ',': case '<': vk = 0xBC; return true;
|
|
333
|
+
case '.': case '>': vk = 0xBE; return true;
|
|
334
|
+
case '/': case '?': vk = 0xBF; return true;
|
|
335
|
+
}
|
|
336
|
+
return false;
|
|
337
|
+
}
|
|
338
|
+
var lower = key.ToLowerInvariant();
|
|
339
|
+
if (lower.Length >= 2 && lower[0] == 'f')
|
|
340
|
+
{
|
|
341
|
+
int num;
|
|
342
|
+
if (int.TryParse(lower.Substring(1), out num) && num >= 1 && num <= 24)
|
|
343
|
+
{
|
|
344
|
+
vk = (uint)(0x70 + num - 1);
|
|
345
|
+
return true;
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
switch (lower)
|
|
349
|
+
{
|
|
350
|
+
case "space": vk = 0x20; return true;
|
|
351
|
+
case "enter": case "return": vk = 0x0D; return true;
|
|
352
|
+
case "esc": case "escape": vk = 0x1B; return true;
|
|
353
|
+
case "tab": vk = 0x09; return true;
|
|
354
|
+
case "backspace": vk = 0x08; return true;
|
|
355
|
+
case "delete": case "del": vk = 0x2E; return true;
|
|
356
|
+
case "insert": case "ins": vk = 0x2D; return true;
|
|
357
|
+
case "home": vk = 0x24; return true;
|
|
358
|
+
case "end": vk = 0x23; return true;
|
|
359
|
+
case "pageup": vk = 0x21; return true;
|
|
360
|
+
case "pagedown": vk = 0x22; return true;
|
|
361
|
+
case "up": vk = 0x26; return true;
|
|
362
|
+
case "down": vk = 0x28; return true;
|
|
363
|
+
case "left": vk = 0x25; return true;
|
|
364
|
+
case "right": vk = 0x27; return true;
|
|
365
|
+
case "printscreen": vk = 0x2C; return true;
|
|
366
|
+
}
|
|
367
|
+
return false;
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
static bool ValidateText(string raw, out string error)
|
|
371
|
+
{
|
|
372
|
+
error = null;
|
|
373
|
+
var text = raw == null ? "" : raw.Trim();
|
|
374
|
+
if (text.Length == 0) return true;
|
|
375
|
+
int button;
|
|
376
|
+
if (TryParseMouse(text, out button)) return true;
|
|
377
|
+
uint mods, vk;
|
|
378
|
+
return TryParseHotkey(text, out mods, out vk, out error);
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
static bool TryParseMouse(string raw, out int button)
|
|
382
|
+
{
|
|
383
|
+
button = 0;
|
|
384
|
+
if (raw == null) return false;
|
|
385
|
+
var s = raw.Trim().ToLowerInvariant();
|
|
386
|
+
switch (s)
|
|
387
|
+
{
|
|
388
|
+
case "xbutton1": case "mouse4": case "mb4": button = 1; return true;
|
|
389
|
+
case "xbutton2": case "mouse5": case "mb5": button = 2; return true;
|
|
390
|
+
case "middle": case "mbutton": case "mouse3": button = 3; return true;
|
|
391
|
+
}
|
|
392
|
+
return false;
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
static bool TryParseHotkey(string raw, out uint modifiers, out uint vk, out string error)
|
|
396
|
+
{
|
|
397
|
+
modifiers = 0;
|
|
398
|
+
vk = 0;
|
|
399
|
+
error = null;
|
|
400
|
+
if (raw == null || raw.Trim().Length == 0) { error = "empty"; return false; }
|
|
401
|
+
var parts = raw.Split('+');
|
|
402
|
+
for (int i = 0; i < parts.Length; i++)
|
|
403
|
+
{
|
|
404
|
+
var piece = parts[i].Trim();
|
|
405
|
+
if (piece.Length == 0) { error = "empty part"; return false; }
|
|
406
|
+
if (i < parts.Length - 1)
|
|
407
|
+
{
|
|
408
|
+
var m = piece.ToLowerInvariant();
|
|
409
|
+
if (m == "alt") modifiers |= Native.MOD_ALT;
|
|
410
|
+
else if (m == "ctrl" || m == "control") modifiers |= Native.MOD_CTRL;
|
|
411
|
+
else if (m == "shift") modifiers |= Native.MOD_SHIFT;
|
|
412
|
+
else if (m == "win" || m == "windows") modifiers |= Native.MOD_WIN;
|
|
413
|
+
else { error = "unknown modifier '" + piece + "'"; return false; }
|
|
414
|
+
}
|
|
415
|
+
else
|
|
416
|
+
{
|
|
417
|
+
uint parsed;
|
|
418
|
+
if (!TryVk(piece, out parsed)) { error = "unsupported key '" + piece + "'"; return false; }
|
|
419
|
+
vk = parsed;
|
|
420
|
+
}
|
|
421
|
+
}
|
|
422
|
+
if (modifiers == 0) { error = "needs at least one modifier"; return false; }
|
|
423
|
+
modifiers |= Native.MOD_NOREPEAT;
|
|
424
|
+
return true;
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
// ---------------------------------------------------------------- windows
|
|
428
|
+
|
|
429
|
+
static string TitleOf(IntPtr hWnd)
|
|
430
|
+
{
|
|
431
|
+
var sb = new StringBuilder(512);
|
|
432
|
+
Native.GetWindowText(hWnd, sb, sb.Capacity);
|
|
433
|
+
return sb.ToString();
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
static List<IntPtr> FindByProcess(string processName)
|
|
437
|
+
{
|
|
438
|
+
var found = new List<IntPtr>();
|
|
439
|
+
var pids = new HashSet<uint>();
|
|
440
|
+
try
|
|
441
|
+
{
|
|
442
|
+
foreach (var p in Process.GetProcessesByName(processName))
|
|
443
|
+
{
|
|
444
|
+
pids.Add((uint)p.Id);
|
|
445
|
+
p.Dispose();
|
|
446
|
+
}
|
|
447
|
+
}
|
|
448
|
+
catch (Exception ex) { Log("process lookup failed: " + ex.Message); }
|
|
449
|
+
if (pids.Count == 0) return found;
|
|
450
|
+
|
|
451
|
+
Native.EnumWindows(delegate(IntPtr hWnd, IntPtr l)
|
|
452
|
+
{
|
|
453
|
+
uint pid;
|
|
454
|
+
Native.GetWindowThreadProcessId(hWnd, out pid);
|
|
455
|
+
// 只比较 pid,不要对每个窗口调 Process.GetProcessById:那样每个窗口都要开一次进程句柄,
|
|
456
|
+
// 几百个顶层窗口能拖出好几秒(这正是"按了要等很久"的来源)。
|
|
457
|
+
if (pids.Contains(pid)) found.Add(hWnd);
|
|
458
|
+
return true;
|
|
459
|
+
}, IntPtr.Zero);
|
|
460
|
+
return found;
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
// 主窗被我们 SW_HIDE 之后就不算 visible 了,只按 visible 找会找不到它 ——
|
|
464
|
+
// 于是"小窗 -> 主窗"那一半就永远切不回去。这里用句柄缓存 + IsWindow(隐藏也算存在)。
|
|
465
|
+
static IntPtr cachedMain = IntPtr.Zero;
|
|
466
|
+
static IntPtr cachedMini = IntPtr.Zero;
|
|
467
|
+
|
|
468
|
+
static IntPtr FindMain()
|
|
469
|
+
{
|
|
470
|
+
if (cachedMain != IntPtr.Zero && Native.IsWindow(cachedMain)) return cachedMain;
|
|
471
|
+
cachedMain = IntPtr.Zero;
|
|
472
|
+
IntPtr titled = IntPtr.Zero;
|
|
473
|
+
foreach (var h in FindByProcess(MainProcess))
|
|
474
|
+
{
|
|
475
|
+
var title = TitleOf(h);
|
|
476
|
+
if (title.Length == 0) continue;
|
|
477
|
+
if (Native.IsWindowVisible(h)) { cachedMain = h; return h; }
|
|
478
|
+
if (titled == IntPtr.Zero) titled = h;
|
|
479
|
+
}
|
|
480
|
+
cachedMain = titled;
|
|
481
|
+
return cachedMain;
|
|
482
|
+
}
|
|
483
|
+
|
|
484
|
+
static IntPtr FindMini()
|
|
485
|
+
{
|
|
486
|
+
if (cachedMini != IntPtr.Zero && Native.IsWindow(cachedMini)) return cachedMini;
|
|
487
|
+
cachedMini = IntPtr.Zero;
|
|
488
|
+
IntPtr titled = IntPtr.Zero;
|
|
489
|
+
foreach (var h in FindByProcess(MiniProcess))
|
|
490
|
+
{
|
|
491
|
+
var title = TitleOf(h);
|
|
492
|
+
if (!title.StartsWith("DSH 小窗", StringComparison.Ordinal)) continue;
|
|
493
|
+
if (Native.IsWindowVisible(h)) { cachedMini = h; return h; }
|
|
494
|
+
if (titled == IntPtr.Zero) titled = h;
|
|
495
|
+
}
|
|
496
|
+
cachedMini = titled;
|
|
497
|
+
return cachedMini;
|
|
498
|
+
}
|
|
499
|
+
|
|
500
|
+
static void Show(IntPtr hWnd)
|
|
501
|
+
{
|
|
502
|
+
if (Native.IsIconic(hWnd)) Native.ShowWindow(hWnd, Native.SW_RESTORE);
|
|
503
|
+
Native.ShowWindow(hWnd, Native.SW_SHOW);
|
|
504
|
+
Native.SetForegroundWindow(hWnd);
|
|
505
|
+
}
|
|
506
|
+
|
|
507
|
+
// 窗口操作全部丢到后台线程:SetForegroundWindow/ShowWindow 在 Electron 卡顿时会阻塞,
|
|
508
|
+
// 放在 UI 线程上会把消息队列堵住 —— 那正是"按了没反应、过很久才响应"的原因。
|
|
509
|
+
// 用专用工作线程 + 队列,不用线程池:池被其它东西饿住时延迟能到好几秒。
|
|
510
|
+
static readonly System.Collections.Concurrent.BlockingCollection<string> actionQueue =
|
|
511
|
+
new System.Collections.Concurrent.BlockingCollection<string>();
|
|
512
|
+
|
|
513
|
+
static void RaiseProcessPriority()
|
|
514
|
+
{
|
|
515
|
+
try
|
|
516
|
+
{
|
|
517
|
+
var handle = Native.GetCurrentProcess();
|
|
518
|
+
var priority = Native.SetPriorityClass(handle, 0x00008000); // ABOVE_NORMAL_PRIORITY_CLASS
|
|
519
|
+
var state = new Native.PROCESS_POWER_THROTTLING_STATE();
|
|
520
|
+
state.Version = 1;
|
|
521
|
+
state.ControlMask = 0x1; // PROCESS_POWER_THROTTLING_EXECUTION_SPEED
|
|
522
|
+
state.StateMask = 0x0; // 关闭节流
|
|
523
|
+
var power = Native.SetProcessInformation(handle, 4 /* ProcessPowerThrottling */, ref state,
|
|
524
|
+
(uint)Marshal.SizeOf(typeof(Native.PROCESS_POWER_THROTTLING_STATE)));
|
|
525
|
+
Log(string.Format("priority above_normal={0} no_throttle={1}", priority, power));
|
|
526
|
+
}
|
|
527
|
+
catch (Exception ex) { Log("priority failed: " + ex.Message); }
|
|
528
|
+
}
|
|
529
|
+
|
|
530
|
+
static void StartWorker()
|
|
531
|
+
{
|
|
532
|
+
var worker = new Thread(delegate()
|
|
533
|
+
{
|
|
534
|
+
foreach (var action in actionQueue.GetConsumingEnumerable())
|
|
535
|
+
{
|
|
536
|
+
try
|
|
537
|
+
{
|
|
538
|
+
if (action == "toggle")
|
|
539
|
+
{
|
|
540
|
+
if (opening) { Log("toggle ignored: opening in progress"); continue; }
|
|
541
|
+
ToggleCore();
|
|
542
|
+
}
|
|
543
|
+
else if (action == "follow") Post(ForwardToggle);
|
|
544
|
+
else if (action == "openmini") Post(ForwardToggle);
|
|
545
|
+
else if (action == "quit") Post(ExitApp);
|
|
546
|
+
}
|
|
547
|
+
catch (Exception ex) { Log("action failed: " + ex.Message); }
|
|
548
|
+
}
|
|
549
|
+
});
|
|
550
|
+
worker.IsBackground = true;
|
|
551
|
+
worker.Name = "dsh-hotkey-worker";
|
|
552
|
+
worker.Start();
|
|
553
|
+
}
|
|
554
|
+
|
|
555
|
+
static void RunAction(string action)
|
|
556
|
+
{
|
|
557
|
+
try { actionQueue.Add(action); }
|
|
558
|
+
catch (Exception ex) { Log("enqueue failed: " + ex.Message); }
|
|
559
|
+
}
|
|
560
|
+
|
|
561
|
+
/// <summary>
|
|
562
|
+
/// 小窗不在时请求宿主拉起它,然后在后台轮询等待窗口出现。
|
|
563
|
+
/// 全程不占 UI 线程,也不在钩子回调里阻塞。
|
|
564
|
+
/// </summary>
|
|
565
|
+
static void StartOpenMini()
|
|
566
|
+
{
|
|
567
|
+
if (opening) return;
|
|
568
|
+
opening = true;
|
|
569
|
+
Post(delegate { ShowToast("小窗没开,正在拉起…"); });
|
|
570
|
+
var worker = new Thread(delegate()
|
|
571
|
+
{
|
|
572
|
+
try
|
|
573
|
+
{
|
|
574
|
+
var dataDir = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "dsh-mini", "bin", "data");
|
|
575
|
+
var reqPath = Path.Combine(dataDir, "request.txt");
|
|
576
|
+
if (!File.Exists(reqPath)) { Log("open mini: no request.txt"); return; }
|
|
577
|
+
var url = File.ReadAllText(reqPath, Encoding.UTF8).Trim();
|
|
578
|
+
var u = new Uri(url);
|
|
579
|
+
var target = u.Scheme + "://" + u.Authority + "/dsh-mini/open?" + u.Query.TrimStart('?');
|
|
580
|
+
using (var client = new System.Net.WebClient())
|
|
581
|
+
{
|
|
582
|
+
client.Headers["content-type"] = "application/json";
|
|
583
|
+
client.UploadString(target, "POST", "{\"href\":\"\",\"sessionId\":\"\"}");
|
|
584
|
+
}
|
|
585
|
+
Log("open mini: requested");
|
|
586
|
+
for (int i = 0; i < 60; i++)
|
|
587
|
+
{
|
|
588
|
+
Thread.Sleep(200);
|
|
589
|
+
var mini = FindMini();
|
|
590
|
+
if (mini == IntPtr.Zero) continue;
|
|
591
|
+
var main = FindMain();
|
|
592
|
+
if (main != IntPtr.Zero) Native.ShowWindow(main, Native.SW_HIDE);
|
|
593
|
+
Show(mini);
|
|
594
|
+
Log("-> mini (auto opened)");
|
|
595
|
+
return;
|
|
596
|
+
}
|
|
597
|
+
Log("open mini: timeout");
|
|
598
|
+
Post(delegate { ShowToast("小窗没拉起来:先点一次主窗的 ▯,或重启 DSH"); });
|
|
599
|
+
}
|
|
600
|
+
catch (Exception ex) { Log("open mini failed: " + ex.Message); }
|
|
601
|
+
finally { opening = false; }
|
|
602
|
+
});
|
|
603
|
+
worker.IsBackground = true;
|
|
604
|
+
worker.Start();
|
|
605
|
+
}
|
|
606
|
+
|
|
607
|
+
static void ToggleCore()
|
|
608
|
+
{
|
|
609
|
+
if (opening) { Log("toggle ignored: opening in progress"); return; }
|
|
610
|
+
var main = FindMain();
|
|
611
|
+
var mini = FindMini();
|
|
612
|
+
Log(string.Format("toggle main={0} mini={1}", main, mini));
|
|
613
|
+
|
|
614
|
+
if (mini == IntPtr.Zero)
|
|
615
|
+
{
|
|
616
|
+
Log("no mini window: 请求宿主拉起");
|
|
617
|
+
StartOpenMini();
|
|
618
|
+
return;
|
|
619
|
+
}
|
|
620
|
+
if (Native.IsWindowVisible(mini) && (Native.GetForegroundWindow() == mini || main == IntPtr.Zero || !Native.IsWindowVisible(main)))
|
|
621
|
+
{
|
|
622
|
+
Native.ShowWindow(mini, Native.SW_HIDE);
|
|
623
|
+
if (main != IntPtr.Zero) Show(main);
|
|
624
|
+
Log("-> main");
|
|
625
|
+
}
|
|
626
|
+
else
|
|
627
|
+
{
|
|
628
|
+
if (main != IntPtr.Zero) Native.ShowWindow(main, Native.SW_HIDE);
|
|
629
|
+
Show(mini);
|
|
630
|
+
Log("-> mini");
|
|
631
|
+
}
|
|
632
|
+
}
|
|
633
|
+
|
|
634
|
+
static void FollowCore()
|
|
635
|
+
{
|
|
636
|
+
var mini = FindMini();
|
|
637
|
+
if (mini == IntPtr.Zero) { Log("follow: no mini window"); return; }
|
|
638
|
+
Native.RECT rect;
|
|
639
|
+
if (!Native.GetWindowRect(mini, out rect)) { Log("follow: no rect"); return; }
|
|
640
|
+
Native.POINT p;
|
|
641
|
+
Native.GetCursorPos(out p);
|
|
642
|
+
int w = rect.Right - rect.Left;
|
|
643
|
+
int x = p.X - w / 2;
|
|
644
|
+
int y = p.Y - cfgOffsetY;
|
|
645
|
+
Native.SetWindowPos(mini, Native.HWND_TOPMOST, x, y, 0, 0, Native.SWP_NOSIZE | Native.SWP_SHOWWINDOW);
|
|
646
|
+
Native.SetForegroundWindow(mini);
|
|
647
|
+
Log(string.Format("follow -> {0},{1} (offsetY={2})", x, y, cfgOffsetY));
|
|
648
|
+
}
|
|
649
|
+
|
|
650
|
+
// ---------------------------------------------------------------- 鲸鱼跟随
|
|
651
|
+
|
|
652
|
+
static Bitmap whaleBitmap;
|
|
653
|
+
static bool whalePainted;
|
|
654
|
+
|
|
655
|
+
static void LoadWhaleBitmap()
|
|
656
|
+
{
|
|
657
|
+
if (whaleBitmap != null) return;
|
|
658
|
+
try
|
|
659
|
+
{
|
|
660
|
+
var path = Path.Combine(BaseDir, whaleImage);
|
|
661
|
+
if (File.Exists(path)) whaleBitmap = new Bitmap(path);
|
|
662
|
+
else Log("whale image missing: " + path);
|
|
663
|
+
}
|
|
664
|
+
catch (Exception ex) { Log("whale image failed: " + ex.Message); }
|
|
665
|
+
}
|
|
666
|
+
|
|
667
|
+
static Size WhaleSize()
|
|
668
|
+
{
|
|
669
|
+
int cursor = Native.GetSystemMetrics(Native.SM_CXCURSOR);
|
|
670
|
+
if (cursor < 16) cursor = 32;
|
|
671
|
+
int width = Math.Max(60, cursor * whaleScale);
|
|
672
|
+
int height = (int)Math.Round(width * 0.58);
|
|
673
|
+
try
|
|
674
|
+
{
|
|
675
|
+
if (whaleBitmap != null && whaleBitmap.Width > 0)
|
|
676
|
+
height = Math.Max(24, (int)Math.Round(width * (double)whaleBitmap.Height / whaleBitmap.Width));
|
|
677
|
+
}
|
|
678
|
+
catch { }
|
|
679
|
+
return new Size(width, height);
|
|
680
|
+
}
|
|
681
|
+
|
|
682
|
+
static void BuildWhale()
|
|
683
|
+
{
|
|
684
|
+
if (whaleForm != null) return;
|
|
685
|
+
LoadWhaleBitmap();
|
|
686
|
+
var form = new Form();
|
|
687
|
+
form.FormBorderStyle = FormBorderStyle.None;
|
|
688
|
+
form.ShowInTaskbar = false;
|
|
689
|
+
form.TopMost = true;
|
|
690
|
+
form.StartPosition = FormStartPosition.Manual;
|
|
691
|
+
form.BackColor = Color.Black;
|
|
692
|
+
form.BackgroundImage = whaleBitmap;
|
|
693
|
+
form.BackgroundImageLayout = ImageLayout.Stretch;
|
|
694
|
+
form.Size = WhaleSize();
|
|
695
|
+
var handle = form.Handle;
|
|
696
|
+
int ex = Native.GetWindowLong(handle, Native.GWL_EXSTYLE);
|
|
697
|
+
Native.SetWindowLong(handle, Native.GWL_EXSTYLE, ex | Native.WS_EX_LAYERED | Native.WS_EX_TRANSPARENT | Native.WS_EX_NOACTIVATE | Native.WS_EX_TOOLWINDOW);
|
|
698
|
+
whaleForm = form;
|
|
699
|
+
Log("whale ready (colorkey)");
|
|
700
|
+
}
|
|
701
|
+
|
|
702
|
+
static Point WhaleTarget(Point p, Size size)
|
|
703
|
+
{
|
|
704
|
+
int x = p.X + whaleOffsetX;
|
|
705
|
+
int y = p.Y + whaleOffsetY;
|
|
706
|
+
var screen = Screen.FromPoint(p).WorkingArea;
|
|
707
|
+
if (x + size.Width > screen.Right) x = p.X - size.Width - whaleOffsetX;
|
|
708
|
+
if (y + size.Height > screen.Bottom) y = p.Y - size.Height - whaleOffsetY;
|
|
709
|
+
if (x < screen.Left) x = screen.Left;
|
|
710
|
+
if (y < screen.Top) y = screen.Top;
|
|
711
|
+
return new Point(x, y);
|
|
712
|
+
}
|
|
713
|
+
|
|
714
|
+
static IntPtr CreatePremultipliedDib(Bitmap bitmap)
|
|
715
|
+
{
|
|
716
|
+
int w = bitmap.Width;
|
|
717
|
+
int h = bitmap.Height;
|
|
718
|
+
var info = new Native.BITMAPINFO();
|
|
719
|
+
info.bmiHeader.biSize = (uint)Marshal.SizeOf(typeof(Native.BITMAPINFOHEADER));
|
|
720
|
+
info.bmiHeader.biWidth = w;
|
|
721
|
+
info.bmiHeader.biHeight = -h;
|
|
722
|
+
info.bmiHeader.biPlanes = 1;
|
|
723
|
+
info.bmiHeader.biBitCount = 32;
|
|
724
|
+
info.bmiHeader.biCompression = 0;
|
|
725
|
+
IntPtr bits;
|
|
726
|
+
var handle = Native.CreateDIBSection(IntPtr.Zero, ref info, 0, out bits, IntPtr.Zero, 0);
|
|
727
|
+
if (handle == IntPtr.Zero) return IntPtr.Zero;
|
|
728
|
+
var data = bitmap.LockBits(new Rectangle(0, 0, w, h), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
|
|
729
|
+
try
|
|
730
|
+
{
|
|
731
|
+
var row = new byte[w * 4];
|
|
732
|
+
for (int y = 0; y < h; y++)
|
|
733
|
+
{
|
|
734
|
+
Marshal.Copy(IntPtr.Add(data.Scan0, y * data.Stride), row, 0, row.Length);
|
|
735
|
+
for (int i = 0; i < row.Length; i += 4)
|
|
736
|
+
{
|
|
737
|
+
byte a = row[i + 3];
|
|
738
|
+
if (a == 0) { row[i] = 0; row[i + 1] = 0; row[i + 2] = 0; }
|
|
739
|
+
else if (a < 255)
|
|
740
|
+
{
|
|
741
|
+
row[i] = (byte)(row[i] * a / 255);
|
|
742
|
+
row[i + 1] = (byte)(row[i + 1] * a / 255);
|
|
743
|
+
row[i + 2] = (byte)(row[i + 2] * a / 255);
|
|
744
|
+
}
|
|
745
|
+
}
|
|
746
|
+
Marshal.Copy(row, 0, IntPtr.Add(bits, y * w * 4), row.Length);
|
|
747
|
+
}
|
|
748
|
+
}
|
|
749
|
+
finally { bitmap.UnlockBits(data); }
|
|
750
|
+
return handle;
|
|
751
|
+
}
|
|
752
|
+
|
|
753
|
+
/// <summary>per-pixel alpha 贴图:没有键控颜色,边缘不会有红/洋红描边。</summary>
|
|
754
|
+
static bool PaintWhaleAt(int x, int y)
|
|
755
|
+
{
|
|
756
|
+
if (whaleForm == null) return false;
|
|
757
|
+
LoadWhaleBitmap();
|
|
758
|
+
if (whaleBitmap == null) { Log("paint whale: no bitmap"); return false; }
|
|
759
|
+
var size = WhaleSize();
|
|
760
|
+
try
|
|
761
|
+
{
|
|
762
|
+
using (var scaled = new Bitmap(size.Width, size.Height, PixelFormat.Format32bppArgb))
|
|
763
|
+
{
|
|
764
|
+
using (var g = Graphics.FromImage(scaled))
|
|
765
|
+
{
|
|
766
|
+
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
|
|
767
|
+
g.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
|
|
768
|
+
g.Clear(Color.Transparent);
|
|
769
|
+
g.DrawImage(whaleBitmap, new Rectangle(0, 0, size.Width, size.Height));
|
|
770
|
+
}
|
|
771
|
+
var hBitmap = CreatePremultipliedDib(scaled);
|
|
772
|
+
if (hBitmap == IntPtr.Zero) { Log("dib create failed"); return false; }
|
|
773
|
+
var screenDc = Native.GetDC(IntPtr.Zero);
|
|
774
|
+
var memDc = Native.CreateCompatibleDC(screenDc);
|
|
775
|
+
var previous = Native.SelectObject(memDc, hBitmap);
|
|
776
|
+
var dst = new Native.POINT { X = x, Y = y };
|
|
777
|
+
var src = new Native.POINT { X = 0, Y = 0 };
|
|
778
|
+
var sz = new Native.SIZE { cx = size.Width, cy = size.Height };
|
|
779
|
+
var blend = new Native.BLENDFUNCTION();
|
|
780
|
+
blend.BlendOp = 0;
|
|
781
|
+
blend.BlendFlags = 0;
|
|
782
|
+
blend.SourceConstantAlpha = 255;
|
|
783
|
+
blend.AlphaFormat = 1;
|
|
784
|
+
var ok = Native.UpdateLayeredWindow(whaleForm.Handle, screenDc, ref dst, ref sz, memDc, ref src, 0, ref blend, 2);
|
|
785
|
+
Native.SelectObject(memDc, previous);
|
|
786
|
+
Native.DeleteObject(hBitmap);
|
|
787
|
+
Native.DeleteDC(memDc);
|
|
788
|
+
Native.ReleaseDC(IntPtr.Zero, screenDc);
|
|
789
|
+
whalePainted = ok;
|
|
790
|
+
if (!ok) Log("UpdateLayeredWindow failed");
|
|
791
|
+
return ok;
|
|
792
|
+
}
|
|
793
|
+
}
|
|
794
|
+
catch (Exception ex) { Log("paint whale failed: " + ex.Message); return false; }
|
|
795
|
+
}
|
|
796
|
+
|
|
797
|
+
static void PositionWhale()
|
|
798
|
+
{
|
|
799
|
+
if (whaleForm == null) return;
|
|
800
|
+
try
|
|
801
|
+
{
|
|
802
|
+
var size = WhaleSize();
|
|
803
|
+
var target = WhaleTarget(Cursor.Position, size);
|
|
804
|
+
if (whaleForm.Size != size || !whalePainted)
|
|
805
|
+
{
|
|
806
|
+
whaleForm.Size = size;
|
|
807
|
+
PaintWhaleAt(target.X, target.Y);
|
|
808
|
+
return;
|
|
809
|
+
}
|
|
810
|
+
Native.SetWindowPos(whaleForm.Handle, Native.HWND_TOPMOST, target.X, target.Y, 0, 0,
|
|
811
|
+
Native.SWP_NOSIZE | Native.SWP_SHOWWINDOW | 0x0010 /* SWP_NOACTIVATE */);
|
|
812
|
+
}
|
|
813
|
+
catch { }
|
|
814
|
+
}
|
|
815
|
+
|
|
816
|
+
static void ShowWhale()
|
|
817
|
+
{
|
|
818
|
+
try
|
|
819
|
+
{
|
|
820
|
+
BuildWhale();
|
|
821
|
+
var size = WhaleSize();
|
|
822
|
+
whaleForm.Size = size;
|
|
823
|
+
var target = WhaleTarget(Cursor.Position, size);
|
|
824
|
+
whaleForm.Location = target;
|
|
825
|
+
whaleForm.Show();
|
|
826
|
+
whalePainted = false;
|
|
827
|
+
PaintWhaleAt(target.X, target.Y);
|
|
828
|
+
followActive = true;
|
|
829
|
+
Log("-> whale " + target.X + "," + target.Y);
|
|
830
|
+
}
|
|
831
|
+
catch (Exception ex) { Log("show whale failed: " + ex.Message); }
|
|
832
|
+
}
|
|
833
|
+
|
|
834
|
+
static void HideWhale()
|
|
835
|
+
{
|
|
836
|
+
followActive = false;
|
|
837
|
+
try { if (whaleForm != null) whaleForm.Hide(); } catch { }
|
|
838
|
+
}
|
|
839
|
+
|
|
840
|
+
static void CollapseToWhale()
|
|
841
|
+
{
|
|
842
|
+
var mini = FindMini();
|
|
843
|
+
if (mini == IntPtr.Zero) return;
|
|
844
|
+
if (whaleForm == null) BuildWhale();
|
|
845
|
+
Native.ShowWindow(mini, Native.SW_HIDE);
|
|
846
|
+
collapseArmed = false;
|
|
847
|
+
ShowWhale();
|
|
848
|
+
}
|
|
849
|
+
|
|
850
|
+
/// <summary>侧键"前进"= 把真正的小窗叫回来(并收起鲸鱼)。</summary>
|
|
851
|
+
static void ShowMiniWindow()
|
|
852
|
+
{
|
|
853
|
+
HideWhale();
|
|
854
|
+
var mini = FindMini();
|
|
855
|
+
if (mini == IntPtr.Zero) { StartOpenMini(); return; }
|
|
856
|
+
var main = FindMain();
|
|
857
|
+
if (main != IntPtr.Zero) Native.ShowWindow(main, Native.SW_HIDE);
|
|
858
|
+
Show(mini);
|
|
859
|
+
collapseArmed = false;
|
|
860
|
+
Log("openmini -> mini");
|
|
861
|
+
}
|
|
862
|
+
|
|
863
|
+
/// <summary>前进键:鲸鱼 -> 小窗;小窗 -> 鲸鱼。一个键双向。</summary>
|
|
864
|
+
static void ForwardToggle()
|
|
865
|
+
{
|
|
866
|
+
if (followActive) { ShowMiniWindow(); return; }
|
|
867
|
+
var mini = FindMini();
|
|
868
|
+
if (mini == IntPtr.Zero || !Native.IsWindowVisible(mini)) { ShowMiniWindow(); return; }
|
|
869
|
+
Native.ShowWindow(mini, Native.SW_HIDE);
|
|
870
|
+
collapseArmed = false;
|
|
871
|
+
ShowWhale();
|
|
872
|
+
Log("forward -> whale");
|
|
873
|
+
}
|
|
874
|
+
|
|
875
|
+
static void ToggleWhale() {
|
|
876
|
+
if (followActive)
|
|
877
|
+
{
|
|
878
|
+
HideWhale();
|
|
879
|
+
ShowMiniWindow();
|
|
880
|
+
}
|
|
881
|
+
else
|
|
882
|
+
{
|
|
883
|
+
CollapseToWhale();
|
|
884
|
+
}
|
|
885
|
+
}
|
|
886
|
+
|
|
887
|
+
static void FollowTick(object sender, EventArgs e)
|
|
888
|
+
{
|
|
889
|
+
try
|
|
890
|
+
{
|
|
891
|
+
if (followActive)
|
|
892
|
+
{
|
|
893
|
+
PositionWhale();
|
|
894
|
+
return;
|
|
895
|
+
}
|
|
896
|
+
var mini = FindMini();
|
|
897
|
+
if (mini == IntPtr.Zero || !Native.IsWindowVisible(mini)) { collapseArmed = false; return; }
|
|
898
|
+
Native.RECT rect;
|
|
899
|
+
if (!Native.GetWindowRect(mini, out rect)) return;
|
|
900
|
+
var cursor = Cursor.Position;
|
|
901
|
+
bool inside = cursor.X >= rect.Left && cursor.X <= rect.Right && cursor.Y >= rect.Top && cursor.Y <= rect.Bottom;
|
|
902
|
+
if (inside) { collapseArmed = true; lastCursor = cursor; return; }
|
|
903
|
+
// 只有“进过小窗之后再移出去”才收成鲸鱼;
|
|
904
|
+
// 刚用前进键唤出小窗时光标在别处,不能立刻又把它收起来。
|
|
905
|
+
if (collapseArmed) { collapseArmed = false; CollapseToWhale(); }
|
|
906
|
+
}
|
|
907
|
+
catch (Exception ex) { Log("follow tick failed: " + ex.Message); }
|
|
908
|
+
}
|
|
909
|
+
|
|
910
|
+
// ---------------------------------------------------------------- mouse hook
|
|
911
|
+
|
|
912
|
+
static IntPtr MouseHookProc(int nCode, IntPtr wParam, IntPtr lParam)
|
|
913
|
+
{
|
|
914
|
+
try
|
|
915
|
+
{
|
|
916
|
+
if (nCode >= 0)
|
|
917
|
+
{
|
|
918
|
+
var msg = wParam.ToInt32();
|
|
919
|
+
if (msg == Native.WM_XBUTTONDOWN || msg == Native.WM_MBUTTONDOWN)
|
|
920
|
+
{
|
|
921
|
+
var data = (Native.MSLLHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(Native.MSLLHOOKSTRUCT));
|
|
922
|
+
int button = msg == Native.WM_MBUTTONDOWN ? 3 : (int)((data.mouseData >> 16) & 0xFFFF);
|
|
923
|
+
string action;
|
|
924
|
+
if (mouseActions.TryGetValue(button, out action))
|
|
925
|
+
{
|
|
926
|
+
Log("mouse button " + button + " -> " + action);
|
|
927
|
+
// 钩子回调里只排队,绝不阻塞:低级鼠标钩子超时会被系统摘掉。
|
|
928
|
+
RunAction(action);
|
|
929
|
+
if (blockMouseButton) return new IntPtr(1);
|
|
930
|
+
}
|
|
931
|
+
}
|
|
932
|
+
}
|
|
933
|
+
}
|
|
934
|
+
catch (Exception ex) { Log("mouse hook error: " + ex.Message); }
|
|
935
|
+
return Native.CallNextHookEx(mouseHook, nCode, wParam, lParam);
|
|
936
|
+
}
|
|
937
|
+
|
|
938
|
+
static void InstallMouseHook()
|
|
939
|
+
{
|
|
940
|
+
try
|
|
941
|
+
{
|
|
942
|
+
mouseProc = MouseHookProc;
|
|
943
|
+
mouseHook = Native.SetWindowsHookEx(Native.WH_MOUSE_LL, mouseProc, Native.GetModuleHandle(null), 0);
|
|
944
|
+
Log("mouse hook installed=" + (mouseHook != IntPtr.Zero));
|
|
945
|
+
}
|
|
946
|
+
catch (Exception ex) { Log("mouse hook failed: " + ex.Message); }
|
|
947
|
+
}
|
|
948
|
+
|
|
949
|
+
/// <summary>
|
|
950
|
+
/// 只有真的绑了鼠标键才装钩子。低级鼠标钩子会收到每一次鼠标事件(包括移动),
|
|
951
|
+
/// 常驻它会拖慢 UI 线程,让热键看起来"慢半拍"。
|
|
952
|
+
/// </summary>
|
|
953
|
+
static void UpdateMouseHook()
|
|
954
|
+
{
|
|
955
|
+
try
|
|
956
|
+
{
|
|
957
|
+
if (mouseActions.Count > 0)
|
|
958
|
+
{
|
|
959
|
+
if (mouseHook == IntPtr.Zero) InstallMouseHook();
|
|
960
|
+
}
|
|
961
|
+
else if (mouseHook != IntPtr.Zero)
|
|
962
|
+
{
|
|
963
|
+
Native.UnhookWindowsHookEx(mouseHook);
|
|
964
|
+
mouseHook = IntPtr.Zero;
|
|
965
|
+
Log("mouse hook removed (keyboard only)");
|
|
966
|
+
}
|
|
967
|
+
}
|
|
968
|
+
catch (Exception ex) { Log("mouse hook update failed: " + ex.Message); }
|
|
969
|
+
}
|
|
970
|
+
|
|
971
|
+
// ---------------------------------------------------------------- tray
|
|
972
|
+
|
|
973
|
+
static void BuildTray()
|
|
974
|
+
{
|
|
975
|
+
tray = new NotifyIcon();
|
|
976
|
+
tray.Icon = SystemIcons.Application;
|
|
977
|
+
tray.Text = "DSH 折叠热键";
|
|
978
|
+
tray.Visible = true;
|
|
979
|
+
tray.DoubleClick += delegate { RunAction("toggle"); };
|
|
980
|
+
|
|
981
|
+
var menu = new ContextMenuStrip();
|
|
982
|
+
menu.Items.Add("切换 主窗 / 小窗(" + cfgToggle + ")", null, delegate { RunAction("toggle"); });
|
|
983
|
+
menu.Items.Add("小窗跟到鼠标(" + cfgFollow + ")", null, delegate { RunAction("follow"); });
|
|
984
|
+
menu.Items.Add(new ToolStripSeparator());
|
|
985
|
+
menu.Items.Add("设置热键…", null, delegate { ShowSettings(); });
|
|
986
|
+
menu.Items.Add("打开程序目录", null, delegate { TryStart("explorer.exe", BaseDir); });
|
|
987
|
+
menu.Items.Add(new ToolStripSeparator());
|
|
988
|
+
menu.Items.Add("退出", null, delegate { ExitApp(); });
|
|
989
|
+
tray.ContextMenuStrip = menu;
|
|
990
|
+
Log("tray ready");
|
|
991
|
+
}
|
|
992
|
+
|
|
993
|
+
static void ShowSettings()
|
|
994
|
+
{
|
|
995
|
+
using (var form = new SettingsForm())
|
|
996
|
+
{
|
|
997
|
+
if (form.ShowDialog() == DialogResult.OK)
|
|
998
|
+
{
|
|
999
|
+
SaveConfig(form.ToggleText, form.FollowText, form.QuitText, form.OffsetY);
|
|
1000
|
+
if (window != null) window.ApplyConfig();
|
|
1001
|
+
}
|
|
1002
|
+
}
|
|
1003
|
+
}
|
|
1004
|
+
|
|
1005
|
+
static void ShowToast(string text)
|
|
1006
|
+
{
|
|
1007
|
+
try
|
|
1008
|
+
{
|
|
1009
|
+
if (tray == null) return;
|
|
1010
|
+
tray.BalloonTipTitle = "DSH 折叠热键";
|
|
1011
|
+
tray.BalloonTipText = text;
|
|
1012
|
+
tray.BalloonTipIcon = ToolTipIcon.Info;
|
|
1013
|
+
tray.ShowBalloonTip(4000);
|
|
1014
|
+
}
|
|
1015
|
+
catch { }
|
|
1016
|
+
}
|
|
1017
|
+
|
|
1018
|
+
static void ExitApp()
|
|
1019
|
+
{
|
|
1020
|
+
Log("exit from tray");
|
|
1021
|
+
if (tray != null) { tray.Visible = false; tray.Dispose(); tray = null; }
|
|
1022
|
+
Application.Exit();
|
|
1023
|
+
}
|
|
1024
|
+
|
|
1025
|
+
static void TryStart(string file, string args)
|
|
1026
|
+
{
|
|
1027
|
+
try { Process.Start(file, args); }
|
|
1028
|
+
catch (Exception ex) { Log("start failed: " + ex.Message); }
|
|
1029
|
+
}
|
|
1030
|
+
|
|
1031
|
+
// ---------------------------------------------------------------- hotkey window
|
|
1032
|
+
|
|
1033
|
+
sealed class HotkeyWindow : NativeWindow, IDisposable
|
|
1034
|
+
{
|
|
1035
|
+
public const int IdToggle = 1;
|
|
1036
|
+
public const int IdFollow = 2;
|
|
1037
|
+
public const int IdQuit = 3;
|
|
1038
|
+
public const int IdOpenMini = 4;
|
|
1039
|
+
|
|
1040
|
+
public HotkeyWindow()
|
|
1041
|
+
{
|
|
1042
|
+
CreateHandle(new CreateParams());
|
|
1043
|
+
}
|
|
1044
|
+
|
|
1045
|
+
public void ApplyConfig()
|
|
1046
|
+
{
|
|
1047
|
+
Native.UnregisterHotKey(Handle, IdToggle);
|
|
1048
|
+
Native.UnregisterHotKey(Handle, IdFollow);
|
|
1049
|
+
Native.UnregisterHotKey(Handle, IdQuit);
|
|
1050
|
+
Native.UnregisterHotKey(Handle, IdOpenMini);
|
|
1051
|
+
mouseActions.Clear();
|
|
1052
|
+
Bind("toggle", IdToggle, cfgToggle);
|
|
1053
|
+
Bind("follow", IdFollow, cfgFollow);
|
|
1054
|
+
Bind("quit", IdQuit, cfgQuit);
|
|
1055
|
+
Bind("openmini", IdOpenMini, cfgOpenMini);
|
|
1056
|
+
UpdateMouseHook();
|
|
1057
|
+
}
|
|
1058
|
+
|
|
1059
|
+
void Bind(string action, int id, string raw)
|
|
1060
|
+
{
|
|
1061
|
+
int button;
|
|
1062
|
+
if (TryParseMouse(raw, out button))
|
|
1063
|
+
{
|
|
1064
|
+
mouseActions[button] = action;
|
|
1065
|
+
Log(string.Format("mouse {0}='{1}' bound", action, raw));
|
|
1066
|
+
return;
|
|
1067
|
+
}
|
|
1068
|
+
Register(action, id, raw);
|
|
1069
|
+
}
|
|
1070
|
+
|
|
1071
|
+
void Register(string action, int id, string raw)
|
|
1072
|
+
{
|
|
1073
|
+
uint mods, vk;
|
|
1074
|
+
string error;
|
|
1075
|
+
if (!TryParseHotkey(raw, out mods, out vk, out error))
|
|
1076
|
+
{
|
|
1077
|
+
if (error != "empty") Log(string.Format("hotkey {0}='{1}' invalid: {2}", action, raw, error));
|
|
1078
|
+
else Log(string.Format("hotkey {0} disabled", action));
|
|
1079
|
+
return;
|
|
1080
|
+
}
|
|
1081
|
+
var ok = Native.RegisterHotKey(Handle, id, mods, vk);
|
|
1082
|
+
Log(string.Format("hotkey {0}='{1}' registered={2}", action, raw, ok));
|
|
1083
|
+
}
|
|
1084
|
+
|
|
1085
|
+
protected override void WndProc(ref Message m)
|
|
1086
|
+
{
|
|
1087
|
+
if (m.Msg == Native.WM_HOTKEY)
|
|
1088
|
+
{
|
|
1089
|
+
try
|
|
1090
|
+
{
|
|
1091
|
+
var id = m.WParam.ToInt32();
|
|
1092
|
+
Log("hotkey received id=" + id);
|
|
1093
|
+
if (id == IdToggle) RunAction("toggle");
|
|
1094
|
+
else if (id == IdFollow) RunAction("follow");
|
|
1095
|
+
else if (id == IdQuit) ExitApp();
|
|
1096
|
+
else if (id == IdOpenMini) RunAction("openmini");
|
|
1097
|
+
}
|
|
1098
|
+
catch (Exception ex) { Log("hotkey error: " + ex.Message); }
|
|
1099
|
+
}
|
|
1100
|
+
base.WndProc(ref m);
|
|
1101
|
+
}
|
|
1102
|
+
|
|
1103
|
+
public void Dispose()
|
|
1104
|
+
{
|
|
1105
|
+
Native.UnregisterHotKey(Handle, IdToggle);
|
|
1106
|
+
Native.UnregisterHotKey(Handle, IdFollow);
|
|
1107
|
+
Native.UnregisterHotKey(Handle, IdQuit);
|
|
1108
|
+
Native.UnregisterHotKey(Handle, IdOpenMini);
|
|
1109
|
+
DestroyHandle();
|
|
1110
|
+
}
|
|
1111
|
+
}
|
|
1112
|
+
|
|
1113
|
+
// ---------------------------------------------------------------- settings form
|
|
1114
|
+
|
|
1115
|
+
sealed class SettingsForm : Form
|
|
1116
|
+
{
|
|
1117
|
+
readonly TextBox toggleBox;
|
|
1118
|
+
readonly TextBox followBox;
|
|
1119
|
+
readonly TextBox quitBox;
|
|
1120
|
+
readonly TextBox offsetBox;
|
|
1121
|
+
|
|
1122
|
+
public string ToggleText { get { return toggleBox.Text.Trim(); } }
|
|
1123
|
+
public string FollowText { get { return followBox.Text.Trim(); } }
|
|
1124
|
+
public string QuitText { get { return quitBox.Text.Trim(); } }
|
|
1125
|
+
public int OffsetY
|
|
1126
|
+
{
|
|
1127
|
+
get
|
|
1128
|
+
{
|
|
1129
|
+
int value;
|
|
1130
|
+
return int.TryParse(offsetBox.Text.Trim(), out value) ? value : 24;
|
|
1131
|
+
}
|
|
1132
|
+
}
|
|
1133
|
+
|
|
1134
|
+
public SettingsForm()
|
|
1135
|
+
{
|
|
1136
|
+
Text = "DSH 折叠热键 · 设置";
|
|
1137
|
+
FormBorderStyle = FormBorderStyle.FixedDialog;
|
|
1138
|
+
StartPosition = FormStartPosition.CenterScreen;
|
|
1139
|
+
MaximizeBox = false;
|
|
1140
|
+
MinimizeBox = false;
|
|
1141
|
+
ClientSize = new Size(460, 262);
|
|
1142
|
+
Font = new Font("Microsoft YaHei UI", 9f);
|
|
1143
|
+
|
|
1144
|
+
AddRow("切换 主窗 / 小窗", 16, out toggleBox, cfgToggle);
|
|
1145
|
+
AddRow("小窗跟随鼠标", 56, out followBox, cfgFollow);
|
|
1146
|
+
AddRow("退出热键", 96, out quitBox, cfgQuit);
|
|
1147
|
+
|
|
1148
|
+
var offsetLabel = new Label();
|
|
1149
|
+
offsetLabel.Text = "跟随纵向偏移";
|
|
1150
|
+
offsetLabel.SetBounds(16, 138, 120, 22);
|
|
1151
|
+
Controls.Add(offsetLabel);
|
|
1152
|
+
|
|
1153
|
+
offsetBox = new TextBox();
|
|
1154
|
+
offsetBox.Text = cfgOffsetY.ToString();
|
|
1155
|
+
offsetBox.SetBounds(150, 135, 70, 24);
|
|
1156
|
+
Controls.Add(offsetBox);
|
|
1157
|
+
|
|
1158
|
+
var hint = new Label();
|
|
1159
|
+
hint.Text = "写法:Ctrl/Alt/Shift/Win + 按键(如 Alt+`、Ctrl+Alt+D、Alt+F9);鼠标侧键直接写 XButton1 / XButton2。留空 = 关闭该热键。";
|
|
1160
|
+
hint.SetBounds(16, 170, 430, 36);
|
|
1161
|
+
hint.ForeColor = Color.Gray;
|
|
1162
|
+
Controls.Add(hint);
|
|
1163
|
+
|
|
1164
|
+
var save = new Button();
|
|
1165
|
+
save.Text = "保存并生效";
|
|
1166
|
+
save.SetBounds(210, 214, 110, 30);
|
|
1167
|
+
save.Click += delegate
|
|
1168
|
+
{
|
|
1169
|
+
string error;
|
|
1170
|
+
Log(string.Format("settings save: toggle='{0}' follow='{1}' quit='{2}' offset='{3}'",
|
|
1171
|
+
toggleBox.Text, followBox.Text, quitBox.Text, offsetBox.Text));
|
|
1172
|
+
if (!Validate(toggleBox.Text, out error)) { Fail("「切换 主窗/小窗」", error); return; }
|
|
1173
|
+
if (!Validate(followBox.Text, out error)) { Fail("「小窗跟随鼠标」", error); return; }
|
|
1174
|
+
if (!Validate(quitBox.Text, out error)) { Fail("「退出热键」", error); return; }
|
|
1175
|
+
DialogResult = DialogResult.OK;
|
|
1176
|
+
Close();
|
|
1177
|
+
};
|
|
1178
|
+
Controls.Add(save);
|
|
1179
|
+
AcceptButton = save;
|
|
1180
|
+
|
|
1181
|
+
var defaults = new Button();
|
|
1182
|
+
defaults.Text = "恢复默认";
|
|
1183
|
+
defaults.SetBounds(330, 214, 80, 30);
|
|
1184
|
+
defaults.Click += delegate
|
|
1185
|
+
{
|
|
1186
|
+
toggleBox.Text = DefaultToggle;
|
|
1187
|
+
followBox.Text = DefaultFollow;
|
|
1188
|
+
quitBox.Text = DefaultQuit;
|
|
1189
|
+
offsetBox.Text = "24";
|
|
1190
|
+
};
|
|
1191
|
+
Controls.Add(defaults);
|
|
1192
|
+
|
|
1193
|
+
var cancel = new Button();
|
|
1194
|
+
cancel.Text = "取消";
|
|
1195
|
+
cancel.SetBounds(120, 214, 80, 30);
|
|
1196
|
+
cancel.DialogResult = DialogResult.Cancel;
|
|
1197
|
+
Controls.Add(cancel);
|
|
1198
|
+
CancelButton = cancel;
|
|
1199
|
+
}
|
|
1200
|
+
|
|
1201
|
+
bool Validate(string raw, out string error)
|
|
1202
|
+
{
|
|
1203
|
+
return ValidateText(raw, out error);
|
|
1204
|
+
}
|
|
1205
|
+
|
|
1206
|
+
static void Fail(string field, string error)
|
|
1207
|
+
{
|
|
1208
|
+
MessageBox.Show(field + ":" + error, "热键写法有问题", MessageBoxButtons.OK, MessageBoxIcon.Warning);
|
|
1209
|
+
}
|
|
1210
|
+
|
|
1211
|
+
void AddRow(string label, int top, out TextBox box, string value)
|
|
1212
|
+
{
|
|
1213
|
+
var text = new Label();
|
|
1214
|
+
text.Text = label;
|
|
1215
|
+
text.SetBounds(16, top + 3, 120, 22);
|
|
1216
|
+
Controls.Add(text);
|
|
1217
|
+
box = new TextBox();
|
|
1218
|
+
box.Text = value;
|
|
1219
|
+
box.SetBounds(150, top, 290, 24);
|
|
1220
|
+
Controls.Add(box);
|
|
1221
|
+
}
|
|
1222
|
+
}
|
|
1223
|
+
|
|
1224
|
+
// ---------------------------------------------------------------- main
|
|
1225
|
+
|
|
1226
|
+
[STAThread]
|
|
1227
|
+
static void Main(string[] args)
|
|
1228
|
+
{
|
|
1229
|
+
try { Native.SetProcessDpiAwarenessContext(new IntPtr(-4)); } catch { try { Native.SetProcessDPIAware(); } catch { } }
|
|
1230
|
+
EnsureConfig();
|
|
1231
|
+
LoadConfig();
|
|
1232
|
+
|
|
1233
|
+
var mode = args.Length > 0 ? args[0].ToLowerInvariant() : "";
|
|
1234
|
+
|
|
1235
|
+
if (mode == "--status")
|
|
1236
|
+
{
|
|
1237
|
+
var main = FindMain();
|
|
1238
|
+
var mini = FindMini();
|
|
1239
|
+
Log(string.Format("status: main={0} '{1}' visible={2}; mini={3} '{4}' visible={5}",
|
|
1240
|
+
main, main == IntPtr.Zero ? "" : TitleOf(main), main != IntPtr.Zero && Native.IsWindowVisible(main),
|
|
1241
|
+
mini, mini == IntPtr.Zero ? "" : TitleOf(mini), mini != IntPtr.Zero && Native.IsWindowVisible(mini)));
|
|
1242
|
+
return;
|
|
1243
|
+
}
|
|
1244
|
+
if (mode == "--selftest")
|
|
1245
|
+
{
|
|
1246
|
+
using (var probe = new SettingsForm())
|
|
1247
|
+
{
|
|
1248
|
+
string e1, e2, e3;
|
|
1249
|
+
var ok1 = ValidateText(probe.ToggleText, out e1);
|
|
1250
|
+
var ok2 = ValidateText(probe.FollowText, out e2);
|
|
1251
|
+
var ok3 = ValidateText(probe.QuitText, out e3);
|
|
1252
|
+
Log(string.Format("selftest toggle='{0}' ok={1} {2}", probe.ToggleText, ok1, e1));
|
|
1253
|
+
Log(string.Format("selftest follow='{0}' ok={1} {2}", probe.FollowText, ok2, e2));
|
|
1254
|
+
Log(string.Format("selftest quit='{0}' ok={1} {2}", probe.QuitText, ok3, e3));
|
|
1255
|
+
}
|
|
1256
|
+
return;
|
|
1257
|
+
}
|
|
1258
|
+
if (mode == "--toggle")
|
|
1259
|
+
{
|
|
1260
|
+
ToggleCore();
|
|
1261
|
+
for (int i = 0; i < 50 && opening; i++) System.Threading.Thread.Sleep(200);
|
|
1262
|
+
return;
|
|
1263
|
+
}
|
|
1264
|
+
if (mode == "--follow") { FollowCore(); return; }
|
|
1265
|
+
|
|
1266
|
+
bool created;
|
|
1267
|
+
using (var mutex = new Mutex(true, "Local\\DshHudHotkey", out created))
|
|
1268
|
+
{
|
|
1269
|
+
if (!created) return;
|
|
1270
|
+
Application.EnableVisualStyles();
|
|
1271
|
+
RaiseProcessPriority();
|
|
1272
|
+
Log("start; config: toggle=" + cfgToggle + ", follow=" + cfgFollow + ", quit=" + cfgQuit);
|
|
1273
|
+
BuildTray();
|
|
1274
|
+
dispatcher = new Control();
|
|
1275
|
+
StartWorker();
|
|
1276
|
+
BuildWhale();
|
|
1277
|
+
followTimer = new System.Windows.Forms.Timer();
|
|
1278
|
+
followTimer.Interval = 16;
|
|
1279
|
+
followTimer.Tick += FollowTick;
|
|
1280
|
+
followTimer.Start();
|
|
1281
|
+
var dispatcherHandle = dispatcher.Handle;
|
|
1282
|
+
using (window = new HotkeyWindow())
|
|
1283
|
+
{
|
|
1284
|
+
window.ApplyConfig();
|
|
1285
|
+
var stamp = File.Exists(ConfigPath) ? File.GetLastWriteTimeUtc(ConfigPath) : DateTime.MinValue;
|
|
1286
|
+
var timer = new System.Windows.Forms.Timer();
|
|
1287
|
+
timer.Interval = 2000;
|
|
1288
|
+
timer.Tick += delegate
|
|
1289
|
+
{
|
|
1290
|
+
try
|
|
1291
|
+
{
|
|
1292
|
+
var now = File.Exists(ConfigPath) ? File.GetLastWriteTimeUtc(ConfigPath) : DateTime.MinValue;
|
|
1293
|
+
if (now == stamp) return;
|
|
1294
|
+
stamp = now;
|
|
1295
|
+
LoadConfig();
|
|
1296
|
+
Log("config changed: toggle=" + cfgToggle + ", follow=" + cfgFollow + ", quit=" + cfgQuit);
|
|
1297
|
+
window.ApplyConfig();
|
|
1298
|
+
}
|
|
1299
|
+
catch (Exception ex) { Log("reload failed: " + ex.Message); }
|
|
1300
|
+
};
|
|
1301
|
+
timer.Start();
|
|
1302
|
+
Application.Run(new ApplicationContext());
|
|
1303
|
+
timer.Stop();
|
|
1304
|
+
}
|
|
1305
|
+
if (mouseHook != IntPtr.Zero) { Native.UnhookWindowsHookEx(mouseHook); mouseHook = IntPtr.Zero; }
|
|
1306
|
+
Log("stopped");
|
|
1307
|
+
}
|
|
1308
|
+
}
|
|
1309
|
+
}
|