Login | Register   
RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.

Tip of the Day
Language: VB5,VB6
Expertise: Intermediate
Jan 1, 2000



Application Security Testing: An Integral Part of DevOps

Suppress default Edit popup menu in TextBox controls with subclassing

When you right-click a TextBox control, Windows sends it a WM_CONTEXTMENU message, to which VB reacts by displaying the default Edit popup menu, that contains editing commands such as Cut, Copy, Paste, and Select All. Using a subclassing technique you can easily trap this message before it reaches the original window procedure, and suppress the default Edit menu or substitute it with a custom popup menu. Thanks to the MsgHook DLL this is a trivial task:


' you can omit the following constant definition, 
' because it is contained in the MsgHook type library

Dim WithEvents TextBoxHook As MsgHook

Private Sub Form_Load()
    ' subclass the Text1 control
    Set TextBoxHook = New MsgHook
    TextBoxHook.StartSubclass Text1.hWnd
End Sub

Private Sub TextBoxHook_BeforeMessage(uMsg As Long, wParam As Long, _
    lParam As Long, retValue As Long, Cancel As Boolean)
    If uMsg = WM_CONTEXTMENU Then
        ' Show a custom popup menu.
        PopupMenu mnuPopup
        ' Cancel the default processing
        ' (i.e. don't display the default context menu).
        Cancel = True
    End If
End Sub

Francesco Balena
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date