|
-
Jan 10th, 2001, 08:21 AM
#1
Thread Starter
Member
How can I add submenus to a menu, which do not have submenus at this point, at runtime?
Scand
-
Jan 10th, 2001, 09:07 AM
#2
Fanatic Member
Make the menu items invisible at design time then visible at runtime. Otherwise change the enabled state to grey them out.
-
Jan 10th, 2001, 09:22 AM
#3
Thread Starter
Member
Tnx, but I want to add/create a NEW submenu not enable an existing.
How can I do this? Is there any CreateMenu Funktion or so?
Scand
-
Jan 10th, 2001, 09:28 AM
#4
Frenzied Member
You can create a control array of it, or if you need it for popup menus:
Code:
Option Explicit
'The Menu Constants
Private Const MIIM_STATE = &H1
Private Const MIIM_ID = &H2
Private Const MIIM_TYPE = &H10
Private Const MFT_SEPARATOR = &H800
Private Const MFT_STRING = &H0
Private Const MFS_DEFAULT = &H1000
Private Const MFS_ENABLED = &H0
Private Const MF_BITMAP = 4
Private Const MF_CHECKED = 8
Private Const MF_UNCHECKED = &H0&
Private Const MF_STRING = &H0&
'These constants are for us, to identify which item was clicked
Private Const ID_FIRSTITEM = 101
Private Const ID_SECONDITEM = 102
'You may want to change the names and/or add some more.
'More Menu Constants
Private Const TPM_LEFTALIGN = &H0
Private Const TPM_TOPALIGN = &H0
Private Const TPM_NONOTIFY = &H80
Private Const TPM_RETURNCMD = &H100
Private Const TPM_LEFTBUTTON = &H0
'This is the type to send with the API call, it specifies how the menu should be displayed etc.
Private Type MENUITEMINFO
cbSize As Long
fMask As Long
fType As Long
fState As Long
wID As Long
hSubMenu As Long
hbmpChecked As Long
hbmpUnchecked As Long
dwItemData As Long
dwTypeData As String
cch As Long
End Type
'Api calls for menu handling
Private Declare Function GetMenuItemID Lib "user32" (ByVal hMenu As Long, ByVal nPos As Long) As Long
Private Declare Function ModifyMenu Lib "user32" Alias "ModifyMenuA" (ByVal hMenu As Long, ByVal nPosition As Long, ByVal wFlags As Long, ByVal wIDNewItem As Long, ByVal lpString As Any) 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, ByVal lprc As Any) As Long
Private Declare Function DestroyMenu Lib "user32" (ByVal hMenu As Long) As Long
Private 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
'These are used to specify the menuHandle and if the first item is checked
Private hMenu As Long, checked As Boolean
Private Sub Form_Load()
Dim info As MENUITEMINFO
hMenu = CreatePopupMenu() 'Create the menu
With info
'this is the info for the first menu
.cbSize = Len(info)
.fMask = MIIM_STATE Or MIIM_ID Or MIIM_TYPE
.fType = MFT_STRING 'declare the type of the item
.fState = MFS_ENABLED Or MFS_DEFAULT 'the flags
.wID = ID_FIRSTITEM 'declared above, using CONST
.dwTypeData = "&This is the first item" 'the text for the new item
.cch = Len(.dwTypeData)
'the same for the second item and so on
End With
InsertMenuItem hMenu, 0, 1, info 'inserts the menuitem
With info
'info for the second item
.cbSize = Len(info)
.fMask = MIIM_STATE Or MIIM_ID Or MIIM_TYPE
.fType = MFT_STRING
.fState = MFS_ENABLED
.wID = ID_SECONDITEM
.dwTypeData = "&This is the second item" 'text
.cch = Len(.dwTypeData)
End With
InsertMenuItem hMenu, 1, 1, info 'and again, insert it.
End Sub
Private Sub Form_MouseUp(Button As Integer, Shift As Integer, x As Single, y As Single)
Dim retval&, sMenu&
'This event is fired when the user releases the mousebutton from the form (when clicked)
If Button = vbRightButton Then 'right button
retval = TrackPopupMenu(hMenu, TPM_TOPALIGN Or TPM_LEFTALIGN Or TPM_NONOTIFY _
Or TPM_RETURNCMD Or TPM_LEFTBUTTON, 100, 200, 0, Me.hWnd, ByVal 0& 'pops up the menu at point 100 (x), 200 (y)
'If you want to display it, get the point with the GetCursorPos API (not included here )
Select Case retval 'gets the message retrieved by the pop-up menu
Case ID_FIRSTITEM 'in this case it's the first item (use the constants you declared)
'Now we gonna check/uncheck it
sMenu = GetMenuItemID(hMenu, 0) 'gets the handle of the menu-item
checked = Not checked 'reverse the boolean to check/uncheck the item
If checked Then
ModifyMenu hMenu, sMenu, MF_CHECKED, ID_FIRSTITEM, "This is the first item" 'modify the item to check
Else
ModifyMenu hMenu, sMenu, MF_UNCHECKED, ID_FIRSTITEM, "This is the first item" 'modify the item to uncheck
End If
Case ID_SECONDITEM
sMenu = GetMenuItemID(hMenu, 1)
ModifyMenu hMenu, sMenu, MF_STRING, ID_SECONDITEM, "Now the text changed huh " 'modify the item to change the text
'If you have more items, add
'Case ID_MYID
'...code you want to run...
'and so on.
End Select
End If
End Sub
Private Sub Form_Unload(Cancel As Integer)
'Destroy our menu
DestroyMenu hMenu
End Sub
Jop - validweb.nl
Alcohol doesn't solve any problems, but then again, neither does milk.
-
Jan 10th, 2001, 09:34 AM
#5
Thread Starter
Member
Thanks a lot Jop.
I'll try it.
Scand
-
Jan 10th, 2001, 09:34 AM
#6
New Member
use the load and unload
create a sub menu(mnuIndex), but set the index to 0 and it's visible property to false.
now from your code when at runtime your add the menu you use the following code.
load mnuIndex(intCnt)
mnuIndex.caption = Something
mnuIndex.visible = true
to remove that menu
unload mnuIndex(intCnt)
this is usefull for when you don't know what menu's will be needed during design time and is a bit more flexable than the prevous example
-
Jan 10th, 2001, 09:41 AM
#7
Fanatic Member
Nice one Jop, but with this method, although clever, there is still the need to write the menu event code, for each created menu. I am assuming this could be anything! so use WithEvents for each indexed item. This is now getting very complex, and difficult to support, so I'd still still switch them on and off instead.
-
Jan 15th, 2001, 04:43 AM
#8
Thread Starter
Member
Thank you all for your help.
I've tried it and it does not work with my application.
Perhaps I've to explain my problem exactly.
I have a menu:
Caption: Test
Name: mnuTest
In this menu i have 10 menu items which are disabled and called mnuTestx(Count)
These menu-item's I do enable in my Form_Load.
Now my problem is, to make a specific item to a submenu and add items to it.
I've the handle to the menuitem i would like to make it to a submenu.
mnuTestx(ii)
My question:
How can I make the item to a submenu and add items to it?
I hope you understand my problem. When not, please ask what's unclearly.
Greets
Scand
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|