好库网 好库网首页 | 我的好库
VB爱好者

自定义组合热键 -VB资料

发布者:vbfans
发布日期:2011/4/10 15:00:46   更新日期:2011/4/10 15:00:46
阅读次数:2012
评分:4.80
介绍:自定义组合热键 -VB资料
正文:
通过截取系统消息,我们可以方便的实现自定义组合热键.
下面的例子实现了该功能:
>>步骤1----建立新工程;
>>步骤2----建立一个模块;
>>步骤3----在模块中写入如下代码:

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
Shell "Notepad", vbNormalFocus
End If
End If
End If
'如果不是热键信息则调用原来的程序
Wndproc = CallWindowProc(preWinProc, hWnd, Msg, _
wParam, lParam)
End Function

>>步骤4----在窗体中写入如下代码:
Private 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
Modifiers = MOD_ALT + MOD_CONTROL 'Alt+Ctrl 键
uVirtKey = vbKeyN 'N键
ret = RegisterHotKey(Me.hWnd, idHotKey, Modifiers, uVirtKey)
End Sub

Private Sub Form_Unload(Cancel As Integer)
Dim ret As Long
'取消Message的截取,使之送往原来的windows程序
ret = SetWindowLong(Me.hWnd, GWL_WNDPROC, preWinProc)
Call UnregisterHotKey(Me.hWnd, uVirtKey)
End Sub
>>步骤5----编译运行,按下Ctrl+Alt+N,看看是不是调出了记事本程序.

评论 [发表评论]
账号 密码 还没帐号呢,现在注册一个?

免责声明:好库网所展示的信息由买卖双方自行提供,其真实性、准确性和合法性由信息发布人负责。好库网不提供任何保证,并不承担任何法律责任。