A class for API Created Dynamic Menus


For some controls In my current project, I wanted To be able To display a simple context menu using the Right mouse button.

The solution In plain VB uses an existing menu, which must be extended (at runtime Or at design Time). I found this unacceptable.

Searching the web offered the solution of using the API functions CreatePopupMenu(), TrackPopupMenu() And AppendMenu().

I extended the sample To work With a undetermined number of arguments, thus giving Me the flexibility I needed.

The presented Function Popup() assumes the user wants a context menu at the current mouse pointer location. The argument Array consists of menu items Or separators (presented by a Single dash). The user can Select a menu Item by using either the Left Or Right mouse button. The index of the chosen menu Item Is returned, Or zero If cancelled.

A simple usage of the class module could be : (on a list box)


private Sub List1_MouseUp(Button as Integer, Shift as Integer, _
      x as Single, y as Single)
    Dim oMenu as cPopupMenu
    Dim lMenuChosen as Long
'
    If Button = vbRightButton then
        set oMenu = new cPopupMenu
'
' Pass in the desired menu, use '-' for a separator
'
        lMenuChosen = oMenu.Popup("Menu 1", "Menu 2", "Menu 3", _
                "-", "Menu 4")
'
        Debug.print lMenuChosen
    End If
'
End Sub
'
'

Download Zipped Project File and API Menu Class (3k)



Comments

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: October 29, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Are you interested in building a cognitive application using the power of IBM Watson? Need a platform that provides speed and ease for rapidly deploying this application? Join Chris Madison, Watson Solution Architect, as he walks through the process of building a Watson powered application on IBM Bluemix. Chris will talk about the new Watson Services just released on IBM bluemix, but more importantly he will do a step by step cognitive …

  • Mobile devices, social business apps, and business analytics are converging with the Cloud to create the most substantial changes in technology since the Internet revolution. Businesses have to change the way they think and operate, and with rising budgets for technology, they need someone to provide the services that will keep them competitive in this environment. Learn more about the important technology trends you need to stay on top of to ensure your business doesn't get left behind.

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds