主题:VB自定义消息响应 求求各位了!帮帮我吧!小弟感激!
VB工程
已经创建窗体 FORM1
模块 Module1
*********窗体 FORM1 代码如下***********
---------------------------------------
Private Sub Form_Load()
HOOKMsg Me.hwnd
End Sub
Private Sub Form_Unload(Cancel As Integer)
UNHOOKMsg Me.hwnd
End Sub
Private Function OnComm()
MsgBox ("已经接受自定义消息")
End Function
----------------------------------------
********模块 Module1 代码如下************
----------------------------------------
Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Public 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
Public Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Public Const GW_CHILD = 5
Public Const GW_HWNDNEXT = 2
Public Const WM_USER = &H400
Public Const ERROR_SUCCESS = 0&
Private Const GWL_WNDPROC = (-4)
Public Const WM_MYMESSAGE = WM_USER + 100
Private PrevProc As Long
Public hwndp As Long
Private Function MyWndProc(ByVal hwnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
If Msg = WM_MYMESSAGE Then '消息为自定义消息
Form1.Print "自定义消息"
Form1.OnCom >>>>>>>>>>>>>>>>>>>>加入这句函数调用则程序关闭,VB环境也关闭,无任何提示
>>>>>>>>>>>>>>>>>>>>去掉则正常
Else
'MyWndProc = CallWindowProc(PrevProc, hwnd, Msg, wParam, lParam)
End If
End Function
Public Sub HOOKMsg(ByVal hwnd As Long)
PrevProc = SetWindowLong(hwnd, GWL_WNDPROC, AddressOf MyWndProc)
'修改窗口消息响应
End Sub
Public Sub UNHOOKMsg(ByVal hwnd As Long)
Call SetWindowLong(hwnd, GWL_WNDPROC, PrevProc)
'还原窗口消息响应
End Sub
----------------------------------
在模块中有句函数调用如下
Form1.OnCom >>>>>>>>>>>>>>>>>>>>加入这句函数调用则程序关闭,VB环境也关闭,无任何提示
>>>>>>>>>>>>>>>>>>>>去掉则正常
而在这里如果加如分支选择结构也同样会崩溃
SELECT CASE
CASE
CASE
ELSE CASE
END
弄了很久了!苦求帮助谢谢啊!
已经创建窗体 FORM1
模块 Module1
*********窗体 FORM1 代码如下***********
---------------------------------------
Private Sub Form_Load()
HOOKMsg Me.hwnd
End Sub
Private Sub Form_Unload(Cancel As Integer)
UNHOOKMsg Me.hwnd
End Sub
Private Function OnComm()
MsgBox ("已经接受自定义消息")
End Function
----------------------------------------
********模块 Module1 代码如下************
----------------------------------------
Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Public 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
Public Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Public Const GW_CHILD = 5
Public Const GW_HWNDNEXT = 2
Public Const WM_USER = &H400
Public Const ERROR_SUCCESS = 0&
Private Const GWL_WNDPROC = (-4)
Public Const WM_MYMESSAGE = WM_USER + 100
Private PrevProc As Long
Public hwndp As Long
Private Function MyWndProc(ByVal hwnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
If Msg = WM_MYMESSAGE Then '消息为自定义消息
Form1.Print "自定义消息"
Form1.OnCom >>>>>>>>>>>>>>>>>>>>加入这句函数调用则程序关闭,VB环境也关闭,无任何提示
>>>>>>>>>>>>>>>>>>>>去掉则正常
Else
'MyWndProc = CallWindowProc(PrevProc, hwnd, Msg, wParam, lParam)
End If
End Function
Public Sub HOOKMsg(ByVal hwnd As Long)
PrevProc = SetWindowLong(hwnd, GWL_WNDPROC, AddressOf MyWndProc)
'修改窗口消息响应
End Sub
Public Sub UNHOOKMsg(ByVal hwnd As Long)
Call SetWindowLong(hwnd, GWL_WNDPROC, PrevProc)
'还原窗口消息响应
End Sub
----------------------------------
在模块中有句函数调用如下
Form1.OnCom >>>>>>>>>>>>>>>>>>>>加入这句函数调用则程序关闭,VB环境也关闭,无任何提示
>>>>>>>>>>>>>>>>>>>>去掉则正常
而在这里如果加如分支选择结构也同样会崩溃
SELECT CASE
CASE
CASE
ELSE CASE
END
弄了很久了!苦求帮助谢谢啊!