using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Runtime.InteropServices; namespace PTMedicalInsurance.Forms { public partial class PasswordKeyboardForm : Form { // 导入Windows API函数 [DllImport("user32.dll")] private static extern IntPtr GetForegroundWindow(); [DllImport("user32.dll")] private static extern IntPtr GetFocus(); [DllImport("user32.dll")] private static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam); private const uint WM_CHAR = 0x0102; private IntPtr _targetEditBox; public string PassWord = ""; private int countdownSeconds = 30; public PasswordKeyboardForm(IntPtr targetEditBox) { InitializeComponent(); _targetEditBox = targetEditBox; } public PasswordKeyboardForm() { InitializeComponent(); // 设置最小和最大大小相同,这样窗体大小就固定了 this.MinimumSize = new Size(410, 550); this.MaximumSize = new Size(410, 550); // 方法1:在窗体属性中设置 //this.StartPosition = FormStartPosition.CenterScreen; // 方法2:居中到父窗体 this.StartPosition = FormStartPosition.CenterParent; tb_PassWord.Text = ""; // 设置Timer属性 timer1 = new System.Windows.Forms.Timer(); timer1.Interval = 1000; // 1秒 timer1.Tick += timer1_Tick; // 初始化显示 UpdateCountdownDisplay(); StartCountdown(); } private void PasswordKeyboardForm_Load(object sender, EventArgs e) { this.TopMost = true; tb_PassWord.Focus(); } private void SendKeyToFocusedControl(char key) { IntPtr focusedHandle = GetFocus(); if (focusedHandle != IntPtr.Zero) { SendMessage(focusedHandle, WM_CHAR, (IntPtr)key, IntPtr.Zero); } } // 处理按钮点击事件 private void btn_1_Click(object sender, EventArgs e) { // 可选:重置倒计时 ResetCountdown(); Sunny.UI.UIButton button = sender as Sunny.UI.UIButton; if (button != null) { // 使用 SendKeys 将按钮的文本发送到当前焦点控件 SendKeys.Send(button.Text); tb_PassWord.Text = tb_PassWord.Text + button.Text; } StartCountdown(); } // 处理删除按钮点击事件 private void btn_delete_Click(object sender, EventArgs e) { // 可选:重置倒计时 ResetCountdown(); StartCountdown(); SendKeys.Send("{BACKSPACE}"); // 删除最后一个字符 if (tb_PassWord.Text.Length > 0) { tb_PassWord.Text = tb_PassWord.Text.Substring(0, tb_PassWord.Text.Length - 1); } } // 处理关闭按钮点击事件 private void btn_Exit_Click(object sender, EventArgs e) { this.Close(); // 关闭密码键盘 } private void btn_OK_Click(object sender, EventArgs e) { SendTextToEditBox(sender.ToString()); PassWord = tb_PassWord.Text; DialogResult = DialogResult.OK; } // 将文本发送到目标编辑框 private void SendTextToEditBox(string text) { if (_targetEditBox != IntPtr.Zero) { foreach (char c in text) { SendMessage(_targetEditBox, WM_CHAR, (IntPtr)c, IntPtr.Zero); } } } private void btn_clear_Click(object sender, EventArgs e) { // 可选:重置倒计时 ResetCountdown(); StartCountdown(); SendKeys.Send("^{A}"); // 全选 SendKeys.Send("{DEL}"); // 删除 tb_PassWord.Text = ""; } private void timer1_Tick(object sender, EventArgs e) { countdownSeconds--; UpdateCountdownDisplay(); if (countdownSeconds <= 0) { timer1.Stop(); this.Close(); } } private void UpdateCountdownDisplay() { uiTitlePanel1.Text = "请输入社保卡密码"+ $" {countdownSeconds}秒后关闭"; } private void StartCountdown() { countdownSeconds = 30; UpdateCountdownDisplay(); timer1.Start(); } private void ResetCountdown() { countdownSeconds = 30; UpdateCountdownDisplay(); timer1.Stop(); } } }