Results 1 to 5 of 5

Thread: [RESOLVED] Adding controls to PopUpMenu

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2006
    Location
    3rd rock from the sun
    Posts
    360

    Resolved [RESOLVED] Adding controls to PopUpMenu

    heya all

    is there a way to add controls to a pop up menu at run time?
    e.g., adding "Cut" opiton into an Edit Mnu, not via the Menu Editor but via code at runtime
    (also not "Recent Accessed files", but a new option to select...)

    tnx in advance for any suggestions,
    regards,

  2. #2
    VB Guru ganeshmoorthy's Avatar
    Join Date
    Dec 2005
    Location
    Sharjah, United Arab Emirates
    Posts
    3,031

    Re: Adding controls to PopUpMenu

    you have to have control array for that menu...example when you create menu set its index to 0 and at run time use load command to add to it...
    If an answer to your question has been helpful, then please, Rate it!

    Have done Projects in Access and Member management systems using BioMetric devices, Smart cards and BarCodes.


  3. #3
    VB Guru ganeshmoorthy's Avatar
    Join Date
    Dec 2005
    Location
    Sharjah, United Arab Emirates
    Posts
    3,031

    Re: Adding controls to PopUpMenu

    try this example
    VB Code:
    1. Static i As Integer
    2.     Load mnuNew(i + 1)
    3.     mnuNew(i + 1).Caption = "New " & i + 1
    4.     i = i + 1
    If an answer to your question has been helpful, then please, Rate it!

    Have done Projects in Access and Member management systems using BioMetric devices, Smart cards and BarCodes.


  4. #4
    Frenzied Member Andrew G's Avatar
    Join Date
    Nov 2005
    Location
    Sydney
    Posts
    1,587

    Re: Adding controls to PopUpMenu

    You can also use APIs but its more difficult. Best use ganeshmoorthy's suggestion.

    Just incase you want to use the API heres an example from API Guide that adds a new menu item on the system menu then checks for a click.
    VB Code:
    1. 'Courtesy of Paul Kuliniewicz ([url]www.vbapi.com[/url])
    2.  
    3. ' Declarations and such needed for the example:
    4. ' (Copy them to the (declarations) section of a module.)
    5. ' There's quite a few declarations for this example, but it's worth it!
    6. Public Declare Function GetSystemMenu Lib "user32.dll" (ByVal hWnd As Long, ByVal bRevert As Long) As Long
    7. Public Declare Function GetMenuItemCount Lib "user32.dll" (ByVal hMenu As Long) As Long
    8. Public Type MENUITEMINFO
    9.     cbSize As Long
    10.     fMask As Long
    11.     fType As Long
    12.     fState As Long
    13.     wID As Long
    14.     hSubMenu As Long
    15.     hbmpChecked As Long
    16.     hbmpUnchecked As Long
    17.     dwItemData As Long
    18.     dwTypeData As String
    19.     cch As Long
    20. End Type
    21. Public Const MIIM_STATE = &H1
    22. Public Const MIIM_ID = &H2
    23. Public Const MIIM_TYPE = &H10
    24. Public Const MFT_SEPARATOR = &H800
    25. Public Const MFT_STRING = &H0
    26. Public Const MFS_ENABLED = &H0
    27. Public Const MFS_CHECKED = &H8
    28. 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
    29. Public Declare Function SetMenuItemInfo Lib "user32.dll" Alias "SetMenuItemInfoA" (ByVal hMenu As Long, ByVal uItem As Long, ByVal fByPosition As Long, lpmii As MENUITEMINFO) As Long
    30. Public Declare Function SetWindowPos Lib "user32.dll" (ByVal hWnd As Long, ByVal hWndInsertAfter As Long, ByVal x As Long, ByVal y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long
    31. Public Const HWND_TOPMOST = -1
    32. Public Const HWND_NOTOPMOST = -2
    33. Public Const SWP_NOMOVE = &H2
    34. Public Const SWP_NOSIZE = &H1
    35. Public Declare Function SetWindowLong Lib "user32.dll" Alias "SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
    36. Public Const GWL_WNDPROC = -4
    37. Public Declare Function CallWindowProc Lib "user32.dll" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hWnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    38. Public Const WM_SYSCOMMAND = &H112
    39. Public Const WM_INITMENU = &H116
    40.  
    41. ' Add an option to make window Form1 "Always On Top" to the bottom of its system
    42. ' menu.  A check mark appears next to this option when active.  The menu item acts as a toggle.
    43. ' Note how subclassing the window is necessary to process the two messages needed
    44. ' to give the added system menu item its full functionality.
    45.  
    46. ' *** Place the following code in a module. ***
    47.  
    48. Public pOldProc As Long  ' pointer to Form1's previous window procedure
    49. Public ontop As Boolean  ' identifies if Form1 is always on top or not
    50.  
    51. ' The following function acts as Form1's window procedure to process messages.
    52. Public Function WindowProc (ByVal hwnd As Long, ByVal uMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    53.     Dim hSysMenu As Long     ' handle to Form1's system menu
    54.     Dim mii As MENUITEMINFO  ' menu item information for Always On Top
    55.     Dim retval As Long       ' return value
    56.    
    57.     Select Case uMsg
    58.     Case WM_INITMENU
    59.         ' Before displaying the system menu, make sure that the Always On Top
    60.         ' option is properly checked.
    61.         hSysMenu = GetSystemMenu(hwnd, 0)
    62.         With mii
    63.             ' Size of the structure.
    64.             .cbSize = Len(mii)
    65.             ' Only use what needs to be changed.
    66.             .fMask = MIIM_STATE
    67.             ' If Form1 is now always on top, check the item.
    68.             .fState = MFS_ENABLED Or IIf(ontop, MFS_CHECKED, 0)
    69.         End With
    70.         retval = SetMenuItemInfo(hSysMenu, 1, 0, mii)
    71.         WindowProc = 0
    72.     Case WM_SYSCOMMAND
    73.         ' If Always On Top (ID = 1) was selected, change the on top/not on top
    74.         ' setting of Form1 to match.
    75.         If wParam = 1 Then
    76.             ' Reverse the setting and make it the current one.
    77.             ontop = Not ontop
    78.             retval = SetWindowPos(hwnd, IIf(ontop, HWND_TOPMOST, HWND_NOTOPMOST), 0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZE)
    79.             WindowProc = 0
    80.         Else
    81.             ' Some other item was selected.  Let the previous window procedure
    82.             ' process it.
    83.             WindowProc = CallWindowProc(pOldProc, hwnd, uMsg, wParam, lParam)
    84.         End If
    85.     Case Else
    86.         ' If this is some other message, let the previous procedure handle it.
    87.         WindowProc = CallWindowProc(pOldProc, hwnd, uMsg, wParam, lParam)
    88.     End Select
    89. End Function
    90.  
    91. ' *** Place the following code inside Form1. ***
    92.  
    93. ' When Form1 loads, add Always On Top to the system menu and set up the
    94. ' new window procedure.
    95. Private Sub Form_Load()
    96.     Dim hSysMenu As Long     ' handle to the system menu
    97.     Dim count As Long        ' the number of items initially on the menu
    98.     Dim mii As MENUITEMINFO  ' describes a menu item to add
    99.     Dim retval As Long       ' return value
    100.    
    101.     ' Get a handle to the system menu.
    102.     hSysMenu = GetSystemMenu(Form1.hWnd, 0)
    103.     ' See how many items are currently in it.
    104.     count = GetMenuItemCount(hSysMenu)
    105.    
    106.     ' Add a separator bar and then Always On Top to the system menu.
    107.     With mii
    108.         ' The size of the structure.
    109.         .cbSize = Len(mii)
    110.         ' What parts of the structure to use.
    111.         .fMask = MIIM_ID Or MIIM_TYPE
    112.         ' This is a separator.
    113.         .fType = MFT_SEPARATOR
    114.         ' It has an ID of 0.
    115.         .wID = 0
    116.     End With
    117.     ' Add the separator to the end of the system menu.
    118.     retval = InsertMenuItem(hSysMenu, count, 1, mii)
    119.    
    120.     ' Likewise, add the Always On Top command.
    121.     With mii
    122.         .fMask = MIIM_STATE Or MIIM_ID Or MIIM_TYPE
    123.         ' This is a regular text item.
    124.         .fType = MFT_STRING
    125.         ' The option is enabled.
    126.         .fState = MFS_ENABLED
    127.         ' It has an ID of 1 (this identifies it in the window procedure).
    128.         .wID = 1
    129.         ' The text to place in the menu item.
    130.         .dwTypeData = "&Always On Top"
    131.         .cch = Len(.dwTypeData)
    132.     End With
    133.     ' Add this to the bottom of the system menu.
    134.     retval = InsertMenuItem(hSysMenu, count + 1, 1, mii)
    135.    
    136.     ' Set the custom window procedure to process Form1's messages.
    137.     ontop = False
    138.     pOldProc = SetWindowLong(Form1.hWnd, GWL_WNDPROC, AddressOf WindowProc)
    139. End Sub
    140.  
    141. ' Before unloading, restore the default system menu and remove the
    142. ' custom window procedure.
    143. Private Sub Form_Unload(Cancel As Integer)
    144.     Dim retval As Long  ' return value
    145.    
    146.     ' Replace the previous window procedure to prevent crashing.
    147.     retval = SetWindowLong(Form1.hWnd, GWL_WNDPROC, pOldProc)
    148.     ' Remove the modifications made to the system menu.
    149.     retval = GetSystemMenu(Form1.hWnd, 1)
    150. End Sub
    Last edited by Andrew G; Apr 20th, 2006 at 03:17 AM.

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2006
    Location
    3rd rock from the sun
    Posts
    360

    Resolved Re: Adding controls to PopUpMenu

    Quote Originally Posted by ganeshmoorthy
    try this example
    VB Code:
    1. Static i As Integer
    2.     Load mnuNew(i + 1)
    3.     mnuNew(i + 1).Caption = "New " & i + 1
    4.     i = i + 1
    this works fine, just wat i needed. tnx ganeshmoorthy.

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