borderAndreaVB free resources for Visual Basic developersborder

AndreaVB Visual Basic and VB.NET source code resources - Copyright © 1999-2008 Andrea Tincani
:: How to Subclass a Form

Author  

Andrea Rossignoli

Language  

VB5, VB6

Operating Systems  

Windows 95, 98 and NT
API Declarations

'==============inside a MODULE
Option Explicit
'************************************************************
'API
'************************************************************


Private Declare Function CallWindowProc Lib "user32.dll" 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 SetWindowLong Lib "user32.dll" Alias "SetWindowLongA" ( _
    ByVal hWnd As Long, _
    ByVal nIndex As Long, _
    ByVal dwNewLong As Long) As Long

'************************************************************
'Constants
'************************************************************


Private Const GWL_WNDPROC = -4
Private Const WM_MOUSEMOVE = &H200

'************************************************************
'Variables
'************************************************************


Private hControl As Long
Private lPrevWndProc As Long

Module

'*************************************************************
'WindowProc
'*************************************************************

Private Function WindowProc(ByVal lWnd As Long, ByVal lMsg As Long, _
    ByVal wParam As Long, ByVal lParam As Long) As Long
   
'Selects which messages you want to detect
    Select Case lMsg
       
'Example:
        Case WM_MOUSEMOVE
            Form1.Label1.Caption = "MouseMove(" & lMsg & ") Buttons=" & wParam & _
            " Y=" & (lParam Mod 65536) & " X=" & (lParam \ 65535)
    End Select
   
'Sends message to previous procedure
    'This is VERY IMPORTANT!!!

    WindowProc = CallWindowProc(lPrevWndProc, lWnd, lMsg, wParam, lParam)
End Function

'*************************************************************
'Hook
'*************************************************************

Public Sub Hook(ByVal hControl_ As Long)
    hControl = hControl_
    lPrevWndProc = SetWindowLong(hControl, GWL_WNDPROC, AddressOf WindowProc)
End Sub

'*************************************************************
'Unhook
'*************************************************************

Public Sub Unhook()
    Call SetWindowLong(hControl, GWL_WNDPROC, lPrevWndProc)
End Sub

Usage

'Create a Form with a label
Option Explicit

'===========inside a form
'*************************
'USAGE
'*************************


Private Sub Form_Load()
    Hook Form1.hWnd
End Sub

Private Sub Form_Unload(Cancel As Integer)
    Unhook
End Sub

:: Navigation

Home

Miscellaneous API Tips

Previous Tip

Next Tip

:: Search this site
Google
:: Related Topics
icon 05-05-2006 Re: MsgHook.dll download? by luckyboy
icon 19-12-2005 Re: Passing Font color from form to form by JLRodgers
icon 16-11-2005 Detecting Unload by Vpa
icon 20-09-2005 Re: How to disable resize form by stickleprojects
icon 13-07-2005 Re: Application Deactivate by RayZore
:: Sponsored Links



Partners: Il portale per lui e lei | Download Actual Software | Free Software Download
borderAndreaVB free resources for Visual Basic developersborder

borderAndreaVB Visual Basic and VB.NET source code resources - Copyright © 1999-2008 Andrea Tincaniborder