来源:网络 | 2006-12-19 | (有2172人读过)
热键是什么东西想必大家都明白,它的应用可以大大加快我们操作的速度。有时候看一个人使用热键的频率就可以看出他对这个软件的熟悉程度。在自己的程序中使用热键同样会使操作更简便。现在我们就来谈谈vb中的两种热键用法。 第一种:定义激活窗口的热键 这个热键的功能是无论在哪个程序中,只要按下我们定义的这组热键就可以激活某个窗体。这里我们要用到sendmessage这个API函数。程序如下: Option Explicit Private Declare Function SendMessage Lib “user32” Alias “SendMessageA” (ByVal_ hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long Const WM_SETHOTKEY = &H32 Const HOTKEYF_SHIFT = &H1 ' shift键 Const HOTKEYF_CONTROL = &H2 'ctrl键 Const HOTKEYF_ALT = &H4 'alt键 Const HOTKEYF_EXT = &H8 '附加键 Private Type tInteger aint As Integer End Type Private Type t2Byte lByte As Byte hByte As Byte End Type Private ii As tInteger Private bb As t2Byte Private Sub Command1_Click() Dim wParam As Long, I As Long '设定ctl-shift-T为该window的hotkey bb.hByte = HOTKEYF_CONTROL Or HOTKEYF_SHIFT bb.lByte = vbKeyT LSet ii = bb wParam = CLng(ii.aint) I = SendMessage(Me.hwnd, WM_SETHOTKEY, wParam, 0) If I = 1 Then Debug.Print “Ctl-Shift-T为hotkey” Else If I = 2 Then Debug.Print “有其他Window也用Ctl-Shift-T作Hotkey” Else Debug.Print “指定失败” End If End If End Sub 注: SendMessage()的传回值有以下的意义: -1 hotkey 设定不对 0 hWnd的指定有错 1 成功,并且没有其他window用相同的hotkey 2 成功,但是有其他window使用相同的hotkey 第二种:定义运行某个程序的热键 在DOS时代,我们常用拦截中断向量的方法来实现热键的功能。现在我们可以用RegisterHotKey这个API函数来定义热键。当热键注册过后,无论我们在哪个程序中按下这组热键,系统会把相关信息传递给等待接收这个信息的窗口,再由这个窗口来处理信息,完成指定功能。所以在接收信息的窗口中还要采用子分类的方法来处理,程序结束时用unRegisterHotKey来取消热键的定义。 RegisterHotKey的参数结构如下: RegisterHotKey(ByVal hwnd As Long,//接收Hotkey的Window ByVal idHotKey as Long, // 热键的ID,范围从 0x0000 到 0xBFFF ByVal Modifiers As Long, // 定义alt shift control等的组合 ByVal uVirtKey As Long // 低端的键值 ) 下面我们就来看看具体程序,这个程序的功能是无论在哪个程序中按下ALT-SHIFT-G就执行记事本程序: '以下程序放在模块中 Option Explicit Declare Function SetWindowLong Lib “user32” Alias “SetWindowLongA”_ (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long Declare Function GetWindowLong Lib “user32” Alias “GetWindowLongA”_ (ByVal hwnd As Long, ByVal nIndex As Long) As Long Declare Function CallWindowProc Lib “user32” Alias “CallWindowProcA”_ (ByVal lpPrevWndFunc As Long, ByVal hwnd As Long, ByVal Msg As Long, _ ByVal wParam As Long, ByVal lParam As Long) As Long Declare Function RegisterHotKey Lib “user32” (ByVal hwnd As Long, ByVal id As Long, ByVal fsModifiers As Long, ByVal vk As Long) As Long Declare Function UnregisterHotKey Lib “user32” (ByVal hwnd As Long, ByVal id As Long) As Long Public Const WM_HOTKEY = &H312 Public Const MOD_ALT = &H1 Public Const MOD_CONTROL = &H2 Public Const MOD_SHIFT = &H4 Public Const GWL_WNDPROC = (-4) Public preWinProc As Long Public Modifiers As Long, uVirtKey As Long, idHotKey As Long Private Type taLong ll As Long End Type Private Type t2Int lWord As Integer hword As Integer End Type Public Function wndproc(ByVal hwnd As Long, ByVal Msg As Long, _ ByVal wParam As Long, ByVal lParam As Long) As Long If Msg = WM_HOTKEY Then If wParam = idHotKey Then Dim lp As taLong, i2 As t2Int lp.ll = lParam LSet i2 = lp If (i2.lWord = Modifiers) And i2.hword = uVirtKey Then Debug.Print “HotKey Shift-Alt-G Pressed ” Shell “notepad”, vbNormalFocus End If End If End If '如果不是热键信息则调用原来的程序 wndproc = CallWindowProc(preWinProc, hwnd, Msg, wParam, lParam) End Function '以下程序放在窗体中 Sub Form_Load() Dim ret As Long '记录原来的window程序地址 preWinProc = GetWindowLong(Me.hwnd, GWL_WNDPROC) '用自定义程序代替原来的window程序 ret = SetWindowLong(Me.hwnd, GWL_WNDPROC, AddressOf wndproc) idHotKey = 1 'in the range &h0000 through &hBFFF Modifiers = MOD_ALT + MOD_SHIFT uVirtKey = vbKeyG '注册热键 ret =RegisterHotKey(Me.hwnd, idHotKey, Modifiers, uVirtKey) End Sub Private Sub Form_Unload(Cancel As Integer) Dim ret As Long '取消Message的截取,使之送往原来的window程序 ret = SetWindowLong(Me.hwnd, GWL_WNDPROC, preWinProc) Call UnregisterHotKey(Me.hwnd, uVirtKey) End Sub 怎么样,对热键的定义有所了解了吧?通过这两种方法我想可以满足程序中对热键的需求了,try it。
|