PasswordKeyboardTrigger.cs 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System;
  7. using System.Diagnostics;
  8. using System.Runtime.InteropServices;
  9. using System.Threading;
  10. using System.Windows.Forms;
  11. using PTMedicalInsurance.Forms;
  12. namespace PTMedicalInsurance.Business
  13. {
  14. public class PasswordKeyboardTrigger
  15. {
  16. // 启动监控线程
  17. public static void Start()
  18. {
  19. Thread monitorThread = new Thread(MonitorEditBox);
  20. monitorThread.IsBackground = true;
  21. monitorThread.Start();
  22. }
  23. // 监控编辑框状态
  24. private static void MonitorEditBox()
  25. {
  26. while (true)
  27. {
  28. IntPtr targetWindow = FindWindow(null, "安全控件 V1.0.6"); // 替换为目标窗体的标题
  29. if (targetWindow != IntPtr.Zero)
  30. {
  31. IntPtr editBox = FindWindowEx(targetWindow, IntPtr.Zero, "Edit", null); // 查找编辑框
  32. if (editBox != IntPtr.Zero)
  33. {
  34. // 检查编辑框是否获得焦点
  35. IntPtr focusedWindow = GetFocus();
  36. if (focusedWindow == editBox)
  37. {
  38. // 显示密码键盘
  39. Invoke((MethodInvoker)delegate
  40. {
  41. PasswordKeyboardForm passwordKeyboard = new PasswordKeyboardForm(editBox);
  42. passwordKeyboard.ShowDialog();
  43. });
  44. }
  45. }
  46. }
  47. Thread.Sleep(500); // 每500ms检查一次
  48. }
  49. }
  50. private static void Invoke(MethodInvoker methodInvoker)
  51. {
  52. throw new NotImplementedException();
  53. }
  54. // Windows API
  55. [DllImport("user32.dll", SetLastError = true)]
  56. private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
  57. [DllImport("user32.dll", SetLastError = true)]
  58. private static extern IntPtr FindWindowEx(IntPtr parentHandle, IntPtr childAfter, string className, string windowTitle);
  59. [DllImport("user32.dll", SetLastError = true)]
  60. private static extern IntPtr GetFocus();
  61. }
  62. }