claude-hook-notify 1.3.0 → 1.3.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/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "claude-hook-notify",
|
|
3
|
-
"version": "1.3.
|
|
3
|
+
"version": "1.3.1",
|
|
4
4
|
"description": "🔔 Claude Code 任务完成桌面通知 — 一键安装,跨平台支持",
|
|
5
5
|
"main": "src/index.js",
|
|
6
6
|
"bin": {
|
|
@@ -18,8 +18,16 @@
|
|
|
18
18
|
"notify-send",
|
|
19
19
|
"cli"
|
|
20
20
|
],
|
|
21
|
-
"author": "",
|
|
21
|
+
"author": "Mr-zj388",
|
|
22
22
|
"license": "MIT",
|
|
23
|
+
"repository": {
|
|
24
|
+
"type": "git",
|
|
25
|
+
"url": "https://github.com/Mr-zj388/claude-code-notify.git"
|
|
26
|
+
},
|
|
27
|
+
"homepage": "https://github.com/Mr-zj388/claude-code-notify#readme",
|
|
28
|
+
"bugs": {
|
|
29
|
+
"url": "https://github.com/Mr-zj388/claude-code-notify/issues"
|
|
30
|
+
},
|
|
23
31
|
"engines": {
|
|
24
32
|
"node": ">=16.0.0"
|
|
25
33
|
},
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* 后台点击监听脚本
|
|
5
|
+
*
|
|
6
|
+
* 由主进程 spawn 为 detached 后台进程。
|
|
7
|
+
* 执行通知命令(带等待标志),检测用户点击后激活终端窗口。
|
|
8
|
+
*
|
|
9
|
+
* 参数格式:
|
|
10
|
+
* node activate-watcher.js <JSON配置>
|
|
11
|
+
*
|
|
12
|
+
* JSON 配置:
|
|
13
|
+
* {
|
|
14
|
+
* "platform": "win32" | "linux",
|
|
15
|
+
* "notifyCommand": "...",
|
|
16
|
+
* "notifyArgs": ["..."],
|
|
17
|
+
* "activateCommand": "...",
|
|
18
|
+
* "activateArgs": ["..."],
|
|
19
|
+
* "activateFallbackCommand": "...", // 可选
|
|
20
|
+
* "activateFallbackArgs": ["..."], // 可选
|
|
21
|
+
* "timeout": 120000
|
|
22
|
+
* }
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
const { spawn } = require("child_process");
|
|
26
|
+
|
|
27
|
+
// 120 秒超时自动退出,防僵尸进程
|
|
28
|
+
const config = parseConfig();
|
|
29
|
+
const selfTimeout = setTimeout(() => process.exit(0), config.timeout || 120000);
|
|
30
|
+
selfTimeout.unref();
|
|
31
|
+
|
|
32
|
+
function parseConfig() {
|
|
33
|
+
try {
|
|
34
|
+
return JSON.parse(process.argv[2] || "{}");
|
|
35
|
+
} catch {
|
|
36
|
+
process.exit(1);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function activateTerminal() {
|
|
41
|
+
const { activateCommand, activateArgs, activateFallbackCommand, activateFallbackArgs } = config;
|
|
42
|
+
if (!activateCommand) return;
|
|
43
|
+
|
|
44
|
+
try {
|
|
45
|
+
execFileSync(activateCommand, activateArgs || [], { stdio: "ignore", timeout: 5000 });
|
|
46
|
+
} catch {
|
|
47
|
+
// 主命令失败,尝试回退
|
|
48
|
+
if (activateFallbackCommand) {
|
|
49
|
+
try {
|
|
50
|
+
execFileSync(activateFallbackCommand, activateFallbackArgs || [], { stdio: "ignore", timeout: 5000 });
|
|
51
|
+
} catch {
|
|
52
|
+
// 回退也失败,静默退出
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
async function main() {
|
|
59
|
+
const { platform, notifyCommand, notifyArgs } = config;
|
|
60
|
+
if (!notifyCommand) process.exit(1);
|
|
61
|
+
|
|
62
|
+
if (platform === "linux") {
|
|
63
|
+
// notify-send --action 模式:stdout 输出 action 名
|
|
64
|
+
const child = spawn(notifyCommand, notifyArgs, {
|
|
65
|
+
stdio: ["ignore", "pipe", "ignore"],
|
|
66
|
+
timeout: 120000,
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
let output = "";
|
|
70
|
+
child.stdout.on("data", (chunk) => {
|
|
71
|
+
output += chunk.toString();
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
child.on("close", () => {
|
|
75
|
+
if (output.trim() === "default") {
|
|
76
|
+
activateTerminal();
|
|
77
|
+
}
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
main().catch(() => process.exit(1));
|
package/src/activate.js
ADDED
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
const { execSync, execFileSync } = require("child_process");
|
|
2
|
+
const os = require("os");
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* 终端进程名到 macOS bundleId 的映射
|
|
6
|
+
*/
|
|
7
|
+
const MAC_BUNDLE_MAP = {
|
|
8
|
+
"Terminal": "com.apple.Terminal",
|
|
9
|
+
"iTerm2": "com.googlecode.iterm2",
|
|
10
|
+
"iTerm2-v3": "com.googlecode.iterm2",
|
|
11
|
+
"Electron": null, // 需进一步判断
|
|
12
|
+
"Code": "com.microsoft.VSCode",
|
|
13
|
+
"Code - Insiders": "com.microsoft.VSCodeInsiders",
|
|
14
|
+
"Cursor": "com.todesktop.230313mzl4w4u92",
|
|
15
|
+
"WarpTerminal": "dev.warp.Warp-Stable",
|
|
16
|
+
"Alacritty": "org.alacritty",
|
|
17
|
+
"kitty": "net.kovidgoyal.kitty",
|
|
18
|
+
"Hyper": "co.zeit.hyper",
|
|
19
|
+
"Tabby": "org.tabby",
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Windows 终端进程名(小写,不含 .exe)
|
|
24
|
+
*/
|
|
25
|
+
const WIN_TERMINAL_NAMES = new Set([
|
|
26
|
+
"windowsterminal",
|
|
27
|
+
"code",
|
|
28
|
+
"cursor",
|
|
29
|
+
"claude",
|
|
30
|
+
"cmd",
|
|
31
|
+
"powershell",
|
|
32
|
+
"pwsh",
|
|
33
|
+
"mintty",
|
|
34
|
+
"alacritty",
|
|
35
|
+
"wezterm-gui",
|
|
36
|
+
"hyper",
|
|
37
|
+
"tabby",
|
|
38
|
+
"conhost",
|
|
39
|
+
]);
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Linux 终端进程名
|
|
43
|
+
*/
|
|
44
|
+
const LINUX_TERMINALS = new Set([
|
|
45
|
+
"gnome-terminal-server",
|
|
46
|
+
"gnome-terminal",
|
|
47
|
+
"konsole",
|
|
48
|
+
"xfce4-terminal",
|
|
49
|
+
"mate-terminal",
|
|
50
|
+
"tilix",
|
|
51
|
+
"terminator",
|
|
52
|
+
"alacritty",
|
|
53
|
+
"kitty",
|
|
54
|
+
"wezterm-gui",
|
|
55
|
+
"foot",
|
|
56
|
+
"code",
|
|
57
|
+
"cursor",
|
|
58
|
+
"hyper",
|
|
59
|
+
"tabby",
|
|
60
|
+
]);
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* 沿进程树向上查找终端进程
|
|
64
|
+
*/
|
|
65
|
+
function detectTerminal() {
|
|
66
|
+
const platform = os.platform();
|
|
67
|
+
try {
|
|
68
|
+
if (platform === "darwin") return detectMac();
|
|
69
|
+
if (platform === "win32") return detectWindows();
|
|
70
|
+
if (platform === "linux") return detectLinux();
|
|
71
|
+
} catch {
|
|
72
|
+
// 检测失败
|
|
73
|
+
}
|
|
74
|
+
return null;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
function detectMac() {
|
|
78
|
+
let pid = process.ppid;
|
|
79
|
+
for (let i = 0; i < 20 && pid > 1; i++) {
|
|
80
|
+
try {
|
|
81
|
+
const info = execSync(`ps -o ppid=,comm= -p ${pid}`, { encoding: "utf-8" }).trim();
|
|
82
|
+
const match = info.match(/^\s*(\d+)\s+(.+)$/);
|
|
83
|
+
if (!match) break;
|
|
84
|
+
const ppid = parseInt(match[1], 10);
|
|
85
|
+
const comm = match[2].trim().split("/").pop();
|
|
86
|
+
|
|
87
|
+
if (MAC_BUNDLE_MAP[comm] !== undefined) {
|
|
88
|
+
const bundleId = MAC_BUNDLE_MAP[comm];
|
|
89
|
+
if (bundleId) {
|
|
90
|
+
return { name: comm, bundleId, pid, processName: comm };
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
pid = ppid;
|
|
94
|
+
} catch {
|
|
95
|
+
break;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
// 回退:检查 TERM_PROGRAM 环境变量
|
|
99
|
+
const termProgram = process.env.TERM_PROGRAM;
|
|
100
|
+
if (termProgram) {
|
|
101
|
+
const map = {
|
|
102
|
+
"Apple_Terminal": { name: "Terminal", bundleId: "com.apple.Terminal" },
|
|
103
|
+
"iTerm.app": { name: "iTerm2", bundleId: "com.googlecode.iterm2" },
|
|
104
|
+
"vscode": { name: "Code", bundleId: "com.microsoft.VSCode" },
|
|
105
|
+
"WarpTerminal": { name: "Warp", bundleId: "dev.warp.Warp-Stable" },
|
|
106
|
+
"Hyper": { name: "Hyper", bundleId: "co.zeit.hyper" },
|
|
107
|
+
};
|
|
108
|
+
if (map[termProgram]) {
|
|
109
|
+
return { ...map[termProgram], pid: 0, processName: termProgram };
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
return null;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* Windows: 优先环境变量,回退单次 PowerShell 批量查询
|
|
117
|
+
*/
|
|
118
|
+
function detectWindows() {
|
|
119
|
+
// 1. 环境变量快速检测(仅保留精确 PID 的路径)
|
|
120
|
+
if (process.env.VSCODE_PID) {
|
|
121
|
+
const pid = parseInt(process.env.VSCODE_PID, 10);
|
|
122
|
+
if (pid > 0) return { name: "Code", pid, processName: "code.exe" };
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
// 2. WMI 遍历进程树(OS 级 ParentProcessId 始终完整,不受 spawn 方式影响)
|
|
126
|
+
try {
|
|
127
|
+
const script = [
|
|
128
|
+
"$cpid=" + process.pid,
|
|
129
|
+
"for($i=0;$i -lt 30 -and $cpid -gt 0;$i++){",
|
|
130
|
+
" $w=Get-CimInstance Win32_Process -Filter \"ProcessId=$cpid\" -EA SilentlyContinue",
|
|
131
|
+
" if(-not $w){break}",
|
|
132
|
+
" Write-Output \"$($w.Name)|$($w.ProcessId)|$($w.ParentProcessId)\"",
|
|
133
|
+
" $cpid=$w.ParentProcessId",
|
|
134
|
+
"}",
|
|
135
|
+
].join(";");
|
|
136
|
+
const output = execFileSync("powershell", ["-NoProfile", "-Command", script], {
|
|
137
|
+
encoding: "utf-8",
|
|
138
|
+
timeout: 8000,
|
|
139
|
+
}).trim();
|
|
140
|
+
|
|
141
|
+
// 遍历整棵进程树,取最高层级(最靠近根)的终端匹配
|
|
142
|
+
// 避免误取 powershell/cmd 等 shell 进程 —— 它们在 Windows Terminal 下不拥有窗口
|
|
143
|
+
let bestMatch = null;
|
|
144
|
+
for (const line of output.split("\n")) {
|
|
145
|
+
const parts = line.trim().split("|");
|
|
146
|
+
if (parts.length < 3) continue;
|
|
147
|
+
// WMI 返回的 Name 带 .exe 后缀,去掉再匹配
|
|
148
|
+
const name = parts[0].replace(/\.exe$/i, "").toLowerCase();
|
|
149
|
+
const pid = parseInt(parts[1], 10);
|
|
150
|
+
if (WIN_TERMINAL_NAMES.has(name)) {
|
|
151
|
+
bestMatch = { name: parts[0].replace(/\.exe$/i, ""), pid, processName: parts[0] };
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
return bestMatch;
|
|
155
|
+
} catch {
|
|
156
|
+
// PowerShell 调用失败
|
|
157
|
+
}
|
|
158
|
+
return null;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
function detectLinux() {
|
|
162
|
+
// 1. 环境变量快速检测
|
|
163
|
+
if (process.env.VSCODE_PID) {
|
|
164
|
+
const pid = parseInt(process.env.VSCODE_PID, 10);
|
|
165
|
+
if (pid > 0) return { name: "code", pid, processName: "code" };
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
// 2. 进程树遍历
|
|
169
|
+
let pid = process.ppid;
|
|
170
|
+
for (let i = 0; i < 20 && pid > 1; i++) {
|
|
171
|
+
try {
|
|
172
|
+
const info = execSync(`ps -o ppid=,comm= -p ${pid}`, { encoding: "utf-8" }).trim();
|
|
173
|
+
const match = info.match(/^\s*(\d+)\s+(.+)$/);
|
|
174
|
+
if (!match) break;
|
|
175
|
+
const ppid = parseInt(match[1], 10);
|
|
176
|
+
const comm = match[2].trim();
|
|
177
|
+
|
|
178
|
+
if (LINUX_TERMINALS.has(comm)) {
|
|
179
|
+
return { name: comm, pid, processName: comm };
|
|
180
|
+
}
|
|
181
|
+
pid = ppid;
|
|
182
|
+
} catch {
|
|
183
|
+
break;
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
return null;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* 获取激活终端窗口的命令和参数
|
|
191
|
+
*/
|
|
192
|
+
function getActivateCommand(terminalInfo) {
|
|
193
|
+
if (!terminalInfo) return null;
|
|
194
|
+
const platform = os.platform();
|
|
195
|
+
|
|
196
|
+
if (platform === "darwin") {
|
|
197
|
+
if (terminalInfo.bundleId) {
|
|
198
|
+
return {
|
|
199
|
+
command: "osascript",
|
|
200
|
+
args: ["-e", `tell application id "${terminalInfo.bundleId}" to activate`],
|
|
201
|
+
};
|
|
202
|
+
}
|
|
203
|
+
return null;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
// Windows 使用 notify-helper.exe 自行处理激活,不需要此函数
|
|
207
|
+
|
|
208
|
+
if (platform === "linux") {
|
|
209
|
+
return {
|
|
210
|
+
command: "xdotool",
|
|
211
|
+
args: ["search", "--pid", String(terminalInfo.pid), "--onlyvisible", "windowactivate"],
|
|
212
|
+
fallbackCommand: "wmctrl",
|
|
213
|
+
fallbackArgs: ["-ia", String(terminalInfo.pid)],
|
|
214
|
+
};
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
return null;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
module.exports = { detectTerminal, getActivateCommand };
|
|
Binary file
|
|
@@ -0,0 +1,222 @@
|
|
|
1
|
+
using System;
|
|
2
|
+
using System.Diagnostics;
|
|
3
|
+
using System.Drawing;
|
|
4
|
+
using System.IO;
|
|
5
|
+
using System.Runtime.InteropServices;
|
|
6
|
+
using System.Windows.Forms;
|
|
7
|
+
|
|
8
|
+
[assembly: System.Reflection.AssemblyTitle("Claude Code 完成通知")]
|
|
9
|
+
[assembly: System.Reflection.AssemblyProduct("Claude Code Notify")]
|
|
10
|
+
|
|
11
|
+
class NotifyHelper
|
|
12
|
+
{
|
|
13
|
+
[DllImport("user32.dll")]
|
|
14
|
+
static extern bool SetForegroundWindow(IntPtr hWnd);
|
|
15
|
+
|
|
16
|
+
[DllImport("user32.dll")]
|
|
17
|
+
static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
|
|
18
|
+
|
|
19
|
+
delegate bool EnumWindowsProc(IntPtr hWnd, IntPtr lParam);
|
|
20
|
+
|
|
21
|
+
[DllImport("user32.dll")]
|
|
22
|
+
static extern bool EnumWindows(EnumWindowsProc lpEnumFunc, IntPtr lParam);
|
|
23
|
+
|
|
24
|
+
[DllImport("user32.dll")]
|
|
25
|
+
static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId);
|
|
26
|
+
|
|
27
|
+
[DllImport("user32.dll")]
|
|
28
|
+
static extern bool IsWindowVisible(IntPtr hWnd);
|
|
29
|
+
|
|
30
|
+
[DllImport("user32.dll", SetLastError = true)]
|
|
31
|
+
static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
|
|
32
|
+
|
|
33
|
+
[DllImport("user32.dll")]
|
|
34
|
+
static extern bool AttachThreadInput(uint idAttach, uint idAttachTo, bool fAttach);
|
|
35
|
+
|
|
36
|
+
[DllImport("user32.dll")]
|
|
37
|
+
static extern IntPtr GetForegroundWindow();
|
|
38
|
+
|
|
39
|
+
[DllImport("kernel32.dll")]
|
|
40
|
+
static extern uint GetCurrentThreadId();
|
|
41
|
+
|
|
42
|
+
// 已知 GUI 终端的窗口类名(用于 PID 查找失败时的回退)
|
|
43
|
+
static string[] KnownWindowClasses = new string[] {
|
|
44
|
+
"CASCADIA_HOSTING_WINDOW_CLASS", // Windows Terminal
|
|
45
|
+
"mintty", // Git Bash / mintty
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
// Claude sparkle 图标 PNG(内嵌,无需外部文件)
|
|
49
|
+
static string IconBase64 = "iVBORw0KGgoAAAANSUhEUgAAAIAAAACACAMAAAD04JH5AAAAFVBMVEXZdlXabkzaaEP99vT66uXtw7jimoUHIS5GAAALV0lEQVR4nO1ai3bcuA4TaUv//8nXEkkQlDxNuo+T3XvWadMZP0QQACnZbmv/bd/axk8DkJ8G8Ge2P8Xej1P/3/ZHt3+CdD+O4V9duXX7cS4/bf9YYP+vm0pT1R8E0O/77v3Hikv79QC4r/sdgfTx9yKTcdt29ddAD7IP0P6q7cZ2pvqguyY5f6NBtCeAfsQRk+d+J+cvB3BQveRZFPzR4WUO+Uv0DOAauh+8ZvzHHt+M9rpH4uMrEB03U8BniRXI9dGfsgf9nKrUT8I7b6ZAyili+S8AX7ngqzOk1bHzQGhwkdnEr7hdgkMbpdbJ452fjz07Dhk7BXSGW/Da0Y/eZ3/aNCg8E49Ewe7ylU2/wusP10qeWV1gccDDizTzZgFL6ZY8pQmfcOokLV0wERhMG1qHw/JGFAFvaw63IlalQeo+wV8ioXAz7hjySgqaAVg/hZfmsvhuHp6cyOEz7mkCu8JFuLwSceFIYRAiekP2bimBCgEFU7FMYUmbj1lc0KINXFEE6wqAterk4dmp0spXYml9UeHzreVfbkSFXCvZuXtAPss/GKh2f4FyMuCij8HXivvw2tPtl4XDcM6/E1DkSpE2vl9qxSp/pLBQ24cVCQALUpegZARN1rZKL9oUzn9l/xudZyII36yme3k8eN73dB9HRyY/f60B2E0clxoAhLAP6HxXT4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|
51
|
+
static IntPtr targetHandle = IntPtr.Zero;
|
|
52
|
+
static NotifyIcon notify;
|
|
53
|
+
|
|
54
|
+
static void Main(string[] args)
|
|
55
|
+
{
|
|
56
|
+
string title = args.Length > 0 ? args[0] : "Claude Code";
|
|
57
|
+
string message = args.Length > 1 ? args[1] : "任务完成";
|
|
58
|
+
string targetName = args.Length > 2 ? args[2] : "";
|
|
59
|
+
int targetPid = 0;
|
|
60
|
+
if (args.Length > 3) int.TryParse(args[3], out targetPid);
|
|
61
|
+
|
|
62
|
+
// 策略 1:通过 PID 精确定位终端窗口(多实例场景)
|
|
63
|
+
if (targetPid > 0)
|
|
64
|
+
{
|
|
65
|
+
try
|
|
66
|
+
{
|
|
67
|
+
using (var proc = Process.GetProcessById(targetPid))
|
|
68
|
+
{
|
|
69
|
+
if (proc.MainWindowHandle != IntPtr.Zero)
|
|
70
|
+
{
|
|
71
|
+
targetHandle = proc.MainWindowHandle;
|
|
72
|
+
}
|
|
73
|
+
else
|
|
74
|
+
{
|
|
75
|
+
// MainWindowHandle 可能为零(如 Windows Terminal),用 EnumWindows 查找
|
|
76
|
+
targetHandle = FindWindowByPid(targetPid);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
catch { /* 进程可能已退出,回退到名称查找 */ }
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
// 策略 2:通过进程名查找(回退方案)
|
|
84
|
+
if (targetHandle == IntPtr.Zero && !string.IsNullOrEmpty(targetName))
|
|
85
|
+
{
|
|
86
|
+
foreach (var name in targetName.Split(','))
|
|
87
|
+
{
|
|
88
|
+
try
|
|
89
|
+
{
|
|
90
|
+
var procs = Process.GetProcessesByName(name.Trim());
|
|
91
|
+
try
|
|
92
|
+
{
|
|
93
|
+
foreach (var p in procs)
|
|
94
|
+
{
|
|
95
|
+
if (p.MainWindowHandle != IntPtr.Zero)
|
|
96
|
+
{
|
|
97
|
+
targetHandle = p.MainWindowHandle;
|
|
98
|
+
break;
|
|
99
|
+
}
|
|
100
|
+
// MainWindowHandle 为零时尝试 EnumWindows 查找
|
|
101
|
+
IntPtr hwnd = FindWindowByPid(p.Id);
|
|
102
|
+
if (hwnd != IntPtr.Zero)
|
|
103
|
+
{
|
|
104
|
+
targetHandle = hwnd;
|
|
105
|
+
break;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
finally
|
|
110
|
+
{
|
|
111
|
+
foreach (var p in procs) p.Dispose();
|
|
112
|
+
}
|
|
113
|
+
if (targetHandle != IntPtr.Zero) break;
|
|
114
|
+
}
|
|
115
|
+
catch { }
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
// 策略 3:通过已知窗口类名查找(兜底方案)
|
|
120
|
+
if (targetHandle == IntPtr.Zero)
|
|
121
|
+
{
|
|
122
|
+
foreach (var cls in KnownWindowClasses)
|
|
123
|
+
{
|
|
124
|
+
IntPtr hwnd = FindWindow(cls, null);
|
|
125
|
+
if (hwnd != IntPtr.Zero)
|
|
126
|
+
{
|
|
127
|
+
targetHandle = hwnd;
|
|
128
|
+
break;
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
notify = new NotifyIcon();
|
|
134
|
+
notify.Icon = LoadEmbeddedIcon();
|
|
135
|
+
notify.Visible = true;
|
|
136
|
+
notify.BalloonTipTitle = title;
|
|
137
|
+
notify.BalloonTipText = message;
|
|
138
|
+
notify.BalloonTipIcon = ToolTipIcon.None;
|
|
139
|
+
|
|
140
|
+
notify.BalloonTipClicked += delegate { ActivateAndExit(); };
|
|
141
|
+
notify.Click += delegate { ActivateAndExit(); };
|
|
142
|
+
notify.BalloonTipClosed += delegate { Cleanup(); };
|
|
143
|
+
|
|
144
|
+
notify.ShowBalloonTip(15000);
|
|
145
|
+
|
|
146
|
+
var timer = new Timer();
|
|
147
|
+
timer.Interval = 60000;
|
|
148
|
+
timer.Tick += delegate { Cleanup(); };
|
|
149
|
+
timer.Start();
|
|
150
|
+
|
|
151
|
+
Application.Run();
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
static IntPtr FindWindowByPid(int pid)
|
|
155
|
+
{
|
|
156
|
+
IntPtr found = IntPtr.Zero;
|
|
157
|
+
EnumWindows((hWnd, lParam) =>
|
|
158
|
+
{
|
|
159
|
+
uint procId;
|
|
160
|
+
GetWindowThreadProcessId(hWnd, out procId);
|
|
161
|
+
if ((int)procId == pid && IsWindowVisible(hWnd))
|
|
162
|
+
{
|
|
163
|
+
found = hWnd;
|
|
164
|
+
return false;
|
|
165
|
+
}
|
|
166
|
+
return true;
|
|
167
|
+
}, IntPtr.Zero);
|
|
168
|
+
return found;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
static Icon LoadEmbeddedIcon()
|
|
172
|
+
{
|
|
173
|
+
try
|
|
174
|
+
{
|
|
175
|
+
var bytes = Convert.FromBase64String(IconBase64);
|
|
176
|
+
using (var ms = new MemoryStream(bytes))
|
|
177
|
+
{
|
|
178
|
+
var bmp = new Bitmap(ms);
|
|
179
|
+
var resized = new Bitmap(bmp, 32, 32);
|
|
180
|
+
return Icon.FromHandle(resized.GetHicon());
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
catch
|
|
184
|
+
{
|
|
185
|
+
return SystemIcons.Information;
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
static void ActivateAndExit()
|
|
190
|
+
{
|
|
191
|
+
if (targetHandle != IntPtr.Zero)
|
|
192
|
+
{
|
|
193
|
+
ShowWindow(targetHandle, 9); // SW_RESTORE
|
|
194
|
+
// 使用 AttachThreadInput 确保 SetForegroundWindow 成功
|
|
195
|
+
// (Windows 限制后台进程直接抢占前台焦点)
|
|
196
|
+
uint unusedPid;
|
|
197
|
+
uint targetThread = GetWindowThreadProcessId(targetHandle, out unusedPid);
|
|
198
|
+
uint curThread = GetCurrentThreadId();
|
|
199
|
+
if (curThread != targetThread)
|
|
200
|
+
{
|
|
201
|
+
AttachThreadInput(curThread, targetThread, true);
|
|
202
|
+
SetForegroundWindow(targetHandle);
|
|
203
|
+
AttachThreadInput(curThread, targetThread, false);
|
|
204
|
+
}
|
|
205
|
+
else
|
|
206
|
+
{
|
|
207
|
+
SetForegroundWindow(targetHandle);
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
Cleanup();
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
static void Cleanup()
|
|
214
|
+
{
|
|
215
|
+
if (notify != null)
|
|
216
|
+
{
|
|
217
|
+
notify.Visible = false;
|
|
218
|
+
notify.Dispose();
|
|
219
|
+
}
|
|
220
|
+
Application.ExitThread();
|
|
221
|
+
}
|
|
222
|
+
}
|