palin2000
asked on
Add menu sub-items dynamically
Hi
I'm looking for coding on how to emulate IE's "Add to Favorites" for a University project.
I've read a few examples of how to add either a parent menu item or a menu sub-item, for each example you can only add one per session, if you where to add say a sub-item it would work first time but the second attempt would throw up the error "mnu..... already loaded"
If I had the menu started with "Favorites" (parent) and "Add to Favorites"(sub-item), I would like to be able to add a new sub-item using the OnClick event of the menu item "Add to Favorites", repeating this event each time I wished to bookmark a web-site.
Additionally when building a menu dynamically do the extra items added at run-time require to be stored and re-loaded or are they held within the menu control?
I'm fairly new to VB so if full coding could be supplied within the answer that would be of great assistance.
Thanks in advance.
Palin
I'm looking for coding on how to emulate IE's "Add to Favorites" for a University project.
I've read a few examples of how to add either a parent menu item or a menu sub-item, for each example you can only add one per session, if you where to add say a sub-item it would work first time but the second attempt would throw up the error "mnu..... already loaded"
If I had the menu started with "Favorites" (parent) and "Add to Favorites"(sub-item), I would like to be able to add a new sub-item using the OnClick event of the menu item "Add to Favorites", repeating this event each time I wished to bookmark a web-site.
Additionally when building a menu dynamically do the extra items added at run-time require to be stored and re-loaded or are they held within the menu control?
I'm fairly new to VB so if full coding could be supplied within the answer that would be of great assistance.
Thanks in advance.
Palin
ASKER
Hi maheshexp
Thanks for your input, I did state that I was fairly new to VB and the code example you directed me to looks a bit advanced.
I copied the module and form code and understand to some degree what it is doing, but the problem I have is making this a working example, i.e. exactly how to call these functions for the OnClick event, what to name the module etc.
Please understand what seems easy and logical to a VB expert is not quite as easy for a near beginner like myself.
Could you direct me to the point where I can click on the "Add to Favorites" button and on goes the new menu item.
Thanks in advance.
Thanks for your input, I did state that I was fairly new to VB and the code example you directed me to looks a bit advanced.
I copied the module and form code and understand to some degree what it is doing, but the problem I have is making this a working example, i.e. exactly how to call these functions for the OnClick event, what to name the module etc.
Please understand what seems easy and logical to a VB expert is not quite as easy for a near beginner like myself.
Could you direct me to the point where I can click on the "Add to Favorites" button and on goes the new menu item.
Thanks in advance.
ASKER
Additional comment.
Looking at the code supplied it seems to be more appropriate to the system menu, is this the correct way to achieve what I'm trying to do?
Looking at the code supplied it seems to be more appropriate to the system menu, is this the correct way to achieve what I'm trying to do?
ASKER
Additional comment.
Looking at the code supplied it seems to be more appropriate to the system menu, is this the correct way to achieve what I'm trying to do?
Looking at the code supplied it seems to be more appropriate to the system menu, is this the correct way to achieve what I'm trying to do?
Const MF_CHECKED = &H8&
Const MF_APPEND = &H100&
Const TPM_LEFTALIGN = &H0&
Const MF_DISABLED = &H2&
Const MF_GRAYED = &H1&
Const MF_SEPARATOR = &H800&
Const MF_STRING = &H0&
Private Type POINTAPI
x As Long
y As Long
End Type
Private Declare Function CreatePopupMenu Lib "user32" () 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, ByVal lprc As Any) As Long
Private Declare Function GetSystemMenu Lib "user32" (ByVal hwnd As Long, ByVal bRevert 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 DestroyMenu Lib "user32" (ByVal hMenu As Long) As Long
Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
Dim hMenu As Long
Private Sub Form_Load()
hMenu = CreatePopupMenu()
'Append a few menu items
AppendMenu hMenu, MF_STRING, ByVal 0&, "Hello !"
AppendMenu hMenu, MF_GRAYED Or MF_DISABLED, ByVal 0&, "Testing ..."
AppendMenu hMenu, MF_SEPARATOR, ByVal 0&, ByVal 0&
AppendMenu hMenu, MF_CHECKED, ByVal 0&, "TrackPopupMenu"
End Sub
Private Sub Form_MouseUp(Button As Integer, Shift As Integer, x As Single, y As Single)
Dim Pt As POINTAPI
'Get the position of the mouse cursor
GetCursorPos Pt
If Button = 1 Then
'Show our popupmenu
TrackPopupMenu hMenu, TPM_LEFTALIGN, Pt.x, Pt.y, 0, Me.hwnd, ByVal 0&
Else
'Show our form's default popup menu
TrackPopupMenu GetSystemMenu(Me.hwnd, False), TPM_LEFTALIGN, Pt.x, Pt.y, 0, Me.hwnd, ByVal 0&
End If
End Sub
Private Sub Form_Unload(Cancel As Integer)
'Destroy our menu
DestroyMenu hMenu
End Sub
this code i used for system menu. to access ur menu , u use the handle of ur menu. using getmenu function
Const MF_APPEND = &H100&
Const TPM_LEFTALIGN = &H0&
Const MF_DISABLED = &H2&
Const MF_GRAYED = &H1&
Const MF_SEPARATOR = &H800&
Const MF_STRING = &H0&
Private Type POINTAPI
x As Long
y As Long
End Type
Private Declare Function CreatePopupMenu Lib "user32" () 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, ByVal lprc As Any) As Long
Private Declare Function GetSystemMenu Lib "user32" (ByVal hwnd As Long, ByVal bRevert 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 DestroyMenu Lib "user32" (ByVal hMenu As Long) As Long
Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
Dim hMenu As Long
Private Sub Form_Load()
hMenu = CreatePopupMenu()
'Append a few menu items
AppendMenu hMenu, MF_STRING, ByVal 0&, "Hello !"
AppendMenu hMenu, MF_GRAYED Or MF_DISABLED, ByVal 0&, "Testing ..."
AppendMenu hMenu, MF_SEPARATOR, ByVal 0&, ByVal 0&
AppendMenu hMenu, MF_CHECKED, ByVal 0&, "TrackPopupMenu"
End Sub
Private Sub Form_MouseUp(Button As Integer, Shift As Integer, x As Single, y As Single)
Dim Pt As POINTAPI
'Get the position of the mouse cursor
GetCursorPos Pt
If Button = 1 Then
'Show our popupmenu
TrackPopupMenu hMenu, TPM_LEFTALIGN, Pt.x, Pt.y, 0, Me.hwnd, ByVal 0&
Else
'Show our form's default popup menu
TrackPopupMenu GetSystemMenu(Me.hwnd, False), TPM_LEFTALIGN, Pt.x, Pt.y, 0, Me.hwnd, ByVal 0&
End If
End Sub
Private Sub Form_Unload(Cancel As Integer)
'Destroy our menu
DestroyMenu hMenu
End Sub
this code i used for system menu. to access ur menu , u use the handle of ur menu. using getmenu function
ASKER
Hi maheshexp
Thanks once again for your input, but I'm afraid that it is of limited value, it's simply a straight copy from a web page that I have studied.
I have played about with this and other coding most of today and managed only to add an additional(header)menu item by merging the various bits of code together.
I'm not looking to add an item to a system menu, or create and track a popup menu as is decribed in this code, I have a specific need to add a sub-item to an existing menu.
Favorites
...Add to Favorites (OnClick)
...Visual Basic (for example)
If you can help with this specific requirement I would be very grateful.
Rgds Palin
Thanks once again for your input, but I'm afraid that it is of limited value, it's simply a straight copy from a web page that I have studied.
I have played about with this and other coding most of today and managed only to add an additional(header)menu item by merging the various bits of code together.
I'm not looking to add an item to a system menu, or create and track a popup menu as is decribed in this code, I have a specific need to add a sub-item to an existing menu.
Favorites
...Add to Favorites (OnClick)
...Visual Basic (for example)
If you can help with this specific requirement I would be very grateful.
Rgds Palin
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hi
Thanks again for your input, we are getting a lot closer.
hMenu = GetMenu(Me.hWnd)
hMenu = GetSubMenu(hMenu, 0)
Both functions caused compiler errors, I think due to them not being declared, I did try to declare them but could not identify the correct parameters required.
If you could take another stab at this it would be great, I must admit I did not think this question would be quite as awkard as it has turned out to be, still more then myself will benefit if we can resolve it to the exact requirements needed.
Rgds Palin
Thanks again for your input, we are getting a lot closer.
hMenu = GetMenu(Me.hWnd)
hMenu = GetSubMenu(hMenu, 0)
Both functions caused compiler errors, I think due to them not being declared, I did try to declare them but could not identify the correct parameters required.
If you could take another stab at this it would be great, I must admit I did not think this question would be quite as awkard as it has turned out to be, still more then myself will benefit if we can resolve it to the exact requirements needed.
Rgds Palin
expanding on from maheshexp's example (I've run this on my machine with only this code and the menu item gets created.:
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 GetSubMenu Lib "user32" (ByVal hMenu As Long, ByVal nPos As Long) As Long
Private Sub Add_Fav_Click()
Dim hMenu As Long
' get the menu in the form
hMenu = GetMenu(Me.hwnd)
'get the first menu in the form i.e Fav Menu
hMenu = GetSubMenu(hMenu, 0)
'append the new menu to the menu Fav
AppendMenu hMenu, MF_STRING, ByVal 0&, "Visual Basic"
End Sub
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 GetSubMenu Lib "user32" (ByVal hMenu As Long, ByVal nPos As Long) As Long
Private Sub Add_Fav_Click()
Dim hMenu As Long
' get the menu in the form
hMenu = GetMenu(Me.hwnd)
'get the first menu in the form i.e Fav Menu
hMenu = GetSubMenu(hMenu, 0)
'append the new menu to the menu Fav
AppendMenu hMenu, MF_STRING, ByVal 0&, "Visual Basic"
End Sub
ASKER
Hi
Great work from both you guys, twalgrave I'll post a 50 point question to you for you help.
maheshexp please accept the points for this question, thanks for all your help.
Rgds Palin
Great work from both you guys, twalgrave I'll post a 50 point question to you for you help.
maheshexp please accept the points for this question, thanks for all your help.
Rgds Palin
thanks a lot palin :-)
'to append a new menu item to an existing menu
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
'to insert a new menu
Public Declare Function InsertMenuItem Lib "user32.dll" Alias "InsertMenuItemA" (ByVal hMenu As Long, ByVal uItem As Long, ByVal fByPosition As Long, lpmii As MENUITEMINFO) As Long
MENUITEMINFO is a type
for more details visit
http://216.26.168.92/vbapi/ref/funcc.html