Date: 02-16-2022
Return to Index
created by gbSnippets
'Subclassing is used to modify a control's response to a message. It does so by
'creating a 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.
'Compiler Comments:
'This code is written to compile with PBWin10. To compile with PBWin9,
'add this line:
#Include "CommCtrl.inc"
'Primary Code:
'Do this in WM_IniDialog
OrigProc& = SetWindowLong(GetDlgItem(hDlg, %ID_Control), %GWL_WndProc, Codeptr(NewProc)) 'subclass a control
'Do this in WM_Destroy
SetWindowLong hTextBox, %GWL_WNDPROC, OldProc& '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 Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As 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 capture the KeyUp event, which is not
'normally available in the dialog Callback for a RichEdit control.
#Compiler PBWin 10
#Compile EXE
#Dim All
%Unicode=1
#Include "win32api.inc"
#Include "RichEdit.inc"
Global hDlg as Dword, hRichEdit as Dword, OldProc&
Function PBMain() As Long
Local style&, buf$
buf$ = "This is sample" + $CrLf + "text for the" + $CrLf + "edit control."
style& = %WS_Child Or %WS_Visible Or %ES_MultiLine Or %WS_VScroll Or %ES_AutoHScroll _
Or %WS_HScroll Or %ES_AutoVScroll Or %ES_WantReturn Or %ES_NoHideSel Or %WS_TabStop
Dialog New Pixels, 0, "Test Code",300,300,200,150, %WS_OverlappedWindow To hDlg
Control Add Button, hDlg, 100,"Push", 30,10,140,20
LoadLibrary("riched32.dll") : InitCommonControls
Control Add "RichEdit", hDlg, 500, buf$,20,40,160,100, style&
Control Handle hDlg, 500 To hRichEdit
Dialog Show Modal hDlg Call DlgProc
End Function
CallBack Function DlgProc() As Long
Select Case CB.Msg
Case %WM_InitDialog
OldProc& = SetWindowLong(GetDlgItem(hDlg, 500), %GWL_WndProc, Codeptr(NewProc)) 'subclass
Case %WM_Destroy
SetWindowLong hRichEdit, %GWL_WNDPROC, OldProc& 'un-subclass
End Select
End Function
Function NewProc(ByVal hWnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Select Case Msg
Case %WM_KeyUp 'trap key up, for syntax color check while editing
MsgBox "Subclassing: KeyUp"
End Select
Function = CallWindowProc(OldProc&, hWnd, Msg, wParam, lParam)
End Function
'gbs_00285
'Date: 03-10-2012
http://www.garybeene.com/sw/gbsnippets.htm