123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102 |
- 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 PasswordKeyboardForm(IntPtr targetEditBox)
- {
- InitializeComponent();
- _targetEditBox = targetEditBox;
- }
- public PasswordKeyboardForm()
- {
- InitializeComponent();
- }
- 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)
- {
- Button button = sender as Button;
- if (button != null)
- {
- // 使用 SendKeys 将按钮的文本发送到当前焦点控件
- SendKeys.Send(button.Text);
- }
- }
- // 处理删除按钮点击事件
- private void btn_delete_Click(object sender, EventArgs e)
- {
- SendKeys.Send("{BACKSPACE}");
- }
- // 处理关闭按钮点击事件
- private void btn_Exit_Click(object sender, EventArgs e)
- {
- this.Close(); // 关闭密码键盘
- }
- private void btn_OK_Click(object sender, EventArgs e)
- {
- SendTextToEditBox(sender.ToString());
- }
- // 将文本发送到目标编辑框
- 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)
- {
- SendKeys.Send("^{A}"); // 全选
- SendKeys.Send("{DEL}"); // 删除
- }
- private void PasswordKeyboardForm_Load(object sender, EventArgs e)
- {
- this.TopMost = true;
- }
- }
- }
|