关于窗体子类化
因为程序需要,我用WORD的VBA中插入了一个用户窗体USERFORM1,并且将该窗体子类化了。
但是程序运行后,窗体的标题栏似乎是一个“禁区”,无论在标题栏鼠标左击、右击,或者左击窗体右上角的关闭按钮,都会导致程序停止响应,包括WORD,我实在就迷茫了,不知道为什么,怎么解决?
跪求各位大神不吝赐教啊~!!~!
相关代码如下:
'在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 Function
[解决办法]
算了,还是我给一个解决方法吧,这个方法没有解决问题,而是逃避了问题,就是把窗体的标题栏隐藏。
具体代码如下,在窗体的Initialize事件中插入以下代码。(相应声明请自行补齐)
Private Sub UserForm_Initialize()
FrmHwnd = FindWindow(vbNullString, UserForm1.Caption)
Dim FrmStl As Long
FrmStl = GetWindowLong(FrmHwnd, GWL_STYLE)
FrmStl = FrmStl - WS_CAPTION + WS_EX_TOOLWINDOW
SetWindowLong FrmHwnd, GWL_STYLE, FrmStl
DrawMenuBar FrmHwnd
lpPrevWndProc = SetWindowLong(FrmHwnd, GWL_WNDPROC, AddressOf WindowProc)
End Sub