* UIForm:增加全局热键

This commit is contained in:
Sunny 2021-10-25 15:27:28 +08:00
parent c9bb1b0faf
commit 0996892fd0
5 changed files with 125 additions and 1 deletions

Binary file not shown.

Binary file not shown.

93
SunnyUI/Common/UHotKey.cs Normal file
View File

@ -0,0 +1,93 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Sunny.UI
{
public class HotKeyEventArgs : EventArgs
{
public readonly HotKey hotKey;
public readonly DateTime dateTime;
public HotKeyEventArgs(HotKey hotKey, DateTime time)
{
this.hotKey = hotKey;
this.dateTime = time;
}
}
public delegate void HotKeyEventHandler(object sender, HotKeyEventArgs e);
public class HotKey : IEquatable<HotKey>
{
internal static int CalculateID(ModifierKeys modifiers, Keys key)
{
return (int)modifiers + ((int)key << 4);
}
public readonly ModifierKeys ModifierKey;
public readonly Keys Key;
public readonly int id;
internal HotKey(ModifierKeys modifiers, Keys key)
{
this.ModifierKey = modifiers;
this.Key = key;
this.id = HotKey.CalculateID(modifiers, key);
}
public override int GetHashCode()
{
return this.id.GetHashCode();
}
public override bool Equals(object obj)
{
return (obj is HotKey) && this.Equals(obj as HotKey);
}
public bool Equals(HotKey other)
{
return other != null && other.id == this.id;
}
public override string ToString()
{
return $"{ModifierKey} {Key}";
}
}
/// <summary>
/// Specifies the set of modifier keys.
/// flags enum, 4 bits, 0 == none
/// 1111
/// </summary>
[Flags]
public enum ModifierKeys
{
//
// Summary:
// No modifiers are pressed.
None = 0,
//
// Summary:
// The ALT key.
Alt = 1,
//
// Summary:
// The CTRL key.
Control = 2,
//
// Summary:
// The SHIFT key.
Shift = 4,
//
// Summary:
// The Windows logo key.
Windows = 8
}
}

View File

@ -29,6 +29,7 @@
* 2021-08-17: V3.0.8 IFrame接口 * 2021-08-17: V3.0.8 IFrame接口
******************************************************************************/ ******************************************************************************/
using Sunny.UI.Win32;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.ComponentModel; using System.ComponentModel;
@ -1482,6 +1483,23 @@ namespace Sunny.UI
} }
} }
public void RegisterHotKey(Sunny.UI.ModifierKeys modifierKey, Keys key)
{
if (hotKeys == null) hotKeys = new Dictionary<int, HotKey>();
int id = HotKey.CalculateID(modifierKey, key);
if (!hotKeys.ContainsKey(id))
{
HotKey newHotkey = new HotKey(modifierKey, key);
this.hotKeys.Add(id, newHotkey);
Win32.User.RegisterHotKey(Handle, id, (int)newHotkey.ModifierKey, (int)newHotkey.Key);
}
}
public event HotKeyEventHandler HotKeyEventHandler;
private Dictionary<int, HotKey> hotKeys;
#region #region
protected override void WndProc(ref Message m) protected override void WndProc(ref Message m)
@ -1490,6 +1508,12 @@ namespace Sunny.UI
{ {
case Win32.User.WM_ERASEBKGND: case Win32.User.WM_ERASEBKGND:
m.Result = IntPtr.Zero; m.Result = IntPtr.Zero;
break;
case Win32.User.WM_HOTKEY:
int hotKeyId = (int)(m.WParam);
if (hotKeys.ContainsKey(hotKeyId))
HotKeyEventHandler?.Invoke(this, new HotKeyEventArgs(hotKeys[hotKeyId], DateTime.Now));
break; break;
default: default:
base.WndProc(ref m); base.WndProc(ref m);

View File

@ -1,4 +1,6 @@
namespace Sunny.UI using System;
namespace Sunny.UI
{ {
partial class UIForm partial class UIForm
{ {
@ -15,6 +17,11 @@
{ {
this.UnRegister(); this.UnRegister();
foreach (var hotKey in this.hotKeys.Values)
{
Win32.User.UnregisterHotKey(IntPtr.Zero, hotKey.id);
}
if (disposing && (components != null)) if (disposing && (components != null))
{ {
components.Dispose(); components.Dispose();