RaUI/Source/ryControls/Controls/Hotkey/HotkeyTextBox.cs
鑫Intel 6bc527a8a2 ### 2020-12-27 dev更新
------

#### ryControls    V2.1.2012.2701
- *.[改进]IconViewEx控件选中背景颜色支持渐变。
- *.[改进]支持设置IconViewEx控件选中字体颜色。
2020-12-27 17:03:04 +08:00

255 lines
7.6 KiB
C#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using SysFuns;
using ryCommon;
namespace ryControls
{
/// <summary>
/// 热键文本框
/// </summary>
public partial class HotkeyTextBox : UserControl,IMessageFilter
{
Keys m_Modifiers =Keys.None;
Keys m_Key =Keys.None;
/// <summary>
/// 热键文本框
/// </summary>
public HotkeyTextBox()
{
InitializeComponent();
//定义KeyPress事件
//base.KeyPress += delegate(object sender, KeyPressEventArgs e) { e.Handled = true; };
TxtHotKey.ReadOnly = true;
TxtHotKey.BackColor = Color.White;
TxtHotKey.KeyDown += TxtHotKey_KeyDown;
TxtHotKey.KeyUp += TxtHotKey_KeyUp;
TxtHotKey.Top = (Height - TxtHotKey.Height) / 2;
//Application.AddMessageFilter(this);
base.BackColor = Color.White;
}
/// <summary>
///
/// </summary>
~HotkeyTextBox()
{
// Application.RemoveMessageFilter(this);
}
/// <summary>
///
/// </summary>
/// <param name="m"></param>
/// <returns></returns>
public bool PreFilterMessage(ref Message m)
{
//if (m.Msg == 0x0100)
//{
// var key=(Keys)(m.WParam.ToInt32());
// return false;
//}
return false;
}
/// <summary>
/// 在控件大小变化时发生
/// </summary>
/// <param name="e"></param>
protected override void OnSizeChanged(EventArgs e)
{
TxtHotKey.Top = (Height - TxtHotKey.Height) / 2;
TxtHotKey.Width = Width - BtnClear.Width - 2;
BtnClear.Top = 0;
BtnClear.Left = TxtHotKey.Left + TxtHotKey.Width;
BtnClear.Height = Height;
}
private void TxtHotKey_KeyDown(object sender, KeyEventArgs e)
{
OnKeyDown(e);
}
private void TxtHotKey_KeyUp(object sender, KeyEventArgs e)
{
OnKeyUp(e);
}
//使文本属性失效掉
/// <summary>
/// 此属性无效
/// </summary>
[Description("此属性无效")]
public new string Text
{
get { return TxtHotKey.Text; }
set
{
LoadHotKey(value);
}
}
/// <summary>
/// 载入热键配置
/// </summary>
/// <param name="hotkey"></param>
public void LoadHotKey(string hotkey)
{
T_Modifiers = 0;
T_Key = Keys.None;
try
{
string[] item = hotkey.Split('+');
if (item.Length == 1 && item[0].IsInt())
{
T_Modifiers = 0;
T_Key = (Keys)item[0].ToInt(0);
}
else if (item.Length == 2 && item[0].IsInt() && item[1].IsInt())
{
T_Modifiers = item[0].ToInt(0);
T_Key = (Keys)item[1].ToInt(0);
}
}
catch
{
}
UpdateText();
}
private void UpdateText()
{
HotkeyValue xvalue = new HotkeyValue(T_Modifiers, (int)T_Key);
base.Text = xvalue.ToString();
TxtHotKey.Text = base.Text;
TxtHotKey.SelectionLength = 0;
TxtHotKey.SelectionStart = 0;
}
/// <summary>
/// 保存热键
/// </summary>
/// <returns></returns>
public string SaveHotKey()
{
if(T_Key==Keys.None)
{ return "0+0"; }
return T_Modifiers.ToString() + "+" + ((int)T_Key).ToString();
}
/// <summary>
/// 是否含义热键
/// </summary>
public bool HaveHotKey
{
get { return m_Key != Keys.None || (T_Modifiers==0 && T_Key>Keys.A && T_Key<Keys.Z); }
}
/// <summary>
/// 读取或设置热键
/// </summary>
public string HotKey
{
get { return SaveHotKey(); }
set { LoadHotKey(value); }
}
/// <summary>
/// 功能键
/// </summary>
[Description("功能键")]
public int T_Modifiers
{
get
{
int i_Keys = 0;
if ((m_Modifiers & Keys.Alt) != 0)
{ i_Keys |= 1; }
if ((m_Modifiers & Keys.Control) != 0)
{ i_Keys |= 2; }
if ((m_Modifiers & Keys.Shift) != 0 || (m_Modifiers & Keys.LShiftKey) != 0 || (m_Modifiers & Keys.RShiftKey) != 0)
{ i_Keys |= 4; }
if ((m_Modifiers & Keys.LWin) != 0 || (m_Modifiers & Keys.RWin) != 0)
{ i_Keys |= 8; }
return i_Keys;
}
set
{
m_Modifiers =Keys.None;
if ((value & 1) != 0)
{ m_Modifiers |=Keys.Alt; }
if ((value & 2) != 0)
{ m_Modifiers |= Keys.Control; }
if ((value & 4) != 0)
{ m_Modifiers |= Keys.Shift; }
if ((value & 8) != 0)
{ m_Modifiers |= Keys.LWin; }
UpdateText();
}
}
/// <summary>
/// 热键按键
/// </summary>
[Description("热键按键")]
public Keys T_Key
{
get
{
return m_Key;
}
set
{
m_Key = value;
UpdateText();
}
}
//TextBox的KeyDown事件
/// <summary>
/// KeyDown事件
/// </summary>
/// <param name="e"></param>
protected override void OnKeyDown(KeyEventArgs e)
{
//if (e.Modifiers == 0) //没有修改键
//{
// e.Handled = true;
// return;
//}
HotkeyValue value;
if (e.KeyCode == Keys.ShiftKey || e.KeyCode == Keys.ControlKey || e.KeyCode == Keys.Menu || e.KeyCode == Keys.None)
{//除修改键外,没有按键
value = new HotkeyValue(e.Modifiers, Keys.None);
}
else
{
value = new HotkeyValue(e.Modifiers, e.KeyCode);
}
m_Modifiers = value.Modifiers;
m_Key = value.KeyCode;
base.Text = value.ToString();//赋值
TxtHotKey.Text = base.Text;
e.Handled = true;
base.OnKeyDown(e);
}
//TextBox的KeyDown事件
/// <summary>
/// KeyDown事件
/// </summary>
/// <param name="e"></param>
protected override void OnKeyUp(KeyEventArgs e)
{
if(m_Modifiers!=Keys.None && m_Key==Keys.None)
{
m_Modifiers = Keys.None;
TxtHotKey.Text = "错误的热键";
}
e.Handled = false;
base.OnKeyUp(e);
}
private void BtnClear_Click(object sender, EventArgs e)
{
m_Modifiers =Keys.None;
m_Key = Keys.None;
UpdateText();
}
private void CtlHotkey_Paint(object sender, PaintEventArgs e)
{
e.Graphics.DrawRectangle(new Pen(Color.FromArgb(213, 216, 223)), new Rectangle(0, 0, this.Width - 1, this.Height - 1));
}
}
}