Date: 02-16-2022
Return to Index
created by gbSnippets
'An textbox provides a basic cut/copy/paste context menu. By using subclassing,
'a programmer can prevent the menu from showing or can substitute one of their
'own.
'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(GetDlgItem(hDlg, %ID_Control), %GWL_WndProc, Codeptr(NewProc)) 'subclass a control
SetWindowLong hTextBox, %GWL_WNDPROC, OldProc& 'un-subclass, restore original window procedure
'Then you must provide a new window procedure for the textbox. It MUST be declared with the
'following arguments:
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 %WM_Command
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. A substitute context menu
'is then provided. Note that the popup menu can be placed anywhere on the screen,
'so coordinates must be provided to place it within the dialog (usually by the mouse).
#Compiler PBWin 9, PBWin 10
#Compile EXE
#Dim All
%Unicode=1
#Include "Win32API.inc"
Global hDlg As Dword, hContext as Dword, hTextBox as Dword, OldProc&
%ID_Control = 600
Function PBMain() As Long
Local style&
style& = %WS_TabStop Or %WS_Border Or %ES_Left Or %ES_AutoHScroll Or %ES_MultiLine Or %ES_NoHideSel Or %ES_WantReturn
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, style&
Control Handle hDlg, %ID_Control TO hTextBox
CreateCustomPopupMenu
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 Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Local x as Long, y as Long
Select Case Msg
Case %WM_ContextMenu
x = Lo(Integer,lParam) : y = Hi(Integer, lParam) 'WM_ContextMenu returns xy coordinates of mouse
TrackPopupMenu hContext, %TPM_LEFTALIGN, x, y, 0, hDlg, ByVal 0 'put context menu where mouse is
Function = 0 : Exit Function
End Select
Function = CallWindowProc(OldProc&, hWnd, Msg, wParam, lParam)
End Function
Sub CreateCustomPopUpMenu
Menu New Popup To hContext
Menu Add String, hContext, "Jump", 500, %MF_Enabled
Menu Add String, hContext, "Shout", 501, %MF_Enabled
Menu Add String, hContext, "Sing", 502, %MF_Enabled
Menu Add String, hContext, "Dance", 503, %MF_Enabled
End Sub
'gbs_00129
'Date: 03-10-2012
http://www.garybeene.com/sw/gbsnippets.htm