-- 作者:wangxinxin
-- 发布时间:2010-12-14 14:19:11
-- VB中子分类技术实例应用
子分类技术的原理:要先取得原先Window Procedure所在的地址,将之记录起来,接着设定所有的消息都先转到我们所写的消息处理过程上来,我们过滤传过来的消息,寻找特定的消息进行处理,其余的送回系统,由系统决定如何处理。等到我们不需要再处理这些特定的消息时,便取消消息的截取,即中止子分类过程。它一般需要三个过程:开始截取,消息处理,中止截取. 程序需要一个模块,在模块中声明如下: ’api函数Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" _ (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long)
A s Long Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" _ (ByVal hwnd As Long, ByVal nIndex As Long) As Long 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 Const GWL_WNDPROC = (-4) Public Const WM_MOUSEMOVE = &H200 Public Const WM_RBUTTONDOWN = &H204 ’全局变量,存放控件标志性数据Public preWinProc As Long ’本函数就是用来接收子分类时截取的消息的Public Function wndproc(ByVal hwnd As Long, ByVal Msg As Long, _ ByVal wParam As Long, ByVal lParam As Long) As Long ’截取下来的消息存放在msg参数中. if msg=WM_RBUTTONDOWN then ’检测到鼠标右击消息,这里就可以加入我们的处理代码如: msgbox "你好,鼠标右击消息!" ’需要注意,如果这儿不加入任何代码,则相当于吃掉了这条消息. else ’如果我们不是我们需要处理的消息,则将之送回原来的程序. wndproc = CallWindowProc(preWinProc, hwnd, Msg, wParam, lParam) endif End Function ’以下代码在窗体中: ’本例以截取一个combobox控件的消息为例,假设该’控件的名字是:comb1 Private Sub subclass() Dim ret As Long ’记录Window Procedure的地址 preWinProc = GetWindowLong(comb1.hwnd, GWL_WNDPROC)
|