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

  • On-demand Event Event Date: September 10, 2014 Modern mobile applications connect systems-of-engagement (mobile apps) with systems-of-record (traditional IT) to deliver new and innovative business value. But the lifecycle for development of mobile apps is also new and different. Emerging trends in mobile development call for faster delivery of incremental features, coupled with feedback from the users of the app "in the wild." This loop of continuous delivery and continuous feedback is how the best mobile …

  • QA teams don't have time to test everything yet they can't afford to ship buggy code. Learn how Coverity can help organizations shrink their testing cycles and reduce regression risk by focusing their manual and automated testing based on the impact of change.

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds