![]() |
|
||||||||||||||
| | 网站首页 | 数据库教程 | web编程 | 服务器 | 程序设计 | | ||
|
||
|
||||||
| To CNET:全局热键的例子,不知道有没有用 | ||||||
作者:佚名 文章来源:不详 点击数: 更新时间:2007-7-28 ![]() |
||||||
|
正在装载数据…… 类Hotkey.cs using System; namespace Durius.Generics { public delegate void HotkeyEventHandler(int HotKeyID); /// <summary> /// System wide hotkey wrapper. /// /// Send bugs to robert@durius.com /// </summary> public class Hotkey : System.Windows.Forms.IMessageFilter { System.Collections.Hashtable keyIDs = new System.Collections.Hashtable(); IntPtr hWnd; /// <summary> /// Occurs when a hotkey has been divssed. /// </summary> public event HotkeyEventHandler OnHotkey; public enum KeyFlags { MOD_ALT = 0x1, MOD_CONTROL = 0x2, MOD_SHIFT = 0x4, MOD_WIN = 0x8 } [System.Runtime.InteropServices.DllImport("user32.dll")] public static extern UInt32 RegisterHotKey( IntPtr hWnd, UInt32 id, UInt32 fsModifiers, UInt32 vk); [System.Runtime.InteropServices.DllImport("user32.dll")] public static extern UInt32 UnregisterHotKey( IntPtr hWnd, UInt32 id); [System.Runtime.InteropServices.DllImport("kernel32.dll")] public static extern UInt32 GlobalAddAtom( String lpString ); [System.Runtime.InteropServices.DllImport("kernel32.dll")] public static extern UInt32 GlobalDeleteAtom( UInt32 nAtom ); /// <summary> /// Constructor. Adds this instance to the MessageFilters so that this class can raise Hotkey events /// </summary> /// <param name="hWnd">A valid hWnd, i.e. form1.Handle</param> public Hotkey(IntPtr hWnd) { this.hWnd = hWnd; System.Windows.Forms.Application.AddMessageFilter(this); } /// <summary> /// Register a system wide hotkey. /// </summary> /// <param name="hWnd">form1.Handle</param> /// <param name="Key">Your hotkey</param> /// <returns>ID integer for your hotkey. Use this to know which hotkey was divssed.</returns> public int RegisterHotkey(System.Windows.Forms.Keys Key, KeyFlags keyflags) { UInt32 hotkeyid = GlobalAddAtom(System.Guid.NewGuid().ToString()); RegisterHotKey( (IntPtr)hWnd, hotkeyid, (UInt32)keyflags, (UInt32)Key); keyIDs.Add(hotkeyid, hotkeyid); return (int)hotkeyid; } /// <summary> /// Unregister hotkeys and delete atoms. /// </summary> public void UnregisterHotkeys() { System.Windows.Forms.Application.RemoveMessageFilter(this); foreach (UInt32 key in keyIDs.Values) { UnregisterHotKey(hWnd, key); GlobalDeleteAtom(key); } } public bool PreFilterMessage(ref System.Windows.Forms.Message m) { if (m.Msg == 0x312) /*WM_HOTKEY*/ { if(OnHotkey != null) { foreach (UInt32 key in keyIDs.Values) { if((UInt32)m.WParam == key) { OnHotkey((int)m.WParam); return true; } } } } return false; } } } 测试程序Form1.cs using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; using Durius.Generics; namespace TestGenerics { /// <summary> /// Summary description for Form1. /// </summary> public class Form1 : System.Windows.Forms.Form { Hotkey hotkey; int Hotkey1; int Hotkey2; private System.Windows.Forms.Label label1; private System.Windows.Forms.Label label2; private System.Windows.Forms.Label label3; private System.Windows.Forms.Button button1; /// <summary> /// Required designer variable. /// </summary> private System.ComponentModel.Container components = null; public Form1() { // // Required for Windows Form Designer support // InitializeComponent(); /* * Initialize our hotkeys. Pass in a handle to the main form in the constructor, * then call RegisterHotkey for each of our hotkey combinations and wire up * the event */ hotkey = new Hotkey(this.Handle); Hotkey1 = hotkey.RegisterHotkey(System.Windows.Forms.Keys.D1, Hotkey.KeyFlags.MOD_WIN); Hotkey2 = hotkey.RegisterHotkey(System.Windows.Forms.Keys.D2, Hotkey.KeyFlags.MOD_CONTROL); hotkey.OnHotkey += new HotkeyEventHandler(OnHotkey); } /// <summary> /// The hotkey event handler. If you have several hotkeys, you will have to check /// which one was divssed using HotkeyID. /// RegisterHotkey returns the HotkeyID that was assigned to your hotkey. /// </summary> /// <param name="HotkeyID"></param> public void OnHotkey(int HotkeyID) { this.Activate(); if(HotkeyID == Hotkey1) { MessageBox.Show("WIN+1 divssed."); } else if(HotkeyID == Hotkey2) { MessageBox.Show("CTRL+2 divssed."); } } /// <summary> /// Clean up any resources being used. /// </summary> protected override void Dispose( bool disposing ) { if( disposing ) { hotkey.UnregisterHotkeys(); if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } #region Windows Form Designer generated code /// <summary> /// Required met
|
||||||
| 文章录入:admin 责任编辑:admin | ||||||
| 【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口】 | ||||||
| 网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!) |
| | 设为首页 | 加入收藏 | 联系站长 | 友情链接 | 版权申明 | 网站公告 | 网站地图 | 管理登录 | | |||
|