feat: 新增时间输入和Alt按钮快捷键

This commit is contained in:
cyf
2026-08-15 22:26:11 +08:00
parent a65dfab897
commit 57dedb5e9d
23 changed files with 818 additions and 233 deletions
@@ -0,0 +1,122 @@
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using DevExpress.XtraEditors;
namespace Lskj.Control.Model
{
/// <summary>
/// 将按钮原有的 &amp; 助记键改为必须按 Alt 才能触发的快捷键。
/// </summary>
public sealed class AltButtonShortcutManager
{
private sealed class ShortcutItem
{
public SimpleButton Button;
public Keys Key;
}
private readonly List<ShortcutItem> mShortcutItems =
new List<ShortcutItem>();
private bool mUpdatingText;
/// <summary>
/// 注册按钮快捷键,并移除标题中的 &amp;,保持界面显示为“按钮名(X)”。
/// </summary>
public void Register(SimpleButton button, Keys defaultKey)
{
if (button == null)
{
return;
}
ShortcutItem item = mShortcutItems.Find(
shortcut => ReferenceEquals(shortcut.Button, button));
if (item == null)
{
item = new ShortcutItem { Button = button, Key = defaultKey };
mShortcutItems.Add(item);
button.TextChanged += OnButtonTextChanged;
}
UpdateShortcut(item, defaultKey);
}
/// <summary>
/// 只响应 Alt+原助记键;未按 Alt、同时按下其他修饰键时均不处理。
/// </summary>
public bool ProcessKey(Keys keyData)
{
if ((keyData & Keys.Modifiers) != Keys.Alt)
{
return false;
}
Keys keyCode = keyData & Keys.KeyCode;
foreach (ShortcutItem item in mShortcutItems)
{
SimpleButton button = item.Button;
if (item.Key == keyCode && button != null &&
!button.IsDisposed && button.Visible && button.Enabled)
{
button.PerformClick();
return true;
}
}
return false;
}
private void OnButtonTextChanged(object sender, EventArgs e)
{
if (mUpdatingText)
{
return;
}
SimpleButton button = sender as SimpleButton;
ShortcutItem item = mShortcutItems.Find(
shortcut => ReferenceEquals(shortcut.Button, button));
if (item != null)
{
UpdateShortcut(item, item.Key);
}
}
private void UpdateShortcut(ShortcutItem item, Keys defaultKey)
{
string text = item.Button.Text ?? string.Empty;
int mnemonicIndex = FindMnemonicIndex(text);
if (mnemonicIndex >= 0)
{
item.Key = (Keys)char.ToUpperInvariant(text[mnemonicIndex + 1]);
mUpdatingText = true;
try
{
item.Button.Text = text.Remove(mnemonicIndex, 1);
}
finally
{
mUpdatingText = false;
}
}
else
{
item.Key = defaultKey;
}
}
private static int FindMnemonicIndex(string text)
{
for (int index = 0; index < text.Length - 1; index++)
{
if (text[index] == '&' && char.IsLetterOrDigit(text[index + 1]))
{
return index;
}
}
return -1;
}
}
}
+21 -4
View File
@@ -2095,8 +2095,6 @@ namespace Lskj.Control.Model
case ControlType.LabDate:
case ControlType.LabDateTime:
case ControlType.LabDateTimeShort:
case ControlType.LabTime:
case ControlType.LabShortTime:
// 日期控件
LabelDateEdit dateEdit = ctr as LabelDateEdit;
if (string.IsNullOrWhiteSpace(dateEdit.FormatType))
@@ -2109,6 +2107,12 @@ namespace Lskj.Control.Model
}
//value = dateEdit.TextEdit.EditValue == null ? dateEdit.EditText : dateEdit.TextEdit.EditValue.ToString();
break;
case ControlType.LabTime:
case ControlType.LabShortTime:
// 时间控件只返回时分秒部分,不附带日期。
LabelTimeEdit timeEdit = ctr as LabelTimeEdit;
value = timeEdit == null ? string.Empty : timeEdit.EditText;
break;
case ControlType.LabDateHalfDay:
// 日期半天控件
LabelDateHalfDayEdit dateHalfDayEdit = ctr as LabelDateHalfDayEdit;
@@ -4519,11 +4523,20 @@ namespace Lskj.Control.Model
Font mRowFont = new Font("Microsoft YaHei UI", 12);
switch (model.FieldType)
{
case ControlType.LabTime:
case ControlType.LabShortTime:
// 时间控件使用 TimeEdit,避免 DateEdit 显示日期日历。
LabelTimeEdit timeEdit = new LabelTimeEdit();
timeEdit.TextEdit.KeyDown += new KeyEventHandler(OnTextEditKeyDown);
timeEdit.TextEdit.TextChanged += new EventHandler(OnTextEditTextChanged);
// 时间类型固定使用时分或时分秒,避免旧的日期格式配置覆盖为年月日。
timeEdit.TimeFormat(DateFormat.Format(model.FieldType));
if (!isLoadBorder) timeEdit.TextEdit.BorderStyle = DevExpress.XtraEditors.Controls.BorderStyles.NoBorder;
baseControl = timeEdit;
break;
case ControlType.LabDate:
case ControlType.LabDateTime:
case ControlType.LabDateTimeShort:
case ControlType.LabTime:
case ControlType.LabShortTime:
case ControlType.LabShortDate:
// 日期控件
LabelDateEdit dateEdit = new LabelDateEdit();
@@ -6699,6 +6712,10 @@ namespace Lskj.Control.Model
LabelDateEdit dateEdit = controlObj as LabelDateEdit;
value = dateEdit.TextEdit.EditValue == null ? dateEdit.EditText : dateEdit.TextEdit.DateTime.ToString("yyyy-MM-dd");
}
else if (controlObj is LabelTimeEdit)
{
value = ((LabelTimeEdit)controlObj).EditText;
}
else if (controlObj is LabelCheckEdit)
{
LabelCheckEdit checkEdit = controlObj as LabelCheckEdit;