Hide Table of Contents

Get Menu Command Items Example (VB.NET)

This example shows how to get add-in menu command items.

NOTE: If using the .NET Framework 4.0 primary interop assembly provided with SolidWorks Enterprise PDM, see Using .NET Framework 4.0 in Stand-alone Applications.

' Preconditions:
' 1. Start Microsoft Visual Studio 2010.
'    a. Click File > New > Project > Visual Basic > Console Application.
'    b. Type MenuVBNET in Name.
'    c. Click Browse and navigate to the folder where to create the project. 
'    d. Click OK.
'    e. Replace the code in Module1.vb with this code.
' 2. Add EPDM.Interop.epdm.dll as a reference (right-click the project
'    name in the Solution Explorer, click Add Reference, click 
'    Assemblies > Framework in the left-side panel, browse to the top folder of 
'    your SolidWorks Enterprise PDM installation, locate and click 
'    EPDM.Interop.epdm.dll, click Open, and click Add).
' 3. Add System.Windows.Forms as a reference (click System.Windows.Forms
'    in the Name column, click Add, and click Close).

' 4. Right-click EPDM.Interop.epdm in References, click Properties, and set 
'    Embed Interop Types to False to handle methods that pass arrays of 
'    structures.
' 5. Replace ACME_LAB with the name of a valid vault view.

' 6. Click Debug > Start Debugging or press F5.
' Postconditions: 

' 1. Displays the command window.
' 2. Displays a message box for each add-in menu command item.
' 3. Click OK to close each message box.

' 4. Displays the menu in the upper-left corner of the desktop.
' 5. Click First Command in the menu.
' 6. Click OK to close the message box.
' 7. Click the command window and press any key.
' 8. Closes the command window and exits the application.


Imports EPDM.Interop.epdm
Imports System.Windows.Forms
Module Module1
    Dim vault As EdmVault5
    Dim frmParent As Form
    Sub Main()
            'Create a vault interface
            vault = New EdmVault5()
            'Log into vault
            vault.LoginAuto("ACME_LAB", 0)
            'Show menu
        Catch ex As System.Runtime.InteropServices.COMException
            MsgBox("HRESULT = 0x" + ex.ErrorCode.ToString("X") + " " + ex.Message)
        Catch ex As Exception
        End Try
        Console.WriteLine("Press any key to exit.")
    End Sub
    Private Sub ShowMenu(ByVal vault As IEdmVault12)
        'Create a context-sensitive menu 
        'using Windows InsertMenu function
        Dim mnu As System.Windows.Forms.ContextMenu
        mnu = New System.Windows.Forms.ContextMenu
        InsertMenu(mnu.Handle, 0, 0, 100, "First Command")
        InsertMenu(mnu.Handle, 0, 0, 101, "Second Command")
        InsertMenu(mnu.Handle, 0, 0, 102, "Third Command")
        'Create a selection list with all files in the root folder
        Dim selList As IEdmSelectionList6
        selList = New EdmSelectionList5
        Dim folder As IEdmFolder6
        folder = vault.RootFolder
        Dim pos As IEdmPos5
        pos = folder.GetFirstFilePosition()
        While Not pos.IsNull
            Dim file As IEdmFile8
            file = folder.GetNextFile(pos)
            Dim obj As EdmSelectionObject
            obj.mbsPath = file.GetLocalPath(folder.ID)
            obj.meType = file.ObjectType
            obj.mlID = file.ID
            obj.mlProjectID = folder.ID
        End While
        'Add menu items for registered add-ins 
        Dim count As Integer
        count = 0
        Dim startID As Integer
        startID = 200
        Dim menuCallback As IEdmMenu6
        Dim ppoRetItems() As EdmCmdInfo = Nothing
        menuCallback = vault.CreatePluginMenu2(mnu.Handle.ToInt32(), 3, startID, selList, CreateMenuFlags.Cmf_ContextMenu, count)
        menuCallback.GetItems(EdmMenuFlags.EdmMenu_Nothing, ppoRetItems)
        Dim str As String
        For Each item As EdmCmdInfo In ppoRetItems
            str = "Add-in menu command item: " & vbLf
            str = str & "Command ID: " & item.mlCmdID & vbLf
            str = str & "Command string: " & item.mbsCmdStr & vbLf
            str = str & "Tooltip: " & item.mbsTooltip & vbLf
            str = str & "Status bar help: " & item.mbsStatusBarHelp & vbLf
            str = str & "EdmMenuFlags: " & item.mlEdmMenuFlags
        'Display the menu using Windows TrackPopupMenu function
        Dim TPM_RETURNCMD As Integer
        TPM_RETURNCMD = 256
        Dim cmdID As Integer
        frmParent = New Form
        cmdID = TrackPopupMenu(mnu.Handle, TPM_RETURNCMD, frmParent.Left, frmParent.Top, 0, frmParent.Handle, 0)
        'Run the selected command
        Select Case cmdID
            Case 100
                MsgBox("First command")
            Case 101
                MsgBox("Second command")
            Case 102
                MsgBox("Third command")
            Case Else
        End Select
    End Sub
    Private Declare Function InsertMenu Lib "user32" Alias "InsertMenuA" _
    (ByVal hMenu As System.IntPtr, _
    ByVal uPosition As Integer, _
    ByVal uFlags As Integer, _
    ByVal uIDNewItem As System.IntPtr, _
    ByVal lpNewItem As StringAs Boolean
    Private Declare Function TrackPopupMenu Lib "user32" Alias "TrackPopupMenu" _
    (ByVal hMenu As IntPtr, _
    ByVal uFlags As Integer, _
    ByVal x As Integer, _
    ByVal y As Integer, _
    ByVal nReserved As Integer, _
    ByVal hWnd As IntPtr, _
    ByVal prcRect As IntPtrAs Integer
End Module

Back to top

Provide feedback on this topic

SOLIDWORKS welcomes your feedback concerning the presentation, accuracy, and thoroughness of the documentation. Use the form below to send your comments and suggestions about this topic directly to our documentation team. The documentation team cannot answer technical support questions. Click here for information about technical support.

* Required

Subject:   Feedback on Help Topics
Page:   Get Menu Command Items Example (VB.NET)
*   I acknowledge I have read and I hereby accept the privacy policy under which my Personal Data will be used by Dassault Systèmes

Print Topic

Select the scope of content to print:


We have detected you are using a browser version older than Internet Explorer 7. For optimized display, we suggest upgrading your browser to Internet Explorer 7 or newer.

 Never show this message again

Web Help Content Version: API Help (English only) 2014 SP05

To disable Web help from within SOLIDWORKS and use local help instead, click Help > Use SOLIDWORKS Web Help.

To report problems encountered with the Web help interface and search, contact your local support representative. To provide feedback on individual help topics, use the “Feedback on this topic” link on the individual topic page.