|
-
Nov 23rd, 2000, 02:09 PM
#1
Thread Starter
Frenzied Member
Hi,
How can I add a menu and delete a menu at run time?
An example would be appreciated..
Dan
-
Nov 23rd, 2000, 02:20 PM
#2
Fanatic Member
The best way is by using the api.
(Maybe this example does what you want)
-
Nov 23rd, 2000, 03:51 PM
#3
Use AppendMenu to add and RemoveMenu to remove.
Code:
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 RemoveMenu Lib "user32" (ByVal hMenu As Long, ByVal nPosition As Long, ByVal wFlags As Long) As Long
Private Declare Function GetMenu Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function GetSubMenu Lib "user32" (ByVal hMenu As Long, ByVal nPos As Long) As Long
Private Const MF_BYPOSITION = &H400&
Private Sub Command1_Click()
'Add a menu
AppendMenu GetSubMenu(GetMenu(hwnd), 0), MF_BYPOSITION, 0, "New Item"
End Sub
Private Sub Command2_Click()
'Remove a menu
RemoveMenu GetSubMenu(GetMenu(hwnd), 0), 0, MF_BYPOSITION
End Sub
-
Nov 23rd, 2000, 08:22 PM
#4
Thread Starter
Frenzied Member
Is the only way to do it through the API?
The reason I ask is that one of the skills measured for the MCSD Certification is to be able to:
"Create an application that adds and deletes menus at run time."
I'd be really suprised if they expect you to work with the API. In fact, the course that I took did not ever talk about this subject..
Is there a way to do this entirely within VB without calling the API? I just want to be prepared for the exam.
thanks,
Dan
-
Nov 28th, 2000, 07:47 AM
#5
Member
Load and Unload
Simple way is to load and unload during runtime. You must have 1 menuitem at the same level present and you cant add new subitems to a menuitem.
I used this code to add submenu's with page numbering. One menuitem must be there with index 0
Code:
Public Sub procSetPageNr()
On Error Resume Next
Dim giCounter as Integer
'clear excisting pages
If mnuGoBarToPageNr.Count > 1 Then
For giCounter = mnuGoBarToPageNr.Count To 1
Unload frmMain.mnuGoBarToPageNr(giCounter)
Next giCounter
End If
'ad new items
For giCounter = 2 To giPageCounter
Load frmMain.mnuGoBarToPageNr(giCounter)
mnuGoBarToPageNr(giCounter).Visible = True
mnuGoBarToPageNr(giCounter).Caption = "Page" & giCounter
Next giCounter
End Sub
It's no good this way, API code is much better but probably not what you need to know for your course.
A mind is like a parachute, it has to open to let it work
www.2beesoft.com for Icon Manager with over 20.000 free icons
VB6 Ent. SP4, ASP, W2000/W98
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
|