Date: 02-16-2022
Return to Index
created by gbSnippets
'Gets control handle from hDlg/control ID. Useful where API require the control handl,
'or where the control handle needs to be checked in a message.
'Primary Code:
hControl = GetDlgItem( hDlg, %ID_Control)
'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.
'Note that the GetDlgItem() function is used to provide SetWindowLong with the textbox
'control handle.
#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 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(GetDlgItem(hDlg, %ID_Control), %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
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_00023
'Date: 03-10-2012
http://www.garybeene.com/sw/gbsnippets.htm