All Controls - SubClassing

Category: Controls - .Techniques

Date: 02-16-2022

Return to Index


 
'Subclassing is used to modify a control's response to a message. It does so by
'creating window procedure within the PowerBASIC app which intercepts
'messages normally sent to the control, allowing the app to override the response
'of the default window procedure.
 
'An application with a subclassed control can pass any messages not processed by the
'new window procedure to the previous window procedure by calling CallWindowProc. Thus,
'the new window procedure can choose to respond to any or all messages.
 
'Primary Code:
OrigProc& = SetWindowLong(hControl, %GWL_WndProc, Codeptr(NewProc))   'subclass a control
SetWindowLong hControl, %GWL_WNDPROC, OrigProc&   'un-subclass, restore original window procedure
 
'Then you must provide a new window procedure for that control. It MUST be declared like this:
Function NewProc(ByVal hWnd As LongByVal Msg As LongByVal wParam As LongByVal lParam As LongAs Long
   'include normal callback function processing ...
   Select Case Msg
      Case X
         ... custom processing...
         Function = 0 : Exit Function    'use if no further message processing is required
      Case Y
         ... custom processing...
         Function = 0 : Exit Function    'use if no further message processing is required
   End Select
   Function = CallWindowProc(OldProc&, hWnd, Msg, wParam, lParam)   'send unprocessed messages to the original procedure
End Function
 
 
'Compilable Example:  (Jose Includes)
'In this example, subclassing is used to prevent the normal context menu from
'appearing when a textbox is right-mouse clicked.  In such a case, the application
'might provide its own custom context menu. One textbox is shown that is not subclassed
'and one is shown that is subclassed. Right-click each to see the effect of subclassing.
#Compiler PBWin 9, PBWin 10
#Compile EXE
#Dim All
%Unicode=1
#Include "Win32API.inc"
Global hDlg As Dword, hTextBox as Dword, OldProc&
%ID_Control = 500
 
Function PBMain() As Long
   Dialog New Pixels, 0, "Test Code",300,300,200,200, %WS_OverlappedWindow To hDlg
   Control Add TextBox, hDlg, %ID_Control, "Right-mouse click me!", 20,10,120,20
   Control Handle hDlg, %ID_Control To hTextBox
   Control Add TextBox, hDlg, 200, "Right-mouse click me!", 20,40,120,20
   Dialog Show Modal hDlg Call DlgProc
End Function
 
CallBack Function DlgProc() As Long
   Select Case CB.Msg
      Case %WM_InitDialog
         OldProc& = SetWindowLong(hTextBox, %GWL_WndProc, Codeptr(NewProc))  'subclass
      Case %WM_Destroy
         SetWindowLong hTextBox, %GWL_WNDPROC, OldProc&   'un-subclass
   End Select
End Function
 
Function NewProc(ByVal hWnd As LongByVal Msg As LongByVal wParam As LongByVal lParam As LongAs Long
   Select Case Msg
      Case %WM_ContextMenu
         MsgBox "custom context Menu would appear, not the default One!"
         Function = 0 : Exit Function
   End Select
   Function = CallWindowProc(OldProc&, hWnd, Msg, wParam, lParam)
End Function
 
'gbs_00107
'Date: 03-10-2012


created by gbSnippets
http://www.garybeene.com/sw/gbsnippets.htm