我的一亩三分地 我就喜欢!
13fen  设为主页
 收藏本站
 
当前位置: > 一亩三分地:首页 > 操作系统 > Linux > 使用经验 > 一份很有价值的子类化的源代码
热门文章排行
热门文章排行 一步一步的制作arm-linux交叉编译环(12-05)
深入理解硬盘的Linux分区(12-22)
深入浅出定制Linux系统环境变量(12-05)
Linux常用基本命令(二)(10-13)
红旗Linux5.0桌面正式版光盘安装{图(11-15)
精采文章排行
精采文章排行 vi基本技巧(11-16)
Linux历史篇(11-16)
Linux不是Windows(11-16)
第一次进入红旗后,应该做的几件事(11-15)
详解linux与win分区格式(11-15)
技术专题推荐
网管论坛交流
 

一份很有价值的子类化的源代码 

作者:佚名   来源:Linux 宝库   点击:   日期:2006-12-05


-->

  新建一个 ActiveX DLL 工程,名称 SmartSubClassLib
  
  ' 以下代码放在标准模块里,模块名 mSmartSubClass
  
  ' ----------------------------------------------------
  ' Module mSmartSubClass
  '
  ' Version... 1.0
  ' Date...... 24 April 2001
  '
  ' Copyright (C) 2001 Andr轶 Pons (andres@vbsmart.com)
  ' ----------------------------------------------------
  
  'API declarations:
  Option Explicit
  
  Public Const SSC_OLDPROC = "SSC_OLDPROC"
  Public Const SSC_OBJADDR = "SSC_OBJADDR"
  
  Private Declare Function GetProp Lib "user32" Alias "GetPropA" ( _
    ByVal hWnd As Long, _
    ByVal lpString As String) As Long
  
  Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" ( _
    Destination As Any, _
    Source As Any, _
    ByVal Length As Long)
  
  ' Function StartSubclassWindowProc()
  '
  ' This is the first windowproc that receives messages
  ' for all subclassed windows.
  ' The aim of this function is to just collect the message
  ' and deliver it to the right SmartSubClass instance.
  '
  Public Function SmartSubClassWindowProc( _
    ByVal hWnd As Long, _
    ByVal uMsg As Long, _
    ByVal wParam As Long, _
    ByVal lParam As Long) As Long
  
    Dim lRet As Long
    Dim oSmartSubClass As SmartSubClass
  
    'Get the memory address of the class instance...
    lRet = GetProp(hWnd, SSC_OBJADDR)
    
    If lRet <> 0 Then
      'oSmartSubClass will point to the class instance
      'without incrementing the class reference counter...
      CopyMemory oSmartSubClass, lRet, 4
      
      'Send the message to the class instance...
      SmartSubClassWindowProc = oSmartSubClass.WindowProc(hWnd, _
        uMsg, wParam, lParam)
  
      'Remove the address from memory...
      CopyMemory oSmartSubClass, 0&, 4
    End If
    
  End Function
  
  ' 以下代码放在类模块里,模块名 SmartSubClass
  
  ' ----------------------------------------------------
  ' Class SmartSubClass
  '
  ' Version... 1.0
  ' Date...... 24 April 2001
  ' ----------------------------------------------------
  
  Option Explicit
  
  'Public event:
  Public Event NewMessage( _
    ByVal hWnd As Long, _
    ByRef uMsg As Long, _
    ByRef wParam As Long, _
    ByRef lParam As Long, _
    ByRef Cancel As Boolean)
  
  'Private variables:
  Private m_hWnds() As Long
  
  'API declarations:
  Private Const GWL_WNDPROC = (-4)
  
  Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" ( _
    ByVal hWnd As Long, _
    ByVal nIndex As Long) As Long
  
  Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" ( _
    ByVal hWnd As Long, _
    ByVal nIndex As Long, _
    ByVal dwNewLong As Long) As Long
    
  Private 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
  
  Private Declare Function GetProp Lib "user32" Alias "GetPropA" ( _
    ByVal hWnd As Long, _
    ByVal lpString As String) As Long
    
  Private Declare Function SetProp Lib "user32" Alias "SetPropA" ( _
    ByVal hWnd As Long, _
    ByVal lpString As String, _
    ByVal hData As Long) As Long
  
  Private Declare Function RemoveProp Lib "user32" Alias "RemovePropA" ( _
    ByVal hWnd As Long, _
    ByVal lpString As String) As Long
  
  Private Declare Function IsWindow Lib "user32" ( _
    ByVal hWnd As Long) As Long
  
  '
  ' Function SubClassHwnd
  '
  ' This is the core function in this class.
  ' You can use it to both subclass and unsubclass a window.
  ' Once a window is subclassed the event NewMessage will
  ' be raised every time a message is sent to the window.
  '
  Public Function SubClassHwnd(ByVal hWnd As Long, _
    ByVal bSubClass As Boolean) As Boolean
  
    Dim lRet As Long
    
    lRet = 0
    
    'Make sure that hWnd is a valid window handler...
    If IsWindow(hWnd) Then
    
      If bSubClass Then
      'We are subclassing a window...
        
        'Make sure that the window wasn't already subclassed...
        If GetProp(hWnd, SSC_OLDPROC) = 0 Then
        
          'Now we subclass the window by changing its windowproc
          lRet = SetWindowLong(hWnd, GWL_WNDPROC, _
             AddressOf SmartSubClassWindowProc)
          
          'Check if we've managed to subclass...
          If lRet <> 0 Then
            'Store the old windowproc and the memory
            ' address of this class...
            SetProp hWnd, SSC_OLDPROC, lRet
            SetProp hWnd, SSC_OBJADDR, ObjPtr(Me)
            
            'Add the window to an internal list of
            ' subclassed windows...
            pAddHwndToList hWnd
          End If
        End If
      Else
      'We are unsubclassing a window...
      
        'Get the old windowproc...
        lRet = GetProp(hWnd, SSC_OLDPROC)
        
        If lRet <> 0 Then
          'Unsubclass the window...
          lRet = SetWindowLong(hWnd, GWL_WNDPROC, lRet)
        End If
        
        'Remove any extra information...
        RemoveProp hWnd, SSC_OLDPROC
        RemoveProp hWnd, SSC_OBJADDR
        
        'Remove the window from the internal list...
        pRemoveHwndFromList hWnd
      End If
    Else
      'If hWnd is not a valid window,
      'make sure that there isn't stored garbage...
      RemoveProp hWnd, SSC_OLDPROC
      RemoveProp hWnd, SSC_OBJADDR
      
      pRemoveHwndFromList hWnd
    End If
     
    SubClassHwnd = (lRet <> 0)
    
  End Function
  
  '
  ' Function WindowProc
  '
  ' This is the link between the windowproc and the class instance.
  ' Every time SmartSubClassWindowProc receives a window message,
  ' it will post it to the right class instance.
  '
  Friend Function WindowProc( _
    ByVal hWnd As Long, _
    ByVal uMsg As Long, _
    ByVal wParam As Long, _
    ByVal lParam As Long) As Long
  
    Dim lRet As Long
    Dim bCancel As Boolean
    
    bCancel = False
    
    WindowProc = 0
    
    'Raise the event NewMessage...
    'This will tell the owner of the class variable that a
    'new message is ready to be processed.
    'The owner will be able to cancel the message by setting
    'the variable bCancel to True.
    RaiseEvent NewMessage(hWnd, uMsg, wParam, lParam, bCancel)
    
    'If the event hasn't been canceled by the owner
    'we need to send it to the original windowproc
    If Not bCa






文章评论】 【收藏本文】 【推荐好友】 【打印本文】 【论坛讨论

   相关文章:
·轻松安装RedHat9.0 ·怎样安装Oracle9iforHP-UX双机
·安装过windows后如何安装linux ·深入理解硬盘的Linux分区
·在虚拟pc上安装linux操作系统 ·在Linux下安装BT服务器的捷径

   文章评论:(条)
  
 请留名: 匿名评论   点击查看所有评论 网管论坛
 

  责任编辑:一分  声明:刊登此文章是为了传递更多信息,文章内容仅供参考,转载请注明出处。