PasswordKeyboardForm.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using System.Runtime.InteropServices;
  11. namespace PTMedicalInsurance.Forms
  12. {
  13. public partial class PasswordKeyboardForm : Form
  14. {
  15. // 导入Windows API函数
  16. [DllImport("user32.dll")]
  17. private static extern IntPtr GetForegroundWindow();
  18. [DllImport("user32.dll")]
  19. private static extern IntPtr GetFocus();
  20. [DllImport("user32.dll")]
  21. private static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
  22. private const uint WM_CHAR = 0x0102;
  23. private IntPtr _targetEditBox;
  24. public string PassWord = "";
  25. private int countdownSeconds = 30;
  26. public PasswordKeyboardForm(IntPtr targetEditBox)
  27. {
  28. InitializeComponent();
  29. _targetEditBox = targetEditBox;
  30. }
  31. public PasswordKeyboardForm()
  32. {
  33. InitializeComponent();
  34. // 设置最小和最大大小相同,这样窗体大小就固定了
  35. this.MinimumSize = new Size(410, 550);
  36. this.MaximumSize = new Size(410, 550);
  37. // 方法1:在窗体属性中设置
  38. //this.StartPosition = FormStartPosition.CenterScreen;
  39. // 方法2:居中到父窗体
  40. this.StartPosition = FormStartPosition.CenterParent;
  41. tb_PassWord.Text = "";
  42. // 设置Timer属性
  43. timer1 = new System.Windows.Forms.Timer();
  44. timer1.Interval = 1000; // 1秒
  45. timer1.Tick += timer1_Tick;
  46. // 初始化显示
  47. UpdateCountdownDisplay();
  48. StartCountdown();
  49. }
  50. private void PasswordKeyboardForm_Load(object sender, EventArgs e)
  51. {
  52. this.TopMost = true;
  53. tb_PassWord.Focus();
  54. }
  55. private void SendKeyToFocusedControl(char key)
  56. {
  57. IntPtr focusedHandle = GetFocus();
  58. if (focusedHandle != IntPtr.Zero)
  59. {
  60. SendMessage(focusedHandle, WM_CHAR, (IntPtr)key, IntPtr.Zero);
  61. }
  62. }
  63. // 处理按钮点击事件
  64. private void btn_1_Click(object sender, EventArgs e)
  65. {
  66. // 可选:重置倒计时
  67. ResetCountdown();
  68. Sunny.UI.UIButton button = sender as Sunny.UI.UIButton;
  69. if (button != null)
  70. {
  71. // 使用 SendKeys 将按钮的文本发送到当前焦点控件
  72. SendKeys.Send(button.Text);
  73. tb_PassWord.Text = tb_PassWord.Text + button.Text;
  74. }
  75. StartCountdown();
  76. }
  77. // 处理删除按钮点击事件
  78. private void btn_delete_Click(object sender, EventArgs e)
  79. {
  80. // 可选:重置倒计时
  81. ResetCountdown();
  82. StartCountdown();
  83. SendKeys.Send("{BACKSPACE}");
  84. // 删除最后一个字符
  85. if (tb_PassWord.Text.Length > 0)
  86. {
  87. tb_PassWord.Text = tb_PassWord.Text.Substring(0, tb_PassWord.Text.Length - 1);
  88. }
  89. }
  90. // 处理关闭按钮点击事件
  91. private void btn_Exit_Click(object sender, EventArgs e)
  92. {
  93. this.Close(); // 关闭密码键盘
  94. }
  95. private void btn_OK_Click(object sender, EventArgs e)
  96. {
  97. SendTextToEditBox(sender.ToString());
  98. PassWord = tb_PassWord.Text;
  99. DialogResult = DialogResult.OK;
  100. }
  101. // 将文本发送到目标编辑框
  102. private void SendTextToEditBox(string text)
  103. {
  104. if (_targetEditBox != IntPtr.Zero)
  105. {
  106. foreach (char c in text)
  107. {
  108. SendMessage(_targetEditBox, WM_CHAR, (IntPtr)c, IntPtr.Zero);
  109. }
  110. }
  111. }
  112. private void btn_clear_Click(object sender, EventArgs e)
  113. {
  114. // 可选:重置倒计时
  115. ResetCountdown();
  116. StartCountdown();
  117. SendKeys.Send("^{A}"); // 全选
  118. SendKeys.Send("{DEL}"); // 删除
  119. tb_PassWord.Text = "";
  120. }
  121. private void timer1_Tick(object sender, EventArgs e)
  122. {
  123. countdownSeconds--;
  124. UpdateCountdownDisplay();
  125. if (countdownSeconds <= 0)
  126. {
  127. timer1.Stop();
  128. this.Close();
  129. }
  130. }
  131. private void UpdateCountdownDisplay()
  132. {
  133. uiTitlePanel1.Text = "请输入社保卡密码"+ $" {countdownSeconds}秒后关闭";
  134. }
  135. private void StartCountdown()
  136. {
  137. countdownSeconds = 30;
  138. UpdateCountdownDisplay();
  139. timer1.Start();
  140. }
  141. private void ResetCountdown()
  142. {
  143. countdownSeconds = 30;
  144. UpdateCountdownDisplay();
  145. timer1.Stop();
  146. }
  147. }
  148. }