如何在VB中实现自定义消息的响应函数
答案:1 悬赏:50 手机版
解决时间 2021-03-28 18:41
- 提问者网友:喧嚣尘世
- 2021-03-28 02:25
如何在VB中实现自定义消息的响应函数
最佳答案
- 五星知识达人网友:鱼芗
- 2021-03-28 04:05
大神给你来个例子 子类化窗口回调
窗体模块 代码开始
Option Explicit
Private Sub Command1_Click()
SendMessage frm1.hwnd, WM_USER + 1, 123, 45
End Sub
Private Sub Form_Load()
SetTimer Me.hwnd, 0, 1000, 0
pOldProc = GetWindowLong(Me.hwnd, GWL_WNDPROC)
SetWindowLong Me.hwnd, GWL_WNDPROC, AddressOf MyProc
End Sub
Private Sub Form_Unload(Cancel As Integer)
KillTimer Me.hwnd, 0
End Sub
窗体模块代码 结束
标准模块 x.bas文件开始
Option Explicit
Public Declare Function GetWindowLong Lib "user32.dll" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
Public Declare Function SetWindowLong Lib "user32.dll" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Public Declare Function CallWindowProc Lib "user32.dll" 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 SetTimer Lib "user32.dll" (ByVal hwnd As Long, ByVal nIDEvent As Long, ByVal uElapse As Long, ByVal lpTimerFunc As Long) As Long
Public Declare Function KillTimer Lib "user32.dll" (ByVal hwnd As Long, ByVal nIDEvent As Long) As Long
Public Const GWL_WNDPROC As Long = -4
Public Const WM_SYSCOMMAND As Long = &H112&
Public Const SC_CONTEXTHELP As Long = &HF180&
Public Const WM_TIMER As Long = &H113
Public pOldProc As Long
Public Declare Function SendMessage Lib "user32.dll" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Public Const WM_USER As Long = &H400
Function MyProc(ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Select Case wMsg
Case WM_USER + 1
frm1.Print "消息 WM_USER+1, wMsg:" & wMsg & ", wParam:" & wParam & ", lParam:" & lParam
Case WM_SYSCOMMAND
If wParam = SC_CONTEXTHELP Then
Shell "notepad c:windowssystem32eula.txt", vbNormalFocus
End If
Case WM_TIMER
'frm1.Cls
'frm1.Print Now
frm1.Caption = Now
End Select
MyProc = CallWindowProc(pOldProc, hwnd, wMsg, wParam, lParam)
End Function
标准模块x.bas文件结束
窗体模块 代码开始
Option Explicit
Private Sub Command1_Click()
SendMessage frm1.hwnd, WM_USER + 1, 123, 45
End Sub
Private Sub Form_Load()
SetTimer Me.hwnd, 0, 1000, 0
pOldProc = GetWindowLong(Me.hwnd, GWL_WNDPROC)
SetWindowLong Me.hwnd, GWL_WNDPROC, AddressOf MyProc
End Sub
Private Sub Form_Unload(Cancel As Integer)
KillTimer Me.hwnd, 0
End Sub
窗体模块代码 结束
标准模块 x.bas文件开始
Option Explicit
Public Declare Function GetWindowLong Lib "user32.dll" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
Public Declare Function SetWindowLong Lib "user32.dll" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Public Declare Function CallWindowProc Lib "user32.dll" 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 SetTimer Lib "user32.dll" (ByVal hwnd As Long, ByVal nIDEvent As Long, ByVal uElapse As Long, ByVal lpTimerFunc As Long) As Long
Public Declare Function KillTimer Lib "user32.dll" (ByVal hwnd As Long, ByVal nIDEvent As Long) As Long
Public Const GWL_WNDPROC As Long = -4
Public Const WM_SYSCOMMAND As Long = &H112&
Public Const SC_CONTEXTHELP As Long = &HF180&
Public Const WM_TIMER As Long = &H113
Public pOldProc As Long
Public Declare Function SendMessage Lib "user32.dll" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Public Const WM_USER As Long = &H400
Function MyProc(ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Select Case wMsg
Case WM_USER + 1
frm1.Print "消息 WM_USER+1, wMsg:" & wMsg & ", wParam:" & wParam & ", lParam:" & lParam
Case WM_SYSCOMMAND
If wParam = SC_CONTEXTHELP Then
Shell "notepad c:windowssystem32eula.txt", vbNormalFocus
End If
Case WM_TIMER
'frm1.Cls
'frm1.Print Now
frm1.Caption = Now
End Select
MyProc = CallWindowProc(pOldProc, hwnd, wMsg, wParam, lParam)
End Function
标准模块x.bas文件结束
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯