Search Tools Links Login

Simple API Popup Menu Class (for context menus on the fly)

Posted: 2002-06-01
By: ArchiveBot
Viewed: 62

Filed Under:

Visual Basic 6

Title Uploaded Size
Simple 9/3/2020 3:45:00 PM 4,517

OK... not looking to blow away the world with this one, but felt lead to make the post after reading this article.

At work, I have an app that is an mdi based program... and while the sub forms don't need a menu bar of their own, they do need popup context menus. Since adding even an invisible menu to a mdichild form will cause it to over write the parent mdi's menu bar, I needed a way to create menus for the child forms on the fly.
This simple class demonstrates using the API calls to create a popup menu, display it, and return the item that was clicked (if any). It is simple and self-contained, but this will at least get you started on your way to wrapping the API menu functions in VB classes. I can already see I will be expanding this class, since getting this far was actually quite easy. The ultimate goal would be to "InsertMenuItem"s not just in a popup, but in the menu bar of the parent mdi form as well. I'll repost the class when I've taken it that far. (just updated to include .ItemEnabled property for the popup menu)

Original Author: TheFrogPrince

API Declarations

Private Declare Function GetCursorPos _
Lib "user32" ( _
lpPoint As POINTAPI) _
As Long
Private Declare Function InsertMenuItem _
Lib "user32.dll" _
Alias "InsertMenuItemA" ( _
ByVal hMenu As Long, _
ByVal uItem As Long, _
ByVal fByPosition As Long, _
As Long
Private Declare Function CreatePopupMenu _
Lib "user32" () _
As Long

Private Declare Function TrackPopupMenu _
Lib "user32" ( _
ByVal hMenu As Long, _
ByVal wFlags As enumTrackPopupMenu, _
ByVal x As Long, _
ByVal y As Long, _
ByVal nReserved As Long, _
ByVal hwnd As Long, _
lprc As RECT) _
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 DestroyMenu Lib "user32" ( _
ByVal hMenu As Long) _
As Long

Comments on this post

No comments have been added for this post.

You must be logged in to make a comment.