win7自动壁纸切换小工具AutoDesk三:全局热键

全局热键,博客园有很多现成的文章,下面的实现感觉封装的比较好,用起来也比较方便(感谢博客园的网友)

 1 using System;  
 2 using System.Runtime.InteropServices;  
 3   
 4 namespace SystemHotKey  
 5 {  
 6     public delegate void HotkeyEventHandler(int HotKeyID);  
 7   
 8     public class Hotkey : System.Windows.Forms.IMessageFilter  
 9     {  
10         System.Collections.Hashtable keyIDs = new System.Collections.Hashtable();  
11         IntPtr hWnd;  
12   
13         public event HotkeyEventHandler OnHotkey;  
14   
15         public enum KeyFlags  
16         {  
17             MOD_ALT = 0x1,  
18             MOD_CONTROL = 0x2,  
19             MOD_SHIFT = 0x4,  
20             MOD_WIN = 0x8  
21         }  
22         [DllImport("user32.dll")]  
23         public static extern UInt32 RegisterHotKey(IntPtr hWnd, UInt32 id, UInt32 fsModifiers, UInt32 vk);  
24   
25         [DllImport("user32.dll")]  
26         public static extern UInt32 UnregisterHotKey(IntPtr hWnd, UInt32 id);  
27   
28         [DllImport("kernel32.dll")]  
29         public static extern UInt32 GlobalAddAtom(String lpString);  
30   
31         [DllImport("kernel32.dll")]  
32         public static extern UInt32 GlobalDeleteAtom(UInt32 nAtom);  
33   
34         public Hotkey(IntPtr hWnd)  
35         {  
36             this.hWnd = hWnd;  
37             System.Windows.Forms.Application.AddMessageFilter(this);  
38         }  
39   
40         public int RegisterHotkey(System.Windows.Forms.Keys Key, KeyFlags keyflags)  
41         {  
42             UInt32 hotkeyid = GlobalAddAtom(System.Guid.NewGuid().ToString());  
43             RegisterHotKey((IntPtr) hWnd, hotkeyid, (UInt32) keyflags, (UInt32) Key);  
44             keyIDs.Add(hotkeyid, hotkeyid);  
45             return (int) hotkeyid;  
46         }  
47   
48         public void UnregisterHotkeys()  
49         {  
50             System.Windows.Forms.Application.RemoveMessageFilter(this);  
51             foreach (UInt32 key in keyIDs.Values)  
52             {  
53                 UnregisterHotKey(hWnd, key);  
54                 GlobalDeleteAtom(key);  
55             }  
56         }  
57   
58         public bool PreFilterMessage(ref System.Windows.Forms.Message m)  
59         {  
60             if (m.Msg == 0x312)  
61             {  
62                 if (OnHotkey != null)  
63                 {  
64                     foreach (UInt32 key in keyIDs.Values)  
65                     {  
66                         if ((UInt32) m.WParam == key)  
67                         {  
68                             OnHotkey((int) m.WParam);  
69                             return true;  
70                         }  
71                     }  
72                 }  
73             }  
74             return false;  
75         }  
76     }  
77 }  

 

该类的使用方法:

在窗体的类中声明一个变量 
private int Hotkey1
在窗体的Load事件中加入如下代码 

 

 1 /// <summary>
 2 /// 窗体启动时挂载全局快捷键Ctrl+F1.
 3 /// </summary>
 4 /// <param name="sender"></param>
 5 /// <param name="e"></param>
 6 private void AutoDesk_Load(object sender, EventArgs e)
 7 {
 8 try
 9 {
10 
11 SystemHotKey.Hotkey hotkey;
12 hotkey = new SystemHotKey.Hotkey(this.Handle); 
13 
14 //定义快键(Ctrl + F1) 
15 Hotkey1 = hotkey.RegisterHotkey(System.Windows.Forms.Keys.F1, SystemHotKey.Hotkey.KeyFlags.MOD_CONTROL);
16 hotkey.OnHotkey += new SystemHotKey.HotkeyEventHandler(OnHotkey);
17 
18 }
19 catch (Exception ex)
20 {
21 MessageBox.Show(ex.Message);
22 }
23 }

 

 

热键的响应函数如下:

 

 1 /// <summary>
 2         /// 快捷键按下后切换到下一个随即壁纸。
 3         /// </summary>
 4         /// <param name="HotkeyID"></param>
 5          public void OnHotkey(int HotkeyID)  
 6          {
 7              try
 8              {
 9                  if (HotkeyID == Hotkey1)
10                  {
11                      switch_to_next_waller();
12                  }
13                  else
14                  {
15                      //this.Visible = false;  
16                  }
17              }
18              catch (Exception ex)
19              {
20                  MessageBox.Show(ex.Message);
21              }
22          }  

 

 

 

posted @ 2012-07-05 23:07  linbirg  阅读(385)  评论(0编辑  收藏  举报