这个程式在VB6中可行,在VB2005不能运行,望高手修改
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)
'##这里报错,AddressOf 应加上委托,但我不知么样加
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
这个程式在VB6可以通过,但在VB2005中报错,有二个地方出错,请高手过目修改一下:
一, ret = SetWindowLong(Me.hwnd, GWL_WNDPROC, AddressOf wndproc)
'##这里报错,AddressOf 应加上委托,但我不知么样加
二,
LSet i2 = lp
不同类型不能转换