using Newtonsoft.Json.Linq; using System; using System.Windows.Forms; namespace UppPay.Forms { public partial class SMForm : Form { public JObject OutjObject { get; set; } public string authcode { get; set; } private bool isButtonClose = false; public SMForm() { InitializeComponent(); OutjObject = new JObject(); authcode = ""; // 设置窗体在屏幕上居中显示 this.StartPosition = FormStartPosition.CenterScreen; // 设置窗体始终显示在其他窗口的最前面 this.TopMost = true; authcodeBox.Focus(); // 为 TextBox 的 KeyDown 事件添加处理程序 authcodeBox.KeyDown += authcodeBox_KeyDown; // 处理窗体关闭事件,阻止关闭操作 this.FormClosing += MainForm_FormClosing; authcodeBox.Focus(); } private void MainForm_FormClosing(object sender, FormClosingEventArgs e) { if (!isButtonClose) { OutjObject = new JObject(); OutjObject["errorCode"] = "-1"; OutjObject["errorMessage"] = "收费员已取消操作"; //this.DialogResult = DialogResult.OK; //this.Close(); } } private void authcodeBox_KeyDown(object sender, KeyEventArgs e) { // 检查是否按下回车键 if (e.KeyCode == Keys.Enter) { // 处理回车键按下事件 SMPayLoading(authcodeBox.Text); // 阻止回车键的默认行为,避免在多行文本框中换行 e.SuppressKeyPress = true; } } private async void SMPayLoading(string authcode) { try { if (string.IsNullOrWhiteSpace(authcode)) { Tlabel.Text = "请输入支付码内容...."; authcodeBox.Focus(); } else { OutjObject = new JObject(); OutjObject["errorCode"] = "0"; OutjObject["errorMessage"] = "扫码成功"; OutjObject["authcode"] = authcode; isButtonClose = true; this.DialogResult = DialogResult.OK; this.Close(); } } catch (Exception ex) { // 处理异常 Tlabel.Text = $"发生错误: {ex.Message}"; } } } }