Wednesday, December 17, 2008

[vb6] Disable default popups

Some controls, such as the intrinsic textbox control, display their own popupmenus, thereby effectively excluding you from displaying your own popup (it looks awkward if the default menu displays, followed by your custom menu). You can disable the default menu by disabling the control, as in the code below. The only disadvantage is that the control displays its disabled state while the custom menu is on - that may be confusing to the user. By using the LockWindowUpdate API, the control never gets a chance to display its disabled state. If the control name is "Text1" and the name of the custom menu is "MyPopUpMenu":
    Private Declare Function LockWindowUpdate Lib "user32" _
(ByVal hwndLock As Long) As Long

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 own context menu
PopupMenu MyPopUpMenu
' Enable the control again
Text1.Enabled = True
' Unlock updates
LockWindowUpdate 0&
End If
End Sub

No comments:

Post a Comment