To CNET:全局热键的例子,不知道有没有用
来源:互联网 发布:百电通软件下载 编辑:程序博客网 时间:2024/05/01 15:57
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
using System;
namespace Durius.Generics
{
public delegate void HotkeyEventHandler(int HotKeyID);
/// <summary>
/// System wide hotkey wrapper.
///
/// Robert Jeppesen
/// 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 pressed.
/// </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 pressed.</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 pressed 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 pressed.");
}
else if(HotkeyID == Hotkey2)
{
MessageBox.Show("CTRL+2 pressed.");
}
}
/// <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 method for Designer support - do not modify
/// the contents of this method with the code ediTor.
/// </summary>
private void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.butTon1 = new System.Windows.Forms.ButTon();
this.SuspendLayout();
//
// label1
//
this.label1.Location = new System.Drawing.Point(16, 8);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(200, 16);
this.label1.TabIndex = 0;
this.label1.Text = "Hotkeys:";
//
// label2
//
this.label2.Location = new System.Drawing.Point(16, 32);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(208, 16);
this.label2.TabIndex = 1;
this.label2.Text = "Win + 1";
//
// label3
//
this.label3.Location = new System.Drawing.Point(16, 56);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(208, 16);
this.label3.TabIndex = 2;
this.label3.Text = "Ctrl + 2";
//
// butTon1
//
this.butTon1.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.butTon1.Location = new System.Drawing.Point(88, 80);
this.butTon1.Name = "butTon1";
this.butTon1.TabIndex = 3;
this.butTon1.Text = "About";
this.butTon1.Click += new System.EventHandler(this.butTon1_Click);
//
// Form1
//
this.AuToScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(168, 123);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.butTon1,
this.label3,
this.label2,
this.label1});
this.Name = "Form1";
this.Text = "Hotkey";
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void butTon1_Click(object sender, System.EventArgs e)
{
MessageBox.Show(this, @"System wide hotkey wrapper
- Robert Jeppesen
http://www.durius.com", "Hotkey sample");
}
}
}
To CNET:全局热键的例子,不知道有没有用';return true">
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
类Hotkey.csusing System;
namespace Durius.Generics
{
public delegate void HotkeyEventHandler(int HotKeyID);
/// <summary>
/// System wide hotkey wrapper.
///
/// Robert Jeppesen
/// 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 pressed.
/// </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 pressed.</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 pressed 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 pressed.");
}
else if(HotkeyID == Hotkey2)
{
MessageBox.Show("CTRL+2 pressed.");
}
}
/// <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 method for Designer support - do not modify
/// the contents of this method with the code ediTor.
/// </summary>
private void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.butTon1 = new System.Windows.Forms.ButTon();
this.SuspendLayout();
//
// label1
//
this.label1.Location = new System.Drawing.Point(16, 8);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(200, 16);
this.label1.TabIndex = 0;
this.label1.Text = "Hotkeys:";
//
// label2
//
this.label2.Location = new System.Drawing.Point(16, 32);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(208, 16);
this.label2.TabIndex = 1;
this.label2.Text = "Win + 1";
//
// label3
//
this.label3.Location = new System.Drawing.Point(16, 56);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(208, 16);
this.label3.TabIndex = 2;
this.label3.Text = "Ctrl + 2";
//
// butTon1
//
this.butTon1.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.butTon1.Location = new System.Drawing.Point(88, 80);
this.butTon1.Name = "butTon1";
this.butTon1.TabIndex = 3;
this.butTon1.Text = "About";
this.butTon1.Click += new System.EventHandler(this.butTon1_Click);
//
// Form1
//
this.AuToScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(168, 123);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.butTon1,
this.label3,
this.label2,
this.label1});
this.Name = "Form1";
this.Text = "Hotkey";
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void butTon1_Click(object sender, System.EventArgs e)
{
MessageBox.Show(this, @"System wide hotkey wrapper
- Robert Jeppesen
http://www.durius.com", "Hotkey sample");
}
}
}
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
- To CNET:全局热键的例子,不知道有没有用
- 计算UDP校验和遇到的弱智问题。不知道对初学者有没有用。
- 计算UDP校验和遇到的弱智问题。不知道对初学者有没有用
- 注册全局热键应用例子
- Window 热键 以及全局热键的处理。
- 关于QQ热键在不知道的情况下找出热键组合的办法
- VC全局热键的实现
- 全局热键的一些知识
- Qt 全局热键的实现
- pyqt的全局键盘热键
- WINME/WIN2000/LINUX多重启动详解(4年前写的,不知道现在还有没有用)
- 彻底解决中文名文件下载和下载文件内容乱码问题!!!!! (不知道有没有用,我还没有试过)
- 全局热键
- 全局热键
- 全局热键
- 你不知道的Eclipse用法:全局搜索和更替
- C#注册全局热键的方法
- 实现gnome桌面的全局热键
- 在.NET中使用API的方法
- 漫谈.Net中的自动垃圾收集(Garbage Collection)机制(转)
- 用ASP.NET构建完整E-mail发送系统
- 轻松调整VB.NET控件
- 贴篇文章,BETA2中ACCESS操作数据库
- To CNET:全局热键的例子,不知道有没有用
- 除了使用hook外,下面的代码也能够在全局捕捉鼠标:(csdn)
- Visual Basic .NET 已不再支持的 Visual Basic 6.0 功能
- Dot Net的调试 - 2
- VB.NET与VB6的比较:被撤销的关键字和函数
- C#—非对称加密:加密文件
- c#中的反射
- 聚合函数
- HOW TO: Set a Windows Hook in Visual C# .NET