We help IT Professionals succeed at work.

right click menu in vb6

syamchandran
syamchandran asked
on
Medium Priority
31,146 Views
Last Modified: 2013-12-26
I have a Form at Visual Basic 6.
I want to provide one pop-up menu by right mouse click on the Form.
The options on the menu are 1. Open 2. Close.

If user select Open then show frmUser.frm form as non-modal and if user select close then close that form on which he press right click.


Please, help me how to do this. If possible right me the code.

Comment
Watch Question

CERTIFIED EXPERT
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
LOL posted at the exact same time :(
CERTIFIED EXPERT

Commented:
To open the from or close it, use the click event of the menu items

Private Sub mnuClose_Click()
  Unload Me
End Sub

Private Sub mnuOpen_Click()
  Load Form2
  Form2.Show
End Sub
Make a form that looks like this:

Private Declare Function CreatePopupMenu Lib "user32" () As Long
Private Declare Function DestroyMenu Lib "user32" (ByVal hMenu As Long) As Long
Private Declare Function AppendMenu Lib "user32" Alias "AppendMenuA" (ByVal hMenu As Long, ByVal wFlags As Long, ByVal wIDNewItem As Long, ByVal lpNewItem As Any) As Long
Private Declare Function TrackPopupMenu Lib "user32" (ByVal hMenu As Long, ByVal wFlags As Long, ByVal x As Long, ByVal y As Long, ByVal nReserved As Long, ByVal hwnd As Long, lprc As Any) As Long

Const TPM_NONOTIFY = &H80
Const TPM_RETURNCMD = &H100

Private Type PointAPI
    x As Long
    y As Long
End Type

Private Declare Function GetCursorPos Lib "user32" (lpPoint As PointAPI) As Long

Dim Menu As Long

Private Sub Form_Load()
    Menu = CreatePopupMenu()
    Call AppendMenu(Menu, MF_STRING, 1, "Open")
    Call AppendMenu(Menu, MF_STRING, 2, "Close")
End Sub

Private Sub Form_MouseDown(Button As Integer, Shift As Integer, x As Single, y As Single)
    Dim ID As Long
    Dim Cursor As PointAPI
    Call GetCursorPos(Cursor)
    ID = TrackPopupMenu(Menu, TPM_NONOTIFY + TPM_RETURNCMD, Cursor.x, Cursor.y, 0, Me.hwnd, 0)
    Select Case ID
    Case 1:
        'MsgBox ("Open")
        Show frmUser
    Case 2:
        'MsgBox ("Close")
        Unload Me
    End Select
End Sub

Private Sub Form_Unload(Cancel As Integer)
    Call DestroyMenu(Menu)
End Sub

Commented:
Hello syamchandran,

To display the menu on mouse right  button is as said in starting message :

* Create the popup menu thru menu wizard and made visible false.

* now my question is  what type of applicatio u r using SDK or MDI
  - if its SDI its is quit simple, u have to create the menu on the same form and display them on click of mouse button down if "Button" value of mouse button down is 2 (mean right mouse button down) thru function me.PopupMenu passing parent menu name.

  - if its MDI its not difficult but u have to do some code..... create the menu on mdi form and on mouse down of active form in MDI u have to do the things like in SDI but click button as its call menu click event of MDI Form. In Open menu click u have to call <MDIFormName>.ActiveForm.Open and define public open function in every form where u want to diaplay menu. And perform the required function in Open function.

visible off the menu is not compulsory.....its depend on ur requirement.

Amit Jain
<email removed by Bingie EE Page Editor>
Pi7

Commented:
Or after creating the menu (called mnuName with submenus Open and Close) at design time, set its visible property to false to hide the menu.you can then use Me.PopUpMenu mnuName to display the popup menu
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.