Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


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

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:


' REQUIRES THE MSGHOOK.DLL COMPONENT

' you can omit the following constant definition, 
' because it is contained in the MsgHook type library
Const WM_CONTEXTMENU = &H7B

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.

 

 

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