DialogTopmostWatcher.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Runtime.InteropServices;
  4. using System.Text;
  5. using System.Threading;
  6. using BeiJingComService.Infrastructure;
  7. namespace BeiJingComService.Core
  8. {
  9. /// <summary>
  10. /// COM 弹框置顶(Round57):让首信医保 COM 自己弹出的对话框始终显示在所有窗口最前。
  11. ///
  12. /// 背景:COM 调用跑在 <see cref="ComGateway"/> 的 STA 工作线程上,宿主是托盘进程
  13. /// (主窗体最小化 + ShowInTaskbar=false),进程没有前台窗口。COM 以 hwndParent=0 弹的
  14. /// 模态框会被系统排到当前前台窗口(HIS / prBrowser)后面,操作员看不见;而模态框又堵着
  15. /// STA 线程,直到有人点掉 —— 没人点就一路等到 ComTimeoutMs(60 秒)超时,结果作废,
  16. /// 后续每一笔调用继续排队干等。
  17. ///
  18. /// 两条路并行,互为兜底:
  19. /// A. 线程级 WH_CBT 钩子:在 ComGatewayWorker 线程上装,窗口激活瞬间就置顶(零延迟)。
  20. /// B. 进程级看门狗:独立后台线程,仅在一笔 COM 调用在飞期间每 ComDialogTopmostWatchMs
  21. /// 扫一遍本进程窗口。兜住「COM 自己另起线程弹框」这类钩子抓不到的情况。
  22. ///
  23. /// 五条硬约束(改动前请先读):
  24. /// 1. 钩子委托必须 static 持有,否则被 GC 回收后系统回调野指针,随机崩溃;
  25. /// 2. 钩子回调里<b>绝不抛异常</b>,且无条件 CallNextHookEx —— 它是在 COM 的窗口
  26. /// 创建/激活流程中间被调用的,异常穿出去会打断对方流程;
  27. /// 3. 钩子回调里<b>零 I/O</b>(不写日志、不碰文件),只累加计数;日志由看门狗线程事后落;
  28. /// 4. 看门狗必须是自己的线程,绝不能放 UI 线程 —— 跨线程 SetWindowPos 要等目标线程
  29. /// 泵消息,万一被拖住会连带假死诊断面板和托盘菜单;
  30. /// 5. 判据不能用窗口类名 "#32770" 当主判据:那只是 Win32 标准对话框的类名,
  31. /// 首信是 VB6 系,自绘窗体类名是 ThunderRT6FormDC,用 #32770 会漏一半。
  32. /// 本实现用「本进程 + 顶层 + 有标题栏」,类名只作可选的收窄条件。
  33. ///
  34. /// 失败代价:任何一步失败都只写日志,不影响读卡与交易,最坏等于回到 1.0.0.3。
  35. /// </summary>
  36. internal static class DialogTopmostWatcher
  37. {
  38. // ===================== 常量 =====================
  39. private const int WH_CBT = 5;
  40. private const int HCBT_ACTIVATE = 5;
  41. private const int GWL_STYLE = -16;
  42. private const int GWL_EXSTYLE = -20;
  43. private const int WS_CHILD = 0x40000000;
  44. private const int WS_CAPTION = 0x00C00000;
  45. private const int WS_EX_TOPMOST = 0x00000008;
  46. private const uint SWP_NOSIZE = 0x0001;
  47. private const uint SWP_NOMOVE = 0x0002;
  48. private const uint SWP_NOACTIVATE = 0x0010;
  49. private const uint SWP_NOOWNERZORDER = 0x0200;
  50. private const uint FLASHW_ALL = 0x00000003;
  51. private const uint FLASHW_TIMERNOFG = 0x0000000C;
  52. /// <summary>本服务自身的 WinForms 窗体类名前缀,看门狗要排除掉(托盘窗、诊断面板)。</summary>
  53. private const string OWN_WINFORMS_CLASS_PREFIX = "WindowsForms10";
  54. // ===================== 状态 =====================
  55. private static readonly object _sync = new object();
  56. /// <summary>钩子回调委托:必须 static 持有,防止被 GC 回收(钩子最经典崩溃点)。</summary>
  57. private static readonly CbtProc _proc = HookCallback;
  58. /// <summary>EnumWindows 回调委托,同样 static 持有。</summary>
  59. private static readonly EnumWindowsProc _enumProc = EnumWindowCallback;
  60. private static IntPtr _hook = IntPtr.Zero;
  61. private static int _hookThreadId;
  62. /// <summary>是否有一笔 COM 调用在飞(1=是)。钩子与看门狗都以它为前提。</summary>
  63. private static int _inCall;
  64. private static long _appliedCount;
  65. private static Thread _dog;
  66. private static volatile bool _dogRunning;
  67. private readonly static List<IntPtr> _found = new List<IntPtr>(8);
  68. /// <summary>只用于「避免同一个句柄重复写日志」,不参与是否置顶的判定。</summary>
  69. private static readonly HashSet<IntPtr> _logged = new HashSet<IntPtr>();
  70. private static readonly uint _currentPid = GetCurrentProcessId();
  71. // 配置(首次安装时读一次,配置项变更需重启程序)
  72. private static bool _configLoaded;
  73. private static bool _enabled = true;
  74. private static int _watchMs = 200;
  75. private static bool _foreground;
  76. private static string[] _classFilter;
  77. // ===================== 对外接口 =====================
  78. /// <summary>钩子是否已装上(供 /health 展示)。</summary>
  79. public static bool IsHookInstalled
  80. {
  81. get { return _hook != IntPtr.Zero; }
  82. }
  83. /// <summary>累计置顶次数(钩子 + 看门狗)。</summary>
  84. public static long TopmostAppliedCount
  85. {
  86. get { return Interlocked.Read(ref _appliedCount); }
  87. }
  88. /// <summary>是否有一笔 COM 调用在执行。</summary>
  89. public static bool IsCallInFlight
  90. {
  91. get { return Volatile.Read(ref _inCall) == 1; }
  92. }
  93. /// <summary>
  94. /// 在<b>当前线程</b>上装 WH_CBT 钩子。
  95. /// 必须由 ComGateway 的 STA 工作线程在线程体内调用(WorkerLoop 第一行),
  96. /// 装在外层 StartWorker() 里等于装在调用方线程上,收不到弹框事件。
  97. /// </summary>
  98. public static void InstallForCurrentThread()
  99. {
  100. try
  101. {
  102. EnsureConfig();
  103. if (!_enabled)
  104. {
  105. Logger.Info("COM 弹框置顶已关闭(ComDialogTopmost=false),不装钩子、不起看门狗");
  106. return;
  107. }
  108. // 服务模式(--service)跑在 Session 0:弹框不在用户桌面,SetWindowPos 无处可施
  109. if (!Environment.UserInteractive)
  110. {
  111. Logger.Info("非交互式会话(--service / Session 0),COM 弹框不在用户桌面,跳过置顶钩子");
  112. return;
  113. }
  114. if (_hook != IntPtr.Zero) return;
  115. _hook = SetWindowsHookEx(WH_CBT, _proc, GetModuleHandle(null), GetCurrentThreadId());
  116. if (_hook == IntPtr.Zero)
  117. {
  118. Logger.Error("COM 弹框置顶钩子安装失败,Win32 错误码=" + Marshal.GetLastWin32Error()
  119. + ",本次运行不置顶(不影响读卡)");
  120. return;
  121. }
  122. _hookThreadId = (int)GetCurrentThreadId();
  123. Logger.Info("COM 弹框置顶钩子已安装,线程=" + _hookThreadId
  124. + ",看门狗间隔=" + _watchMs + "ms"
  125. + ",抢前台=" + (_foreground ? "是" : "否")
  126. + ",类名过滤=" + (_classFilter == null || _classFilter.Length == 0 ? "不限" : string.Join(",", _classFilter)));
  127. if (_watchMs > 0) StartDog();
  128. }
  129. catch (Exception ex)
  130. {
  131. Logger.Error("安装 COM 弹框置顶钩子异常(已忽略,不影响读卡)", ex);
  132. }
  133. }
  134. /// <summary>摘钩子、停看门狗。工作线程退出前调用。</summary>
  135. public static void Uninstall()
  136. {
  137. try
  138. {
  139. StopDog();
  140. if (_hook != IntPtr.Zero)
  141. {
  142. UnhookWindowsHookEx(_hook);
  143. _hook = IntPtr.Zero;
  144. Logger.Info("COM 弹框置顶钩子已摘除,累计置顶 " + TopmostAppliedCount + " 次");
  145. }
  146. }
  147. catch (Exception ex)
  148. {
  149. Logger.Error("摘除 COM 弹框置顶钩子异常(已忽略)", ex);
  150. }
  151. }
  152. /// <summary>一笔 COM 调用开始:允许钩子/看门狗工作。</summary>
  153. public static void BeginCall()
  154. {
  155. Interlocked.Exchange(ref _inCall, 1);
  156. }
  157. /// <summary>一笔 COM 调用结束:停掉看门狗扫描(钩子本身不动,等下次弹框)。</summary>
  158. public static void EndCall()
  159. {
  160. Interlocked.Exchange(ref _inCall, 0);
  161. // 清掉「已记过日志」的句柄集合:句柄会被系统复用,不清会把新弹框的日志误吞掉
  162. lock (_sync)
  163. {
  164. _logged.Clear();
  165. }
  166. }
  167. // ===================== 钩子(A 路) =====================
  168. private static IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
  169. {
  170. try
  171. {
  172. // HCBT_ACTIVATE:窗口即将被激活。此刻 SetWindowPos 有效;
  173. // HCBT_CREATEWND 阶段窗口还没真正创建,改 z 序会失败,故不用它。
  174. //
  175. // ★ 这里刻意不判 _inCall:钩子是线程级的,只看得见 ComGatewayWorker 这个
  176. // COM 线程创建的窗口,本来就都是 COM 的弹框。而「队列空闲后自动 close」
  177. // 那条路径不经过 CallNative,判 _inCall 会漏掉它在关闭时弹的框。
  178. if (nCode >= 0 && wParam == (IntPtr)HCBT_ACTIVATE)
  179. {
  180. IntPtr hwnd = lParam;
  181. if (hwnd != IntPtr.Zero && IsTargetWindow(hwnd))
  182. {
  183. // 回调里零 I/O:只做 SetWindowPos + 计数,日志交给看门狗线程
  184. ApplyTopmost(hwnd, false);
  185. }
  186. }
  187. }
  188. catch
  189. {
  190. // ★ 绝不能抛出去:这里处于 COM 的窗口激活流程中间
  191. }
  192. return CallNextHookEx(_hook, nCode, wParam, lParam);
  193. }
  194. // ===================== 看门狗(B 路) =====================
  195. private static void StartDog()
  196. {
  197. lock (_sync)
  198. {
  199. if (_dog != null) return;
  200. _dogRunning = true;
  201. _dog = new Thread(DogLoop);
  202. _dog.IsBackground = true;
  203. _dog.Name = "ComTopmostDog";
  204. _dog.Start();
  205. }
  206. }
  207. private static void StopDog()
  208. {
  209. Thread dog;
  210. lock (_sync)
  211. {
  212. _dogRunning = false;
  213. dog = _dog;
  214. _dog = null;
  215. }
  216. if (dog != null && dog.IsAlive)
  217. {
  218. try { dog.Join(TimeSpan.FromSeconds(1)); }
  219. catch (Exception ex) { Logger.Error("等待弹框置顶看门狗退出超时", ex); }
  220. }
  221. }
  222. private static void DogLoop()
  223. {
  224. while (_dogRunning)
  225. {
  226. try
  227. {
  228. if (Volatile.Read(ref _inCall) == 1) ScanAndApply();
  229. }
  230. catch (Exception ex)
  231. {
  232. // 看门狗失败只要记一次日志继续跑,不影响业务
  233. Logger.Error("弹框置顶看门狗扫描失败(已忽略)", ex);
  234. }
  235. // 分片睡眠:保证 StopDog 能在 1 秒内退出
  236. int wait = _watchMs > 0 ? _watchMs : 200;
  237. for (int slept = 0; slept < wait && _dogRunning; slept += 50)
  238. {
  239. Thread.Sleep(Math.Min(50, wait - slept));
  240. }
  241. }
  242. }
  243. private static void ScanAndApply()
  244. {
  245. _found.Clear();
  246. EnumWindows(_enumProc, IntPtr.Zero);
  247. if (_found.Count == 0) return;
  248. for (int i = 0; i < _found.Count; i++)
  249. {
  250. ApplyTopmost(_found[i], true);
  251. }
  252. }
  253. private static bool EnumWindowCallback(IntPtr hwnd, IntPtr lParam)
  254. {
  255. try
  256. {
  257. if (hwnd == IntPtr.Zero) return true;
  258. if (!IsWindowVisible(hwnd)) return true;
  259. uint pid;
  260. GetWindowThreadProcessId(hwnd, out pid);
  261. if (pid != _currentPid) return true;
  262. if (IsTargetWindow(hwnd)) _found.Add(hwnd);
  263. }
  264. catch
  265. {
  266. // 单个窗口判断失败不影响整轮扫描
  267. }
  268. return true;
  269. }
  270. // ===================== 判定与执行 =====================
  271. /// <summary>
  272. /// 是否属于「要置顶的弹框」:
  273. /// 1. 顶层窗口(无 WS_CHILD);
  274. /// 2. 有标题栏(WS_CAPTION)—— 顺带排掉 tooltip、IME 候选窗、拖动小窗;
  275. /// 3. 不是本服务自己的 WinForms 窗体(托盘窗、诊断面板);
  276. /// 4. 类名不在可选白名单外(白名单为空表示不限);
  277. /// 5. 目前还不是 topmost(已经是就不用再动,也顺便防了反复调用)。
  278. /// </summary>
  279. private static bool IsTargetWindow(IntPtr hwnd)
  280. {
  281. int style = GetStyle(hwnd);
  282. if ((style & WS_CHILD) != 0) return false;
  283. if ((style & WS_CAPTION) != WS_CAPTION) return false;
  284. string cls = GetClassNameSafe(hwnd);
  285. if (cls.Length > 0 && cls.StartsWith(OWN_WINFORMS_CLASS_PREFIX, StringComparison.Ordinal))
  286. {
  287. return false;
  288. }
  289. if (_classFilter != null && _classFilter.Length > 0)
  290. {
  291. bool hit = false;
  292. for (int i = 0; i < _classFilter.Length; i++)
  293. {
  294. if (string.Equals(cls, _classFilter[i], StringComparison.OrdinalIgnoreCase))
  295. {
  296. hit = true;
  297. break;
  298. }
  299. }
  300. if (!hit) return false;
  301. }
  302. if ((GetExStyle(hwnd) & WS_EX_TOPMOST) != 0) return false;
  303. return true;
  304. }
  305. private static void ApplyTopmost(IntPtr hwnd, bool fromDog)
  306. {
  307. try
  308. {
  309. if (!IsWindow(hwnd)) return;
  310. bool wasTopmost = (GetExStyle(hwnd) & WS_EX_TOPMOST) != 0;
  311. SetWindowPos(hwnd, HWND_TOPMOST, 0, 0, 0, 0,
  312. SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE | SWP_NOOWNERZORDER);
  313. if (_foreground)
  314. {
  315. TryActivate(hwnd);
  316. }
  317. else if (!wasTopmost)
  318. {
  319. // 默认不抢焦点,只闪任务栏/标题栏提醒(RDP 下通常只闪任务栏,属正常)
  320. Flash(hwnd);
  321. }
  322. Interlocked.Increment(ref _appliedCount);
  323. // 钩子路径(fromDog=false)不落盘:CBT 钩子是串行的,写文件会拖住 COM 的窗口激活
  324. if (fromDog && !wasTopmost) LogApplied(hwnd);
  325. }
  326. catch (Exception ex)
  327. {
  328. if (fromDog) Logger.Error("弹框置顶失败(已忽略)", ex);
  329. }
  330. }
  331. /// <summary>同一个句柄只记一次日志,避免看门狗 200ms 一轮刷屏。</summary>
  332. private static void LogApplied(IntPtr hwnd)
  333. {
  334. bool first;
  335. lock (_sync)
  336. {
  337. first = _logged.Add(hwnd);
  338. }
  339. if (!first) return;
  340. Logger.Info("[Topmost] 已置顶 hwnd=0x" + hwnd.ToInt64().ToString("X")
  341. + " class=" + GetClassNameSafe(hwnd)
  342. + " title=" + GetWindowTextSafe(hwnd)
  343. + "(累计 " + TopmostAppliedCount + " 次)");
  344. }
  345. /// <summary>
  346. /// 抢前台(仅 ComDialogForeground=true 时调用)。前后台锁定下这招不是 100% 生效,
  347. /// 失败就失败,不影响置顶本身。
  348. /// </summary>
  349. private static void TryActivate(IntPtr hwnd)
  350. {
  351. try
  352. {
  353. uint pid;
  354. IntPtr fg = GetForegroundWindow();
  355. uint fgThread = fg == IntPtr.Zero ? 0 : GetWindowThreadProcessId(fg, out pid);
  356. uint curThread = GetCurrentThreadId();
  357. if (fgThread == 0 || fgThread == curThread)
  358. {
  359. SetForegroundWindow(hwnd);
  360. return;
  361. }
  362. if (AttachThreadInput(curThread, fgThread, true))
  363. {
  364. try { SetForegroundWindow(hwnd); }
  365. finally { AttachThreadInput(curThread, fgThread, false); }
  366. }
  367. }
  368. catch
  369. {
  370. // 抢前台失败无所谓,置顶已经生效
  371. }
  372. }
  373. private static void Flash(IntPtr hwnd)
  374. {
  375. try
  376. {
  377. var fi = new FLASHWINFO
  378. {
  379. cbSize = (uint)Marshal.SizeOf(typeof(FLASHWINFO)),
  380. hwnd = hwnd,
  381. dwFlags = FLASHW_ALL | FLASHW_TIMERNOFG,
  382. uCount = 3,
  383. dwTimeout = 0
  384. };
  385. FlashWindowEx(ref fi);
  386. }
  387. catch
  388. {
  389. // 闪烁失败无所谓
  390. }
  391. }
  392. // ===================== 配置 =====================
  393. private static void EnsureConfig()
  394. {
  395. if (_configLoaded) return;
  396. _configLoaded = true;
  397. try
  398. {
  399. _enabled = AppConfig.ComDialogTopmost;
  400. _watchMs = AppConfig.ComDialogTopmostWatchMs;
  401. _foreground = AppConfig.ComDialogForeground;
  402. _classFilter = ParseClasses(AppConfig.ComDialogTopmostClasses);
  403. if (_watchMs < 0) _watchMs = 0;
  404. }
  405. catch (Exception ex)
  406. {
  407. // 配置写坏也不能让钩子把网关带崩:按默认值启用
  408. _enabled = true;
  409. _watchMs = 200;
  410. _foreground = false;
  411. _classFilter = null;
  412. Logger.Error("读取 COM 弹框置顶配置失败,按默认值(启用 + 看门狗 200ms)处理", ex);
  413. }
  414. }
  415. private static string[] ParseClasses(string raw)
  416. {
  417. if (string.IsNullOrWhiteSpace(raw)) return null;
  418. string[] parts = raw.Split(new[] { ',', ';' }, StringSplitOptions.RemoveEmptyEntries);
  419. var list = new List<string>(parts.Length);
  420. for (int i = 0; i < parts.Length; i++)
  421. {
  422. string item = parts[i].Trim();
  423. if (item.Length > 0) list.Add(item);
  424. }
  425. return list.Count == 0 ? null : list.ToArray();
  426. }
  427. // ===================== 辅助 =====================
  428. private static int GetStyle(IntPtr hwnd)
  429. {
  430. return IntPtr.Size == 8
  431. ? (int)GetWindowLongPtr(hwnd, GWL_STYLE).ToInt64()
  432. : GetWindowLong(hwnd, GWL_STYLE);
  433. }
  434. private static int GetExStyle(IntPtr hwnd)
  435. {
  436. return IntPtr.Size == 8
  437. ? (int)GetWindowLongPtr(hwnd, GWL_EXSTYLE).ToInt64()
  438. : GetWindowLong(hwnd, GWL_EXSTYLE);
  439. }
  440. private static string GetClassNameSafe(IntPtr hwnd)
  441. {
  442. try
  443. {
  444. var sb = new StringBuilder(256);
  445. int n = GetClassName(hwnd, sb, sb.Capacity);
  446. return n > 0 ? sb.ToString(0, n) : string.Empty;
  447. }
  448. catch
  449. {
  450. return string.Empty;
  451. }
  452. }
  453. private static string GetWindowTextSafe(IntPtr hwnd)
  454. {
  455. try
  456. {
  457. var sb = new StringBuilder(256);
  458. int n = GetWindowText(hwnd, sb, sb.Capacity);
  459. return n > 0 ? sb.ToString(0, n) : string.Empty;
  460. }
  461. catch
  462. {
  463. return string.Empty;
  464. }
  465. }
  466. // ===================== P/Invoke =====================
  467. private delegate IntPtr CbtProc(int nCode, IntPtr wParam, IntPtr lParam);
  468. private delegate bool EnumWindowsProc(IntPtr hwnd, IntPtr lParam);
  469. [StructLayout(LayoutKind.Sequential)]
  470. private struct FLASHWINFO
  471. {
  472. public uint cbSize;
  473. public IntPtr hwnd;
  474. public uint dwFlags;
  475. public uint uCount;
  476. public uint dwTimeout;
  477. }
  478. private static readonly IntPtr HWND_TOPMOST = new IntPtr(-1);
  479. [DllImport("user32.dll", SetLastError = true)]
  480. private static extern IntPtr SetWindowsHookEx(int idHook, CbtProc lpfn, IntPtr hMod, uint dwThreadId);
  481. [DllImport("user32.dll", SetLastError = true)]
  482. private static extern bool UnhookWindowsHookEx(IntPtr hhk);
  483. [DllImport("user32.dll")]
  484. private static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode, IntPtr wParam, IntPtr lParam);
  485. [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
  486. private static extern IntPtr GetModuleHandle(string lpModuleName);
  487. [DllImport("kernel32.dll")]
  488. private static extern uint GetCurrentThreadId();
  489. [DllImport("kernel32.dll")]
  490. private static extern uint GetCurrentProcessId();
  491. [DllImport("user32.dll")]
  492. private static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId);
  493. [DllImport("user32.dll", SetLastError = true)]
  494. private static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter,
  495. int X, int Y, int cx, int cy, uint uFlags);
  496. [DllImport("user32.dll", EntryPoint = "GetWindowLong")]
  497. private static extern int GetWindowLong(IntPtr hWnd, int nIndex);
  498. [DllImport("user32.dll", EntryPoint = "GetWindowLongPtr")]
  499. private static extern IntPtr GetWindowLongPtr(IntPtr hWnd, int nIndex);
  500. [DllImport("user32.dll", CharSet = CharSet.Unicode)]
  501. private static extern int GetClassName(IntPtr hWnd, StringBuilder lpClassName, int nMaxCount);
  502. [DllImport("user32.dll", CharSet = CharSet.Unicode)]
  503. private static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);
  504. [DllImport("user32.dll")]
  505. private static extern bool IsWindowVisible(IntPtr hWnd);
  506. [DllImport("user32.dll")]
  507. private static extern bool IsWindow(IntPtr hWnd);
  508. [DllImport("user32.dll")]
  509. private static extern bool EnumWindows(EnumWindowsProc lpEnumFunc, IntPtr lParam);
  510. [DllImport("user32.dll")]
  511. private static extern IntPtr GetForegroundWindow();
  512. [DllImport("user32.dll")]
  513. private static extern bool SetForegroundWindow(IntPtr hWnd);
  514. [DllImport("user32.dll")]
  515. private static extern bool AttachThreadInput(uint idAttach, uint idAttachTo, bool fAttach);
  516. [DllImport("user32.dll")]
  517. private static extern bool FlashWindowEx(ref FLASHWINFO pwfi);
  518. }
  519. }