create menu/sub menu at runtime

cris02
02-03-2008, 11:26 PM
hello, how to create a submenu/menu at runtime and the caption of each menu coming from database and captured it when the menu has been clicked?. please help, thanks.
i have example here i got from other site, but i dont know how to capture the item when it clicked.

Option Explicit

Private Declare Function DestroyMenu Lib "user32" (ByVal hMenu As Long) As Long
Private Declare Function SetMenu Lib "user32" (ByVal hwnd As Long, ByVal hMenu As Long) As Long
Private Declare Function GetMenu Lib "user32" (ByVal hwnd As Long) 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 DeleteMenu Lib "user32" (ByVal hMenu As Long, ByVal nPosition As Long, ByVal wFlags As Long) As Long
Private Declare Function InsertMenu Lib "user32" Alias "InsertMenuA" (ByVal hMenu As Long, ByVal nPosition As Long, ByVal wFlags As Long, ByVal wIDNewItem As Long, ByVal lpNewItem As Any) As Long

Private Declare Function CreatePopupMenu Lib "user32" () 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()

hSubMenu = CreatePopupMenu
AppendMenu hSubMenu, 0, 121, "Sub Menu Test1"
AppendMenu hSubMenu, 0, 122, "Sub Menu Test2"

hMenu = CreatePopupMenu
AppendMenu hMenu, 0, 107, "Menu1"
AppendMenu hMenu, 0, 106, "Menu2"
AppendMenu hMenu, 0, 105, "Menu3"

AppendMenu hMenu, MF_POPUP, hSubMenu, "Menu4 'insert the sub menu
AppendMenu hMenu, 0, 101, "Menu5"
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

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum