UnHook子类的安全方法(Unhook Subclassing When Windows is Ready)

Unhook Subclassing When Windows is Ready
Don’t unhook your Windows procedures from Form_Unload when
subclassing forms. When you subclass forms, the hook is often set
during Form_Load with code like this:
    OriginalProc = SetWindowLong Me.hWnd, _
                   GWL_WNDPROC, AddressOf MyWindowProc
A common mistake is forgetting to put the corresponding unhook
call in your Form_Unload event:(很容易忘了在Form_Unload 事件写如下代码)
      SetWindowLong Me.hWnd, GWL_WNDPROC, _
              AddressOf OriginalProc
If you forget to reinstate the old procedure in your Form_Unload
event, it’s bye-bye VB. Instead, add this code within your
subclassing procedure:(其实可以在安装子类时就写上如下窗口过程代码,就可以了,而且不怕进入调试模式!!!!)
Select Case Msg
Case WM_NCDESTROY
    If OriginalProc <> 0 Then
         Call SetWindowLong(hWnd, _
            GWL_WNDPROC, OriginalProc)
        OriginalProc=0
    End If
Case ...

This code restores the original procedure automatically when the
window is destroyed. To make it even safer, place all your
subclassing code in a separate DLL and debug your subclassed
forms without worrying about the Integrated Development Environment
(IDE) crashing. You can always move the code back to
your EXE when it’s fully debugged.

posted on 2006-09-17 20:38  杨志农  阅读(104)  评论(0)    收藏  举报

导航