You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
215 lines
6.8 KiB
215 lines
6.8 KiB
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using System.Windows;
|
|
using System.Windows.Controls;
|
|
using System.Windows.Data;
|
|
using System.Windows.Documents;
|
|
using System.Windows.Input;
|
|
using System.Windows.Media;
|
|
using System.Windows.Media.Imaging;
|
|
using System.Windows.Shapes;
|
|
|
|
namespace DyeingComputer.KEY
|
|
{
|
|
/// <summary>
|
|
/// TouchKeyboard.xaml 的交互逻辑
|
|
/// </summary>
|
|
public partial class TouchKeyboardNumeral : Window
|
|
{
|
|
private bool mode = false;
|
|
public string KeyValue;
|
|
public event EventHandler<KeyPressedEventArgs> KeyPressed;
|
|
private KeyboardMode currentMode = KeyboardMode.Default;
|
|
private int minValue = 0;
|
|
private int maxValue = 9;
|
|
private TextBox targetTextBox;
|
|
// 定义键盘模式枚举
|
|
public enum KeyboardMode
|
|
{
|
|
Default, // 默认模式,所有键可用
|
|
NumericOnly, // 只允许数字
|
|
DecimalOnly, // 允许数字和小数点
|
|
LimitedRange // 限制数字范围
|
|
}
|
|
|
|
public TouchKeyboardNumeral()
|
|
{
|
|
InitializeComponent();
|
|
}
|
|
public void SetKeyboardMode(KeyboardMode mode, TextBox target = null, int min = 0, int max = 9)
|
|
{
|
|
currentMode = mode;
|
|
targetTextBox = target;
|
|
minValue = min;
|
|
maxValue = max;
|
|
|
|
UpdateKeyboardLayout();
|
|
}
|
|
private void UpdateKeyboardLayout()
|
|
{
|
|
switch (currentMode)
|
|
{
|
|
case KeyboardMode.NumericOnly:
|
|
// 只允许数字,禁用小数点
|
|
SetButtonEnabled(".", false);
|
|
break;
|
|
|
|
case KeyboardMode.DecimalOnly:
|
|
// 允许数字和小数点
|
|
SetButtonEnabled(".", true);
|
|
break;
|
|
|
|
case KeyboardMode.LimitedRange:
|
|
// 根据范围启用/禁用数字键
|
|
for (int i = 0; i <= 9; i++)
|
|
{
|
|
SetButtonEnabled(i.ToString(), i >= minValue && i <= maxValue);
|
|
}
|
|
mode = true;
|
|
break;
|
|
|
|
case KeyboardMode.Default:
|
|
default:
|
|
// 启用所有键
|
|
foreach (var element in FirstRow.Children)
|
|
{
|
|
if (element is Button button && char.IsDigit(button.Content.ToString()[0]))
|
|
button.IsEnabled = true;
|
|
}
|
|
foreach (var element in SecondRow.Children)
|
|
{
|
|
if (element is Button button && (char.IsDigit(button.Content.ToString()[0]) || button.Content.ToString() == "."))
|
|
button.IsEnabled = true;
|
|
}
|
|
foreach (var element in ThirdRow.Children)
|
|
{
|
|
if (element is Button button && char.IsDigit(button.Content.ToString()[0]))
|
|
button.IsEnabled = true;
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
|
|
// 设置按钮启用状态
|
|
private void SetButtonEnabled(string content, bool enabled)
|
|
{
|
|
// 在第一行查找
|
|
foreach (var element in FirstRow.Children)
|
|
{
|
|
if (element is Button button && button.Content.ToString() == content)
|
|
{
|
|
button.IsEnabled = enabled;
|
|
return;
|
|
}
|
|
}
|
|
|
|
// 在第二行查找
|
|
foreach (var element in SecondRow.Children)
|
|
{
|
|
if (element is Button button && button.Content.ToString() == content)
|
|
{
|
|
button.IsEnabled = enabled;
|
|
return;
|
|
}
|
|
}
|
|
|
|
// 在第三行查找
|
|
foreach (var element in ThirdRow.Children)
|
|
{
|
|
if (element is Button button && button.Content.ToString() == content)
|
|
{
|
|
button.IsEnabled = enabled;
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
|
|
// 数字键点击事件
|
|
private void KeyButton_Click(object sender, RoutedEventArgs e)
|
|
{
|
|
if (mode && PreviewTextBox.Text.Length > 0) return;
|
|
Button button = sender as Button;
|
|
if (button != null && button.IsEnabled)
|
|
{
|
|
string keyValue = button.Content.ToString();
|
|
PreviewTextBox.Text += keyValue;
|
|
|
|
// 触发按键事件
|
|
// KeyPressed?.Invoke(this, new KeyPressedEventArgs(KeyType.Character, keyValue));
|
|
}
|
|
}
|
|
|
|
// 退格键点击事件
|
|
private void Backspace_Click(object sender, RoutedEventArgs e)
|
|
{
|
|
if (PreviewTextBox.Text.Length > 0)
|
|
{
|
|
PreviewTextBox.Text = PreviewTextBox.Text.Substring(0, PreviewTextBox.Text.Length - 1);
|
|
|
|
// 触发退格事件
|
|
// KeyPressed?.Invoke(this, new KeyPressedEventArgs(KeyType.Backspace, ""));
|
|
}
|
|
}
|
|
|
|
// 回车键点击事件
|
|
private void Enter_Click(object sender, RoutedEventArgs e)
|
|
{
|
|
// 触发回车事件
|
|
KeyPressed?.Invoke(this, new KeyPressedEventArgs(KeyType.Enter, PreviewTextBox.Text));
|
|
|
|
// 隐藏键盘
|
|
this.Hide();
|
|
}
|
|
|
|
// 隐藏键点击事件
|
|
private void Hide_Click(object sender, RoutedEventArgs e)
|
|
{
|
|
KeyPressed?.Invoke(this, new KeyPressedEventArgs(KeyType.Hide, ""));
|
|
// 隐藏键盘
|
|
this.Hide();
|
|
}
|
|
|
|
public class KeyPressedEventArgs : EventArgs
|
|
{
|
|
public KeyType KeyType { get; }
|
|
public string KeyValue { get; }
|
|
|
|
public KeyPressedEventArgs(KeyType keyType, string keyValue)
|
|
{
|
|
KeyType = keyType;
|
|
KeyValue = keyValue;
|
|
}
|
|
}
|
|
private void Window_Loaded(object sender, RoutedEventArgs e)
|
|
{
|
|
// 初始化为默认模式
|
|
// SetKeyboardMode(KeyboardMode.Default);
|
|
PreviewTextBox.Text = null;
|
|
mode = false;
|
|
}
|
|
}
|
|
// 按键类型枚举
|
|
public enum KeyType
|
|
{
|
|
Character,
|
|
Backspace,
|
|
Enter,
|
|
Hide
|
|
}
|
|
|
|
// 按键事件参数
|
|
public class KeyPressedEventArgs : EventArgs
|
|
{
|
|
public KeyType KeyType { get; }
|
|
public string KeyValue { get; }
|
|
|
|
public KeyPressedEventArgs(KeyType keyType, string keyValue)
|
|
{
|
|
KeyType = keyType;
|
|
KeyValue = keyValue;
|
|
}
|
|
}
|
|
}
|
|
|