im afraid its not a simple example, but i think its a nice thing to have fun with. its a sort of menu editor i made, and it uses TrackPopupMenuEx and all the stuff you said, but a lot of other stuff too.
im just posting this since something is better than nothing. usually.
email me if you have any more questions about this, i spent quite a lot of time dealing with menus in this way so i might be able to help. having said that im not amazingly good at them or anything
I suggest that you go the easy way. For example, you can use AppendMenu instead of InsertMenu if you are only inserting the menu at the end which is easy. I also suggest TrackPopupMenu instead of TrackPopupMenuEx unless you want some advanced features. Here's an example that I came up which uses some of the menu APIs to create a dynamic menu and when you right click on your form, it pops up the menu:
VB Code:
Private Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
Private Type TPMPARAMS
cbSize As Long
rcExclude As RECT
End Type
Private Type POINTAPI
x As Long
y As Long
End Type
Private Declare Function CreateMenu Lib "user32" () As Long
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, lprc As RECT) As Long
Private Declare Function TrackPopupMenuEx Lib "user32" (ByVal hMenu As Long, ByVal un As Long, ByVal n1 As Long, ByVal n2 As Long, ByVal hwnd As Long, lpTPMParams As TPMPARAMS) 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 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 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 DrawMenuBar Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function ClientToScreen Lib "user32" (ByVal hwnd As Long, lpPoint As POINTAPI) As Long
Private Const MF_BYPOSITION = &H400&
Private Const MF_STRING = &H0&
Private Const MF_POPUP = &H10&
Private Const MF_DISABLED = &H2&
Private Const MF_CHECKED = &H8&
Private Const MF_GRAYED = &H1&
Private Const TPM_LEFTALIGN = &H0&
Private Const TPM_CENTERALIGN = &H4&
Private Const TPM_LEFTBUTTON = &H0&
Private Const TPM_RIGHTALIGN = &H8&
Private Const TPM_RIGHTBUTTON = &H2&
Dim hMenu, hSubMenu As Long
Dim pt As POINTAPI
Private Sub Form_Load()
hMenu = CreateMenu()
hSubMenu = CreatePopupMenu()
AppendMenu hSubMenu, MF_STRING, 0, "Open"
AppendMenu hSubMenu, MF_STRING, 0, "Save"
AppendMenu hSubMenu, MF_STRING Or MF_CHECKED, 0, "Checked"
AppendMenu hSubMenu, MF_STRING Or MF_GRAYED, 0, "Disabled"
AppendMenu hSubMenu, MF_STRING, 0, "Print"
AppendMenu hSubMenu, MF_STRING, 0, "Exit"
AppendMenu hMenu, MF_STRING Or MF_POPUP, hSubMenu, "File"
End Sub
Private Sub Form_MouseUp(Button As Integer, Shift As Integer, myX As Single, myY As Single)
Dim rc As RECT
If Button = vbRightButton Then
pt.x = myX
pt.y = myY
ClientToScreen Me.hwnd, pt
TrackPopupMenu hSubMenu, TPM_LEFTALIGN Or TPM_BOTTOMALIGN, pt.x, pt.y, 0, Me.hwnd, rc
Thanks for the example of a popup menu. Its similar to one I have been playing around with but I included the GetCursorPos function.
I have the same problem with your exmaple in that I am trying to implement popupmenus in a lotus approach database. I can successfully initiate the popup menu but am unable to identify the users selection. Any ideas ???
but unless im much mistaken the user selection is in the return value of TrackPopupMenu(Ex) so if you can make it pop up, how come you cant get the input?
Ofcourse it'll always return 0 or at least it won't return which menu is clicked. You'll have to *subclass* your window and then handle the WM_COMMAND to see if your menu item is clicked. Notice the example I provided:
[Highlight=VB]
VB Code:
AppendMenu hSubMenu, MF_STRING, 0, "Open"
AppendMenu hSubMenu, MF_STRING, 0, "Save"
AppendMenu hSubMenu, MF_STRING Or MF_CHECKED, 0, "Checked"
AppendMenu hSubMenu, MF_STRING Or MF_GRAYED, 0, "Disabled"
AppendMenu hSubMenu, MF_STRING, 0, "Print"
AppendMenu hSubMenu, MF_STRING, 0, "Exit"
I used "0" for the ID of every menu which isn't right. You have to do something like this to distinguish all the menu items from each other:
VB Code:
public const MENU_OPEN 400
public const MENU_SAVE 401
public const MENU_CHECKED 402
public const MENU_DSIABLED 403
public const MENU_PRINT 404
public const MENU_EXIT 405
AppendMenu hSubMenu, MF_STRING, MENU_OPEN, "Open"
AppendMenu hSubMenu, MF_STRING, MENU_SAVE, "Save"
AppendMenu hSubMenu, MF_STRING Or MF_CHECKED, MENU_CHECKED, "Checked"
AppendMenu hSubMenu, MF_STRING Or MF_GRAYED, MENU_DISABLED, "Disabled"
Now you have declared the IDs for your menu items, you only have to subclass your window...search this forum for an example of subclassing. Then you handle WM_COMMAND but and check the Loword of wParam against the menu item ID. If both are equal, your menu item has been clicked.
I am really sorry to keep bugging you with this. I am knew to the world of programming, been developing lotus applications with script for a couple of years, now trying to expand a little by calling API's
Can you show me how to abtain the handle then how I retrieve the message. I have searched on here but dont understand the examples.
In spite of my tentative attitude, I am pretty certain (ie 100%) that TrackPopupMenuEx DOES return the ID of the Menu Item which has been clicked. No need to subclass.
What you HAVE to do is make sure you give each menu item their own unique ID AND include TPM_NONOTIFY and TPM_RETURNCMD in the flags of your call.
If this isnt the case ill eat my hat, which is a rathy musty looking bowler.
Sorry mate but I refuse to eat my hat. I need it. I mean, otherwise, my hair wil get wet when it rains, and i wont have anything to do when i walk into a building, and my whole world would come crashing down.
So if you want to send me your project I could have a look at getting it to work, because SubClassing causes you a LOT of extra sh*t with a capital i. Maybe not that much. Take the capital i away. That's about right.
Hahaha, just been reading your profile mate Mr Bondage boy !! LOL
I dont doubt your theory on this one mate, if think the big problem with it is that I am trying (so got damn desperately !) to utilise popup menu's in Lotus Approach via Lotus Script, being that its the same basis as VB Script.
I have played around somemore today and now when I move the mouse over the options in the menu, the caption in the main window title bar changes to various Approach options, although still only returning the same value.
I have only ever seen popup menu's once before in Approach and the developer who did this converted his code to lso (Lotus Script Object) so I cant see how he does it (Grrrrr !!!!). However they also had same issue with the titlebar caption showing various Approach options.
What do you reckon, I give up ??
Cheers Mate
C
Last edited by Carl Carter; Aug 6th, 2002 at 04:23 PM.
Actually TrackPopupMenu() also returns the ID of the menu item that was clicked if the flag TPM_RETURNCMB is set.
Here is a complete example that also shows you which menu item was clicked:
VB Code:
Option Explicit
Const MENU_OPEN = 400
Const MENU_SAVE = 401
Const MENU_CHECKED = 402
Const MENU_DISABLED = 403
Const MENU_PRINT = 404
Const MENU_EXIT = 405
Private Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
Private Type TPMPARAMS
cbSize As Long
rcExclude As RECT
End Type
Private Type POINTAPI
x As Long
y As Long
End Type
Private Declare Function CreateMenu Lib "user32" () As Long
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, lprc As RECT) As Long
Private Declare Function TrackPopupMenuEx Lib "user32" (ByVal hMenu As Long, ByVal un As Long, ByVal n1 As Long, ByVal n2 As Long, ByVal hwnd As Long, lpTPMParams As TPMPARAMS) 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 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 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 DrawMenuBar Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function ClientToScreen Lib "user32" (ByVal hwnd As Long, lpPoint As POINTAPI) As Long
Private Const MF_BYPOSITION = &H400&
Private Const MF_STRING = &H0&
Private Const MF_POPUP = &H10&
Private Const MF_DISABLED = &H2&
Private Const MF_CHECKED = &H8&
Private Const MF_GRAYED = &H1&
Private Const TPM_LEFTALIGN = &H0&
Private Const TPM_CENTERALIGN = &H4&
Private Const TPM_LEFTBUTTON = &H0&
Private Const TPM_RIGHTALIGN = &H8&
Private Const TPM_RIGHTBUTTON = &H2&
Private Const TPM_RETURNCMD = &H100&
Dim hMenu, hSubMenu As Long
Dim pt As POINTAPI
Private Sub Form_Load()
Me.ScaleMode = vbPixels
hMenu = CreateMenu()
hSubMenu = CreatePopupMenu()
AppendMenu hSubMenu, MF_STRING, MENU_OPEN, "Open"
AppendMenu hSubMenu, MF_STRING, MENU_SAVE, "Save"
AppendMenu hSubMenu, MF_STRING Or MF_CHECKED, MENU_CHECKED, "Checked"
AppendMenu hSubMenu, MF_STRING Or MF_GRAYED, MENU_DISABLED, "Disabled"