自定义winform 的keypass事件 求优化!
//添加自定义类
- C# code
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Runtime.InteropServices;using System.Windows.Forms;namespace 编辑器{ public class HotKey: Form1 { //委托 key 函数 public delegate void delegateKeyPress(object sender, EventArgsKey e); // 自增ID 标示 private int idForDiff = 0; public IntPtr hWnd{get;set;} //当前空中句柄 public Control control { get; set; } //要注册的按键: public List<keyNode> keys { get; set; } /// <summary> /// 如果函数执行成功,返回值不为0。 /// 如果函数执行失败,返回值为0。要得到扩展错误信息,调用GetLastError。 /// /// </summary> /// <param name="hWnd"></param> /// <param name="id"></param> /// <param name="fsModifiers"></param> /// <param name="vk"></param> /// <returns></returns> [DllImport("user32.dll ", SetLastError = true)] public static extern bool RegisterHotKey( IntPtr hWnd, //要定义热键的窗口的句柄 int id, //定义热键ID(不能与其它ID重复) KeyModifiers fsModifiers, //标识热键是否在按Alt、Ctrl、Shift、Windows等键时才会生效 Keys vk //定义热键的内容 ); [DllImport("user32.dll ", SetLastError = true)] public static extern bool UnregisterHotKey( IntPtr hWnd, //要取消热键的窗口的句柄 int id //要取消热键的ID ); //定义了辅助键的名称(将数字转变为字符以便于记忆,也可去除此枚举而直接使用数值) [Flags()] public enum KeyModifiers { None = 0, Alt = 1, Ctrl = 2, Shift = 4, WindowsKey = 8 } //构造函数 public HotKey(IntPtr hwnd) { this.control = null; this.hWnd = hwnd; this.keys = new List<keyNode>();//对象init } //构造函数 public HotKey(Control control) { this.control = control; this.hWnd = control.Handle; this.keys = new List<keyNode>();//对象init } //注册按键: public void AddKeyToRegister(IEnumerable<keyNode> keys) { foreach (keyNode key in keys) { keyNode k = new keyNode(key.key,key.keyMode,key.keyPassFunction); HotKey.RegisterHotKey(this.hWnd, this.idForDiff, k.keyMode, k.key); k.keyPassFunction = key.keyPassFunction; k.id = this.idForDiff; this.idForDiff++; this.keys.Add(k); } } public void AddKeyToRegister(Keys key,HotKey.KeyModifiers keyMode, delegateKeyPress keyFunction) { keyNode k = new keyNode(key, keyMode, keyFunction); HotKey.RegisterHotKey(this.hWnd, this.idForDiff, k.keyMode, k.key); k.id = this.idForDiff; this.idForDiff++; this.keys.Add(k); } } //每一个Key对象 public class keyNode { public Keys key { get; set; } public 编辑器.HotKey.KeyModifiers keyMode { get; set; } public int id { get; set; } public 编辑器.HotKey.delegateKeyPress keyPassFunction { get; set; } public keyNode(Keys key, 编辑器.HotKey.KeyModifiers keyModefier,编辑器.HotKey.delegateKeyPress keyPassFunction ) { this.key = key; this.keyMode = keyModefier; this.keyPassFunction = keyPassFunction; } } public class EventArgsKey : EventArgs { public Keys key { get; set; } public int keyId { get; set; } public EventArgsKey(Keys k,int id) { this.key = k; this.keyId = id; } }}
在winform 中应用:
- C# code
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using System.IO;namespace 编辑器{ public partial class Form1 : Form { public Form1() { InitializeComponent(); } public HotKey keyManager; private void Form1_Load(object sender, EventArgs e) { keyManager = new HotKey(this); keyManager.AddKeyToRegister(Keys.X, HotKey.KeyModifiers.Ctrl, this.ctrlXPress); } //按键 事件 public void ctrlXPress(object sender, EventArgsKey e) { MessageBox.Show("ctrl+X"); }//下面这段代码少不了,我想提到 自定义类中,不让使用都看到,可是想来想去,想不出方法。// /// <summary> /// key Press 按键重写 /// </summary> /// <param name="m">message</param> protected override void WndProc(ref Message m) { const int WM_HOTKEY = 0x0312; //按快捷键 if (m.Msg == WM_HOTKEY) { foreach (keyNode key in keyManager.keys) { if (key.id == m.WParam.ToInt32()) { if (key.keyPassFunction != null) { key.keyPassFunction(((keyManager.control != null) ? keyManager.control : null), new EventArgsKey(key.key, key.id)); } } } } base.WndProc(ref m); } } }
[解决办法]
定义一个类,继承自 Form,WndProc放到这个类里,还是override,你的窗体继承自自定义类,并且 WndProc中,先base.WndProc(ref m);这样可以现在自定义类中做一些处理