微软没有发现的VB与VBA的BUG——关于窗体子类化
很多人认为理论上VB与VBA没有区别,可是事实真的如此吗?因为程序需要,我用WORD在VBA中插入了一个用户窗体USERFORM1,并且将该窗体子类化了。
但是程序运行后,窗体的标题栏似乎是一个“禁区”,无论在标题栏鼠标左击、右击,或者左击窗体右上角的关闭按钮,都会导致程序停止响应,包括WORD,可是在VB环境中却不会出现这种现象……
我实在就迷茫了,不知道为什么,怎么解决?
跪求各位大神不吝赐教啊~!!~!
相关代码如下:
'在USERFORM1中
Private Sub UserForm_Click()
Unload UserForm1
End Sub
Private Sub UserForm_Initialize()
FrmHwnd = FindWindow(vbNullString, UserForm1.Caption)
lpPrevWndProc = SetWindowLong(FrmHwnd, GWL_WNDPROC, AddressOf WindowProcTest)
End Sub
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
SetWindowLong FrmHwnd, GWL_WNDPROC, lpPrevWndProc
End Sub
'在模块中
Public Const GWL_WNDPROC = -4
Public lpPrevWndProc As Long
Public FrmHwnd As Long
Public Declare Function SetWindowLong _
Lib "user32" _
Alias "SetWindowLongA" (ByVal hwnd As Long, _
ByVal nIndex As Long, _
ByVal dwNewLong As Long) As Long
Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) 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 Function WindowProcTest(ByVal hwnd As Long, _
ByVal uMsg As Long, _
ByVal wParam As Long, _
ByVal lParam As Long) As Long
WindowProcTest = CallWindowProc(lpPrevWndProc, hwnd, uMsg, wParam, lParam)
End