〥羽〥 發表於 2008-12-25 02:43:58

[轉貼]VB2005為一個項目加上全域熱鍵功能

1.定義一些必要的API函數、常量和變數   
Public   Declare   Function   RegisterHotKey   Lib   "user32"   (ByVal   hWnd   As   Integer,   ByVal   id   As   Integer,   ByVal   fsModifiers   As   Integer,   ByVal   vk   As   Integer)   As   Integer   
          Public   Declare   Function   UnregisterHotKey   Lib   "user32"   (ByVal   hWnd   As   Integer,   ByVal   id   As   Integer)   As   Integer   
   
          Public   Const   WM_HOTKEY   As   Short   =   &H312S   
          Public   Const   MOD_ALT   As   Short   =   &H1S   
          Public   Const   MOD_CONTROL   As   Short   =   &H2S   
          Public   Const   MOD_SHIFT   As   Short   =   &H4S   
   
          Public   preWinProc   As   Integer   
          Public   uVirtKey1,   Modifiers,   idHotKey   As   Integer   
   
          Public   Structure   taLong   
                  Dim   ll   As   Integer   
          End   Structure   
   
          Public   Structure   t2Int   
                  Dim   lWord   As   Short   
                  Dim   hword   As   Short   
          End   Structure   
   
2.先註冊熱鍵   
Private   Sub   Form1_Load(ByVal   sender   As   Object,   ByVal   e   As   System.EventArgs)   Handles   MyBase.Load   
                  Dim   ret   As   Integer   
   
                  idHotKey   =   100   ''in   the   range   &h0000   through   &hBFFF   
                  Modifiers   =   MOD_ALT   ''輔助鍵為Alt   
                  uVirtKey1   =   System.Windows.Forms.Keys.D   ''註冊的熱鍵為Alt+D   
                  '註冊熱鍵   
                  ret   =   Module1.RegisterHotKey(Me.Handle.ToInt32,   idHotKey,   Modifiers,   uVirtKey1)   
                  If   ret   =   0   Then   
                        MsgBox("註冊熱鍵失敗,請使用其它熱鍵!",   MsgBoxStyle.Critical,   "錯誤")   
                  End   If   
          End   Sub   
記得在表單關閉時登出熱鍵哦!!!   
          Private   Sub   Form1_Closing(ByVal   sender   As   Object,   ByVal   e   As   System.ComponentModel.CancelEventArgs)   Handles   MyBase.Closing   
                  Module1.UnregisterHotKey(Me.Handle.ToInt32,   uVirtKey1)   
          End   Sub   
3.然後展開表單內碼表最上面那一句“Windows   表單設計器生成的代碼”,找到其中New的那一段,在這一段後面加上這麼一個函數(其實哪裡都行,只是我喜歡放在這裡   ^_^)   
<System.Security.Permissions.PermissionSetAttribute(System.Security.Permissions.SecurityAction.Demand,   Name:="FullTrust")>   _   
                  Protected   Overrides   Sub   WndProc(ByRef   m   As   Message)   
                  Dim   lp   As   taLong   
                  Dim   i2   As   t2Int   
                  Dim   TempData   As   String   
                  '   Listen   for   operating   system   messages   
                  Select   Case   (m.Msg)   
                        '   The   WM_ACTIVATEAPP   message   occurs   when   the   application   
                        '   becomes   the   active   application   or   becomes   inactive.   
                  Case   WM_HOTKEY   
                                  MsgBox("HotKey1")   
                                  If   m.WParam.ToInt32   =   idHotKey   Then   
                                          lp.ll   =   m.LParam.ToInt32   
   
                                          i2.lWord   =   lp.ll   And   &HFFFF   
                                          i2.hword   =   (lp.ll   And   &HFFFF0000)   >>   16   
   
                                          MsgBox(i2.hword.ToString   &   ","   &   i2.lWord)   
   
                                          If   (i2.lWord   =   Modifiers)   And   i2.hword   =   uVirtKey1   Then   
                                                If   System.Windows.Forms.Clipboard.GetDataObject.GetDataPresent(System.Windows.Forms.DataFormats.Text)   Then       '這一段的判斷好像不是很準確,我還沒仔細研究,不好意思了哦   
                                                          TempData   =   System.Windows.Forms.Clipboard.GetDataObject.GetData(System.Windows.Forms.DataFormats.Text)   
                                                End   If   
   
                                                MsgBox("剪貼板中的資料是文本格式的!"   &   vbCrLf   &   "內容為:   "   &   TempData)   
                                          End   If   
                                  End   If   
                  End   Select   
                  MyBase.WndProc(m)   
          End   Sub   
頁: [1]
查看完整版本: [轉貼]VB2005為一個項目加上全域熱鍵功能