Results 1 to 16 of 16

Thread: CreatePopupMenu

  1. #1

    Thread Starter
    New Member
    Join Date
    Aug 2002
    Location
    UK
    Posts
    7

    Unhappy CreatePopupMenu

    Hello

    I am trying to create a popupmenu using the functions createpopupmenu / insertmenuitem / TrackPopupMenuEx.

    I am really stuck and wondered if someone could post a simple example (in vb) of one for me to look over.

    Many thanks

    Carl.

  2. #2
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256

    Re: CreatePopupMenu

    Originally posted by Carl Carter
    Hello

    I am trying to create a popupmenu using the functions createpopupmenu / insertmenuitem / TrackPopupMenuEx.

    I am really stuck and wondered if someone could post a simple example (in vb) of one for me to look over.

    Many thanks

    Carl.
    Is there any reason you're trying to do this with API and not via the Menu Editor?
    My evil laugh has a squeak in it.

    kristopherwilson.com

  3. #3
    Fanatic Member
    Join Date
    Apr 2000
    Location
    Whats a location?
    Posts
    516
    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

    thanks,

    me.
    Attached Files Attached Files
    Courgettes.

  4. #4
    PowerPoster abdul's Avatar
    Join Date
    Dec 2000
    Location
    Ontario,Canada
    Posts
    2,827
    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:
    1. Private Type RECT
    2.         Left As Long
    3.         Top As Long
    4.         Right As Long
    5.         Bottom As Long
    6. End Type
    7. Private Type TPMPARAMS
    8.     cbSize As Long
    9.     rcExclude As RECT
    10. End Type
    11. Private Type POINTAPI
    12.         x As Long
    13.         y As Long
    14. End Type
    15.  
    16. Private Declare Function CreateMenu Lib "user32" () As Long
    17. Private Declare Function CreatePopupMenu Lib "user32" () As Long
    18. 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
    19. 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
    20. 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
    21. 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
    22. Private Declare Function DestroyMenu Lib "user32" (ByVal hMenu As Long) As Long
    23. Private Declare Function SetMenu Lib "user32" (ByVal hwnd As Long, ByVal hMenu As Long) As Long
    24. Private Declare Function DrawMenuBar Lib "user32" (ByVal hwnd As Long) As Long
    25. Private Declare Function ClientToScreen Lib "user32" (ByVal hwnd As Long, lpPoint As POINTAPI) As Long
    26.  
    27. Private Const MF_BYPOSITION = &H400&
    28. Private Const MF_STRING = &H0&
    29. Private Const MF_POPUP = &H10&
    30. Private Const MF_DISABLED = &H2&
    31. Private Const MF_CHECKED = &H8&
    32. Private Const MF_GRAYED = &H1&
    33. Private Const TPM_LEFTALIGN = &H0&
    34. Private Const TPM_CENTERALIGN = &H4&
    35. Private Const TPM_LEFTBUTTON = &H0&
    36. Private Const TPM_RIGHTALIGN = &H8&
    37. Private Const TPM_RIGHTBUTTON = &H2&
    38.  
    39. Dim hMenu, hSubMenu As Long
    40. Dim pt As POINTAPI
    41.  
    42. Private Sub Form_Load()
    43. hMenu = CreateMenu()
    44. hSubMenu = CreatePopupMenu()
    45. AppendMenu hSubMenu, MF_STRING, 0, "Open"
    46. AppendMenu hSubMenu, MF_STRING, 0, "Save"
    47. AppendMenu hSubMenu, MF_STRING Or MF_CHECKED, 0, "Checked"
    48. AppendMenu hSubMenu, MF_STRING Or MF_GRAYED, 0, "Disabled"
    49. AppendMenu hSubMenu, MF_STRING, 0, "Print"
    50. AppendMenu hSubMenu, MF_STRING, 0, "Exit"
    51. AppendMenu hMenu, MF_STRING Or MF_POPUP, hSubMenu, "File"
    52. End Sub
    53.  
    54. Private Sub Form_MouseUp(Button As Integer, Shift As Integer, myX As Single, myY As Single)
    55. Dim rc As RECT
    56. If Button = vbRightButton Then
    57. pt.x = myX
    58. pt.y = myY
    59. ClientToScreen Me.hwnd, pt
    60. TrackPopupMenu hSubMenu, TPM_LEFTALIGN Or TPM_BOTTOMALIGN, pt.x, pt.y, 0, Me.hwnd, rc
    61. End If
    62. End Sub
    63.  
    64. Private Sub Form_Unload(Cancel As Integer)
    65. DestroyMenu hMenu
    66. DestroyMenu hSubMenu
    67. End Sub
    Baaaaaaaaah

  5. #5

    Thread Starter
    New Member
    Join Date
    Aug 2002
    Location
    UK
    Posts
    7

    Thanks

    Hi

    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 ???

    Thanks

    Carl

  6. #6
    Fanatic Member
    Join Date
    Apr 2000
    Location
    Whats a location?
    Posts
    516
    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?
    Courgettes.

  7. #7

    Thread Starter
    New Member
    Join Date
    Aug 2002
    Location
    UK
    Posts
    7

    Dunno

    Hi

    It always returns 0 no matter what the selection is ??

  8. #8
    PowerPoster abdul's Avatar
    Join Date
    Dec 2000
    Location
    Ontario,Canada
    Posts
    2,827
    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:
    1. AppendMenu hSubMenu, MF_STRING, 0, "Open"
    2. AppendMenu hSubMenu, MF_STRING, 0, "Save"
    3. AppendMenu hSubMenu, MF_STRING Or MF_CHECKED, 0, "Checked"
    4. AppendMenu hSubMenu, MF_STRING Or MF_GRAYED, 0, "Disabled"
    5. AppendMenu hSubMenu, MF_STRING, 0, "Print"
    6. 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:
    1. public const MENU_OPEN 400
    2. public const MENU_SAVE 401
    3. public const MENU_CHECKED 402
    4. public const MENU_DSIABLED 403
    5. public const MENU_PRINT 404
    6. public const MENU_EXIT 405
    7.  
    8. AppendMenu hSubMenu, MF_STRING, MENU_OPEN, "Open"
    9. AppendMenu hSubMenu, MF_STRING, MENU_SAVE, "Save"
    10. AppendMenu hSubMenu, MF_STRING Or MF_CHECKED, MENU_CHECKED, "Checked"
    11. AppendMenu hSubMenu, MF_STRING Or MF_GRAYED, MENU_DISABLED, "Disabled"
    12. AppendMenu hSubMenu, MF_STRING, MENU_PRINT, "Print"
    13. AppendMenu hSubMenu, MF_STRING, MENU_EXIT, "Exit"

    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.
    Baaaaaaaaah

  9. #9

    Thread Starter
    New Member
    Join Date
    Aug 2002
    Location
    UK
    Posts
    7

    ?

    Hello again

    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.

    Thanks Again

    Carl

  10. #10
    Fanatic Member
    Join Date
    Apr 2000
    Location
    Whats a location?
    Posts
    516
    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.

    me.
    Courgettes.

  11. #11

    Thread Starter
    New Member
    Join Date
    Aug 2002
    Location
    UK
    Posts
    7

    Still no Return Value !

    Hello

    I have tried what said but again with no success.

    Used

    menusel = TrackPopupMenuEx(hPopupMenu, TPM_LEFTALIGN Or TPM_LEFTBUTTON Or TPM_NONOTIFY _
    Or TPM_RETURNCMD, curpos.x, curpos.y, hwnd, tpm)

    and gave each menu item a unique id.

    Should I try the subclass option ?? If so could be show me how to do this ??

    Many thanks

  12. #12
    Fanatic Member
    Join Date
    Apr 2000
    Location
    Whats a location?
    Posts
    516
    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.
    Courgettes.

  13. #13

    Thread Starter
    New Member
    Join Date
    Aug 2002
    Location
    UK
    Posts
    7

    Hehehe

    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.

  14. #14
    PowerPoster abdul's Avatar
    Join Date
    Dec 2000
    Location
    Ontario,Canada
    Posts
    2,827
    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:
    1. Option Explicit
    2.  
    3. Const MENU_OPEN = 400
    4. Const MENU_SAVE = 401
    5. Const MENU_CHECKED = 402
    6. Const MENU_DISABLED = 403
    7. Const MENU_PRINT = 404
    8. Const MENU_EXIT = 405
    9.  
    10. Private Type RECT
    11.         Left As Long
    12.         Top As Long
    13.         Right As Long
    14.         Bottom As Long
    15. End Type
    16. Private Type TPMPARAMS
    17.     cbSize As Long
    18.     rcExclude As RECT
    19. End Type
    20. Private Type POINTAPI
    21.         x As Long
    22.         y As Long
    23. End Type
    24.  
    25. Private Declare Function CreateMenu Lib "user32" () As Long
    26. Private Declare Function CreatePopupMenu Lib "user32" () As Long
    27. 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
    28. 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
    29. 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
    30. 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
    31. Private Declare Function DestroyMenu Lib "user32" (ByVal hMenu As Long) As Long
    32. Private Declare Function SetMenu Lib "user32" (ByVal hwnd As Long, ByVal hMenu As Long) As Long
    33. Private Declare Function DrawMenuBar Lib "user32" (ByVal hwnd As Long) As Long
    34. Private Declare Function ClientToScreen Lib "user32" (ByVal hwnd As Long, lpPoint As POINTAPI) As Long
    35.  
    36. Private Const MF_BYPOSITION = &H400&
    37. Private Const MF_STRING = &H0&
    38. Private Const MF_POPUP = &H10&
    39. Private Const MF_DISABLED = &H2&
    40. Private Const MF_CHECKED = &H8&
    41. Private Const MF_GRAYED = &H1&
    42. Private Const TPM_LEFTALIGN = &H0&
    43. Private Const TPM_CENTERALIGN = &H4&
    44. Private Const TPM_LEFTBUTTON = &H0&
    45. Private Const TPM_RIGHTALIGN = &H8&
    46. Private Const TPM_RIGHTBUTTON = &H2&
    47. Private Const TPM_RETURNCMD = &H100&
    48. Dim hMenu, hSubMenu As Long
    49. Dim pt As POINTAPI
    50.  
    51. Private Sub Form_Load()
    52. Me.ScaleMode = vbPixels
    53. hMenu = CreateMenu()
    54. hSubMenu = CreatePopupMenu()
    55. AppendMenu hSubMenu, MF_STRING, MENU_OPEN, "Open"
    56. AppendMenu hSubMenu, MF_STRING, MENU_SAVE, "Save"
    57. AppendMenu hSubMenu, MF_STRING Or MF_CHECKED, MENU_CHECKED, "Checked"
    58. AppendMenu hSubMenu, MF_STRING Or MF_GRAYED, MENU_DISABLED, "Disabled"
    59. AppendMenu hSubMenu, MF_STRING, MENU_PRINT, "Print"
    60. AppendMenu hSubMenu, MF_STRING, MENU_EXIT, "Exit"
    61. AppendMenu hMenu, MF_STRING Or MF_POPUP, hSubMenu, "File"
    62. End Sub
    63.  
    64. Private Sub Form_MouseUp(Button As Integer, Shift As Integer, myX As Single, myY As Single)
    65. Dim rc As RECT
    66. Dim retcmd As Long
    67. If Button = vbRightButton Then
    68. pt.x = myX
    69. pt.y = myY
    70. ClientToScreen Me.hwnd, pt
    71. retcmd = TrackPopupMenu(hSubMenu, TPM_LEFTALIGN Or TPM_RETURNCMD, pt.x, pt.y, 0, Me.hwnd, rc)
    72.  
    73. Select Case retcmd
    74. Case MENU_OPEN
    75. MsgBox "Open menu item clicked!"
    76. Case MENU_SAVE
    77. MsgBox "Save menu item clicked!"
    78. Case MENU_CHECKED
    79. MsgBox "Checked menu item clicked!"
    80. Case MENU_DISABLED
    81. MsgBox "Disabled menu item clicked!"
    82. Case MENU_PRINT
    83. MsgBox "Print menu item clicked!"
    84. Case MENU_EXIT
    85. MsgBox "Exit menu item clicked!"
    86. End Select
    87.  
    88. End If
    89. End Sub
    90.  
    91. Private Sub Form_Unload(Cancel As Integer)
    92. DestroyMenu hMenu
    93. DestroyMenu hSubMenu
    94. End Sub
    Baaaaaaaaah

  15. #15

    Thread Starter
    New Member
    Join Date
    Aug 2002
    Location
    UK
    Posts
    7

    Woooohooooo !!!

    We have a result !!!!! LOL Popup Menu can be used in Lotus Approach !!!!

    Thank you both so much.

    Cheers

    Carl.

  16. #16
    Fanatic Member
    Join Date
    Apr 2000
    Location
    Whats a location?
    Posts
    516
    aha, abdul, i think youll find i pointed that out first. maybe im entitled to some sort of prize. an OBE, perhaps. queenie, if your listening?
    Courgettes.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width