Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to create a popup menu with submenus using API?

Status
Not open for further replies.

micha12

Programmer
Dec 11, 2002
10
RU
Hello everybody,

(1)

I want to create a popup menu with submenus. I can create an ordinary popup menu using API functions, but am unable to create a popup menu with submenus. Neither can I create a submenu for a certain menu item of the form, using Load mnuItem(0). How can I do that?

(2)

When the mouse cursor moves over a certain menu item, it becomes highlighted. Can any event be generated saying which menu item is being heighlighted?

Thanks in advance.
 
(1)
Sure you can create popup menus containing sub menus using API functions. See the VB example below.
___

Option Explicit
Private Declare Function CreatePopupMenu Lib "user32" () 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 String) As Long
Private Declare Function DestroyMenu Lib "user32" (ByVal hMenu As Long) As Long
Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) 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
Private Type POINTAPI
x As Long
y As Long
End Type
Const MF_POPUP = &H10&
Dim hMenu As Long, hSubMenu As Long
Private Sub Form_Load()
Caption = "7 Layers of the OSI Model"

hSubMenu = CreatePopupMenu
AppendMenu hSubMenu, 0, 121, "Media Access Control"
AppendMenu hSubMenu, 0, 122, "Logical Link Control"

hMenu = CreatePopupMenu
AppendMenu hMenu, 0, 107, "Application"
AppendMenu hMenu, 0, 106, "Presentation"
AppendMenu hMenu, 0, 105, "Session"
AppendMenu hMenu, 0, 104, "Transport"
AppendMenu hMenu, 0, 103, "Network"
AppendMenu hMenu, MF_POPUP, hSubMenu, "Data Link" 'insert the sub menu
AppendMenu hMenu, 0, 101, "Physical"
End Sub

Private Sub Form_MouseUp(Button As Integer, Shift As Integer, x As Single, y As Single)
If Button = vbRightButton Then
Dim P As POINTAPI
GetCursorPos P
TrackPopupMenu hMenu, 0, P.x, P.y, 0, hwnd, 0
End If
End Sub

Private Sub Form_Unload(Cancel As Integer)
DestroyMenu hSubMenu
DestroyMenu hMenu
End Sub
___

(2)
When a menu item is highlighted, Windows sends a WM_MENUSELECT message to the window that owns the menu. You can subclass the window and filter this message to be notified of such events.

See thread222-506228 that covers this issue in details.
 
You CreateMenu the menus, then to append the submenu to the main menu, call AppendMenuItem using the MF_POPUP flag, and passing the submenu handle as uIDNewItem, which is the 3rd parameter.

BOOL AppendMenu(
HMENU hMenu, // handle to menu
UINT uFlags, // menu-item options
UINT_PTR uIDNewItem, // identifier, menu, or submenu
LPCTSTR lpNewItem // menu-item content
);

Hope this helps.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top