WindowMonitor.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Runtime.InteropServices;
  7. using System.Threading;
  8. using System.Windows.Forms;
  9. using PTMedicalInsurance.Forms;
  10. namespace PTMedicalInsurance.Business
  11. {
  12. //使用 EnumWindows API 枚举所有顶层窗口,并检测目标窗体
  13. public class WindowMonitor
  14. {
  15. [DllImport("user32.dll")]
  16. private static extern bool EnumWindows(EnumWindowsProc enumProc, IntPtr lParam);
  17. [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
  18. private static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);
  19. private delegate bool EnumWindowsProc(IntPtr hWnd, IntPtr lParam);
  20. private const string TargetWindowTitle = "新疆维吾尔自治区社会保障卡(电子社保卡)统一鉴权";//"安全控件 V1.0.6"; // 替换为目标窗体的标题
  21. private const int CheckInterval = 1000; // 检查间隔(毫秒)
  22. public static void Start()
  23. {
  24. Thread monitorThread = new Thread(MonitorWindows);
  25. monitorThread.IsBackground = true;
  26. monitorThread.Start();
  27. }
  28. private static void MonitorWindows()
  29. {
  30. while (true)
  31. {
  32. EnumWindows(EnumWindowsCallback, IntPtr.Zero);
  33. Thread.Sleep(CheckInterval);
  34. }
  35. }
  36. private static bool EnumWindowsCallback(IntPtr hWnd, IntPtr lParam)
  37. {
  38. StringBuilder windowTitle = new StringBuilder(256);
  39. GetWindowText(hWnd, windowTitle, windowTitle.Capacity);
  40. if (windowTitle.ToString().Contains(TargetWindowTitle))
  41. {
  42. if (windowTitle.ToString().Contains("安全控件 V1.0.6"))
  43. {
  44. //Thread.Sleep(3000);
  45. ShowPasswordKeyboard();
  46. return false; // 停止枚举
  47. }
  48. }
  49. return true; // 继续枚举
  50. }
  51. private static void ShowPasswordKeyboard()
  52. {
  53. //Application.Run(new PasswordKeyboardForm(IntPtr));
  54. PasswordKeyboardForm passwordKeyboard = new PasswordKeyboardForm();
  55. passwordKeyboard.ShowDialog();
  56. }
  57. }
  58. public class WindowCloseListener
  59. {
  60. private const int WM_CLOSE = 0x0010;
  61. private const int GWL_WNDPROC = -4;
  62. private IntPtr _targetWindowHandle;
  63. private IntPtr _originalWndProc;
  64. private WndProcDelegate _wndProcDelegate;
  65. public event EventHandler WindowClosed;
  66. public WindowCloseListener(IntPtr targetWindowHandle)
  67. {
  68. _targetWindowHandle = targetWindowHandle;
  69. _wndProcDelegate = new WndProcDelegate(WndProc);
  70. }
  71. public void Start()
  72. {
  73. // 替换目标窗口的窗口过程
  74. _originalWndProc = SetWindowLong(_targetWindowHandle, GWL_WNDPROC, Marshal.GetFunctionPointerForDelegate(_wndProcDelegate));
  75. }
  76. public void Stop()
  77. {
  78. // 恢复原始窗口过程
  79. SetWindowLong(_targetWindowHandle, GWL_WNDPROC, _originalWndProc);
  80. }
  81. private IntPtr WndProc(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam)
  82. {
  83. if (msg == WM_CLOSE)
  84. {
  85. // 触发窗体关闭事件
  86. WindowClosed?.Invoke(this, EventArgs.Empty);
  87. }
  88. // 调用原始窗口过程
  89. return CallWindowProc(_originalWndProc, hWnd, msg, wParam, lParam);
  90. }
  91. [DllImport("user32.dll", SetLastError = true)]
  92. private static extern IntPtr SetWindowLong(IntPtr hWnd, int nIndex, IntPtr dwNewLong);
  93. [DllImport("user32.dll", SetLastError = true)]
  94. private static extern IntPtr CallWindowProc(IntPtr lpPrevWndFunc, IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam);
  95. private delegate IntPtr WndProcDelegate(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam);
  96. }
  97. }