Solved

CREATE MENU

Posted on 2000-02-24
7
297 Views
Last Modified: 2008-02-20
how to create any menu item from code...
not sub menu ....
for example how to create menu item called FILE in next structure with next sub menus from code...


FILE
--Open
--Save
--
--Close


Please provide some samples and examples for this.

Thanks.
0
Comment
Question by:bokikg
7 Comments
 
LVL 4

Expert Comment

by:TigerZhao
Comment Utility
'use menueditor create a submenu of mnuFile
caption = "-"
name = "mnuFileSub"
index = 0

Sub CreateSubMenus()
    mnuFileSub(0).Visible = True
    While mnuFileSub.Count > 1
        Unload mnuFileSub(mnuFileSub.Count - 1)
    Wend
    ' create submenus as you wish
    Load mnuFileSub(1)
    mnuFileSub(1).Caption = "&Open"
    mnuFileSub(1).Visible = True
    '...
    mnuFileSub(0).Visible = (mnuFileSub.Count = 1)
End Sub

0
 

Author Comment

by:bokikg
Comment Utility
you didn't figer out ... how to make menu item called FILE not submenu... i know to that.... how to create parent menu item not submenu item from code....
0
 
LVL 4

Expert Comment

by:TigerZhao
Comment Utility
so sorry. Now I'm try exactly way, need some time...
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 1

Expert Comment

by:HunterKiller
Comment Utility
Why dont you create the menu whit the editor, make it invisible, and when you want to show it, make it visible ?

Or explain better your wish..
0
 
LVL 9

Accepted Solution

by:
Ruchi earned 100 total points
Comment Utility
From the web:

---Module-----

Option Explicit
Private Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hwnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Public Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long

Private Declare Function CreateMenu Lib "user32" () 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 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 CreatePopupMenu Lib "user32" () As Long
Public Declare Function GetSystemMenu Lib "user32" (ByVal hwnd As Long, ByVal bRevert As Long) As Long

Private Const MF_ENABLED = &H0&
Private Const MF_POPUP = &H10&
Private Const MF_SEPARATOR = &H800&
Private Const MF_STRING = &H0&

Private Const WM_MENUSELECT = &H11F
Public Const GWL_WNDPROC = (-4)

Public lPrevWnd As Long
Public lSysMnu As Long
Public lMnu As Long

Public Sub CreateMenuBar()
    'Generate a Simple File Menu..
    Dim lSubMnu As Long
    Dim lRes As Long
    lMnu = CreateMenu           'Create a Menu Item
    lSubMnu = CreatePopupMenu   'Create a Popup Menu
    'Add Items to the Popup Menu
    'Each Item Requires a Unique ID to Identify it in our Menu Event
    lRes = AppendMenu(lSubMnu, MF_ENABLED Or MF_STRING, 1, ByVal "&Open")
    lRes = AppendMenu(lSubMnu, MF_ENABLED Or MF_STRING, 2, ByVal "&Save")
    lRes = AppendMenu(lSubMnu, MF_ENABLED Or MF_STRING, 3, ByVal "Save &As..")
    lRes = AppendMenu(lSubMnu, MF_ENABLED Or MF_STRING, 4, ByVal "&Exit")
    'Add the Popup Menu to the Main File Menu Item..
    lRes = AppendMenu(lMnu, MF_ENABLED Or MF_STRING Or MF_POPUP, lSubMnu, ByVal "File")
    'Assign the Menu to the Form
    lRes = SetMenu(Form1.hwnd, lMnu)
    'Draw the Menu, Only works when the Form is Visible.
    lRes = DrawMenuBar(Form1.hwnd)
End Sub

Public Function SubClassedForm(ByVal hwnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    Static lLastItemSelected As Long
    If Msg = &H105A Then
        'Redraw Menu
        Call SetMenu(Form1.hwnd, lMnu)
        Call DrawMenuBar(Form1.hwnd)
    ElseIf Msg = WM_MENUSELECT And lParam Then
        'Process Messages From Any of the Forms Menus, Except the System Menu
        If lParam Then
            'A Valid Item was Selected
            'Store the Index in the Static Var Until the Item is Clicked
            lLastItemSelected = wParam And 255
        Else
            'Call the MenuEvent Sub with the Last Selected Menu Item ID
            Call MenuEvent(lLastItemSelected)
            lLastItemSelected = 0
        End If
    End If
    SubClassedForm = CallWindowProc(lPrevWnd, hwnd, Msg, wParam, lParam)
End Function

Public Sub MenuEvent(ByVal Index As Long)
    'Menu Item Code Goes Here,
    'Each Menu Item is Identified by it's Unique Index
    With Form1
        Select Case Index
            Case 1
                .Caption = "Open"
                'Open Item Code
            Case 2
                .Caption = "Save"
                'Save Item Code
            Case 3
                .Caption = "Save As.."
                'Save As Item Code
            Case 4
                .Caption = "Close"
                'Exit Item Code
                Unload Form1
            Case Else
                .Caption = "No Item Selected"
        End Select
    End With
End Sub



----Form------
Option Explicit
Private Sub Form_Load()
    'Get the System Menu Handle, so we don't process it's Messages
    lSysMnu = GetSystemMenu(hwnd, 0)
    'Sub-Class the Form to Capture the Menu Messages
    lPrevWnd = SetWindowLong(hwnd, GWL_WNDPROC, AddressOf SubClassedForm)
    'Create the Menu Completely at Runtime.
    CreateMenuBar
End Sub

Private Sub Form_Unload(Cancel As Integer)
    'Remove the Form Sub-Classing *** DO NOT REMOVE ***
    Call SetWindowLong(hwnd, GWL_WNDPROC, lPrevWnd)
End Sub
0
 

Author Comment

by:bokikg
Comment Utility
supper man thanks a lot...
0
 
LVL 4

Expert Comment

by:billcute
Comment Utility
Ruchi,
I saw your code during my EE search engine for this topic. I tried to implement your codes as suggested but got numerous errors while trying it. I know it's about 5 years you did this - I am sure you probably have a better way of handling the MENU creation in Access. I'll definitely appreciate it if you could take a look at my new post (see the link below) on the subject matter.

My post was done very recently and the question is very much similar to the one you handled here on February 25, 2000 I will appreciate it if you could drop me a line by clicking on my userid which will take you to my profile. Below is the link to my new post.

http://www.experts-exchange.com/Databases/MS_Access/Q_21373657.html
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now