Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleRemove the context menu that appears when the user right clicks on a TextBox
Description
Keywordscontext menu, popup menu, TextBox
CategoriesAPI, Controls
 
My book Custom Controls Library disables context menus to implement several custom controls.

Warning: Subclassing is dangerous. The debugger does not work well when a new WindowProc is installed. If you halt the program instead of unloading its forms, it will crash and so will the Visual Basic IDE. Save your work often! Don't say you weren't warned.

When the form loads, subclass to watch for WM_CONTEXTMENU messages.

 
Private Sub Form_Load()
    OldWindowProc = SetWindowLong( _
        Text1.hWnd, GWL_WNDPROC, _
        AddressOf NewWindowProc)
End Sub
 
The new WindowProc intercepts and ignores WM_CONTEXTMENU messages.
 
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_CONTEXTMENU = &H7B
' Pass along all messages except the one that
' makes the context menu appear.
Public Function NewWindowProc(ByVal hWnd As Long, ByVal msg _
    As Long, ByVal wParam As Long, ByVal lParam As Long) As _
    Long
Const WM_NCDESTROY = &H82

    ' If we're being destroyed,
    ' restore the original WindowProc.
    If msg = WM_NCDESTROY Then
        SetWindowLong _
            hWnd, GWL_WNDPROC, _
            OldWindowProc
    End If

    If msg <> WM_CONTEXTMENU Then _
        NewWindowProc = CallWindowProc( _
            OldWindowProc, hWnd, msg, wParam, _
            lParam)
End Function
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated