Disable the popup menu of a text box
A common Frequently Asked Question is how to disable the popup menu of a TextBox.
Since VB5 was released it's possible to use the AdressOf keyword to add a Hook and
a callback function. Just add this code to a .BAS module and call the Hook sub and
pass the hWnd of a textbox as an argument. You must call the UnHook sub before you
unload the form or you might get a General Protection Fault.
Declarations
Add the following code to the declarations section of a module. Declare
Function CallWindowProc Lib "user32" Alias "CallWindowProcA"
_
(ByVal lpPrevWndFunc As Long, _
ByVal hWnd As Long, _
ByVal Msg As Long, _
ByVal wParam As Long, _
ByVal lParam As Long) As Long Declare Function SetWindowLong Lib
"user32" _
Alias "SetWindowLongA" _
(ByVal hWnd As Long, _
ByVal nIndex As Long, _
ByVal dwNewLong As Long) As Long Public Const GWL_WNDPROC = -4
Public Const WM_RBUTTONUP = &H205
Public lpPrevWndProc As Long
Private lngHWnd As Long
Module Code
Add the
following code to the module. Public Sub
Hook(hWnd As Long)
lngHWnd=hWnd
lpPrevWndProc = SetWindowLong(lngHWnd, GWL_WNDPROC, _
AddressOf WindowProc)
End Sub Public Sub UnHook()
Dim lngReturnValue As Long
lngReturnValue = SetWindowLong(lngHWnd, GWL_WNDPROC, lpPrevWndProc)
End Sub Function WindowProc(ByVal hw As Long, _
ByVal uMsg As Long, _
ByVal wParam As Long, _
ByVal lParam As Long) As Long Select Case uMsg
Case WM_RBUTTONUP
'Do nothing
'Or popup you own menu
Case Else
WindowProc = CallWindowProc(lpPrevWndProc, hw, _
uMsg, wParam, lParam)
End Select
End Function
Form Code
Add the
following code to the Form_Load event of the form where the
text box is placed: Call Hook(Text1.hWnd)
Where Text1 is the name of the text box you want to Subclass.
Add the following code to the Form_Unload event: Call UnHook
|