HOWTO: Suppress Default Pop-up Menu When Using Custom Menu
ID: Q191670
|
The information in this article applies to:
-
Microsoft Visual Basic Learning, Professional, and Enterprise Editions for Windows, versions 5.0, 6.0
SUMMARY
Some Visual Basic controls, such as the TextBox, have a default pop-up menu
that will automatically be display when you alternate-mouse click on the
control. This article demonstrates one way to disable this default pop-up
menu in order that either no menu or only a custom pop-up menu is
displayed.
MORE INFORMATION
When you alternate-mouse click on the TextBox control, its default pop-up
menu will be displayed. Visual Basic does not have a property or any other
built-in mechanism that directly disables this feature. However, setting
the control's Enabled property to False will prevent the menu from being
displayed although this allows the user to see that the control is
disabled.
One workaround is to use the Windows LockWindowUpdate API in conjunction
with the Enabled property. The LockWindowUpdate function disables or re-
enables drawing in a specified window. After the operation is complete, the
control is re-enabled and the LockWindowUpdate API is called a second time
to resume drawing of the control.
Steps to Create Sample Project
- Start a new Standard EXE project in Visual Basic. Form1 is created by
default.
- Add a TextBox control to Form1.
- Choose Menu Editor from the Tools menu and create a menu named mnuPopUp
on Form1. Deselect the Visible CheckBox and add items such as the
following:
File
New
Open
- Add the following code to the code window of Form1:
Private Declare Function LockWindowUpdate Lib "user32" _
(ByVal hwndLock As Long) As Long
Private Sub mnuOne_Click()
Text1.Text = "Menu One was clicked"
End Sub
Private Sub mnuTwo_Click()
Text1.Text = "Menu two was clicked"
End Sub
Private Sub Text1_MouseDown(Button As Integer, Shift As Integer, _
X As Single, Y As Single)
If Button = vbRightButton Then
' Avoid the 'disabled' gray text by locking updates
LockWindowUpdate Text1.hWnd
' A disabled TextBox will not display a context menu
Text1.Enabled = False
' Give the previous line time to complete
DoEvents
' Display our own context menu
PopupMenu mnuPopup
' Enable the control again
Text1.Enabled = True
' Unlock updates
LockWindowUpdate 0&
End If
End Sub
- Save and run the project.
- Alternate-mouse click on Text1. Only the custom menu is displayed. The
standard editing menu is not shown.
An alternative approach to supressing the default pop-up menu is to
subclass
the control. Through subclassing, you can monitor for the appropriate mouse
messages and handle them accordingly. See the REFERENCES section below for
more information on this topic.
REFERENCES
For additional information, please see the following article in the
Microsoft Knowledge Base:
Q170570
: HOWTO: Build a Windows Message Handler with AddressOf in VB
Q155969
: HOWTO: Distribute the WebBrowser Control
(c) Microsoft Corporation 1998. All Rights Reserved.
Contributions by Brian Combs, Microsoft Corporation.
Additional query words:
kbDSupport kbDSD kbVBp600 kbInternet kbVBp kbVBp400 kbVBp500 kbAPI kbVBp
kbSDKWin32 kbMenu
Keywords : kbGrpVB
Version :
Platform : WINDOWS
Issue type : kbhowto