!!挑战windows API大师:怎样在控件里实现Subclass和hook的引用?
独立控件项目里有一个UserControl,一个Module,UserControl里有一个Control,我要在UserControl里用Windows API利用Subclass技术探测并改变Control的类型,并用HOOK探测Windows消息并重绘它,这所有的一切如果不用UserControl而用普通的控件,那么我已完全搞定,不需别人帮忙!但若将普通的控件换成UserControl,我发现控件无法探测到SetWindowsHookEx()设置的WinProc,剩下的工作无法完成!
例示程序代码如下:
Private Sub UserControl_Initialize()
With App
'
' Create a hook to monitor messages sent to window procedures.
' The system calls fAppHook before passing the message to the
' receiving window procedure.
'
' Then in fAppHook we can modify the controls to our liking.
'
mlHook = SetWindowsHookEx(WH_CALLWNDPROC, AddressOf fAppHook, .hInstance, .ThreadID)
'
' Remove the hook.
Call UnhookWindowsHookEx(mlHook)
End With
OwnerDraw
End Sub
Private Sub OwnerDraw()
' Subclass the Parent of the control
' which will be modified as owner drawn controls. Save the
' address of the original window procedure in the registry.
'
mlWndProc = SetWindowLong(GetParent(ListBox1.hwnd), GWL_WNDPROC, AddressOf fAppWndProc)
ListBox1.ListIndex = 0
End Sub
注:1、fAppHook、fAppWndProc均在Module中,而且代码无误。
2、如果将上述独立控件项目改为EXE项目,UserControl换成form里的普通控件,然后将UserControl_Initialize()中的代码放在Main()中,OwnerDraw()中的代码放在普通控件所在的form中的Form_Load()中,则所有程序将正确执行,毫无问题!
3、有人建议我将UserControl_Initialize()和OwnerDraw()放在控件项目里的Main()中或其他地方,我试过,仍然不行!
请windows API大师指点:我应将UserControl_Initialize()和OwnerDraw()中的代码放于独立控件项目何处才能使SetWindowsHookEx()设置的WinProc生效!