HRESULT AddMenuItems(
  LPDATAOBJECT piDataObject,          // pointer to a data object
  LPCONTEXTMENUCALLBACK piCallback,   // pointer to callback itf
  long * pInsertionAllowed            // pointer to insertion pt flags
);
Parameters
piDataObject 
[in] Pointer to the IDataObject interface on the data object of the menu to which items are being added. 
piCallback 
[in] Pointer to an IContextMenuCallback that can add items to the context menu. 
pInsertionAllowed 
[in, out] Identifies MMC-defined menu-item insertion points that can be used. This can be a combination of the following flags: Value Meaning 
CCM_INSERTIONALLOWED_TOP Items can be inserted at the top of a context menu. 
CCM_INSERTIONALLOWED_NEW Items can be inserted in the New submenu. 
CCM_INSERTIONALLOWED_TASK Items can be inserted in the All Tasks submenu. 
CCM_INSERTIONALLOWED_VIEW Items can be inserted in the toolbar view menu or in the View submenu of the result pane context menu. 
Return Values
S_OK 
Zero or more items were successfully added to the context menu. 
E_UNEXPECTED 
An unexpected error occurred. 
Res
An implementation of IExtendContextMenu::AddMenuItems typically reads the node type and any other parameters needed by calling IDataObject::GetDataHere on piDataObject, then adds context menu items as appropriate by calling IContextMenuCallback::AddItem on piCallback.Your snap-in should check the pInsertionsAllowed flags for permission before attempting to add menu items at the MMC-defined insertion points. For example, a snap-in should not add menu items to CCM_INSERTIONPOINTID_PRIMARY_NEW or CCM_INSERTIONPOINTID_3RDPARTY_NEW unless the CCM_INSERTIONALLOWED_NEW flag is set.The pInsertionsAllowed flags allow the following two features: Subset Context Menus 
The console can create context menus that contain a subset of the full context menu items, such as the toolbar View menu. This menu is created in the same manner as a typical context menu except that only the CCM_INSERTIONALLOWED_VIEW flag is set in calls to the snap-in's AddMenuItems method. 
Extension Snap-in Restrictions 
A primary snap-in is permitted to reset any of the insertion flags in its AddMenuItems method as a way of restricting the kinds of menu items that a third-party extension can add. For example, the primary snap-in can clear the CCM_INSERTIONALLOWED_NEW flag to prevent extensions from adding their own New menu items. 
The primary snap-in should not attempt to set bits in pInsertionAllowed that were originally cleared. This is because future versions of MMC may use bits not currently defined. Third-party extensions should not attempt to change pInsertionAllowed at all. If the user selects a scope item and then displays its context menu, MMC will give both the snap-in's IComponentData and IComponent (that owns the current view) implementations the opportunity to add menu items. MMC calls the IExtendContextMenu::AddMenuitems method implemented by the snap-in's IComponent implementation to allow the snap-in to add menu items to the View menu. MMC calls the IExtendContextMenu::AddMenuitems method implemented by the snap-in's IComponentData to allow the snap-in to add menu items to all other menus. Only the snap-in's IComponent implementation can add items to the View menu.If the user displays a scope item's context menu without first selecting the scope item, MMC will only give the snap-in's IComponentData implementation the opportunity to add menu items to all menus except for the View menu. Consequently, the View menu only appears for a scope item if the user first selects an item.Notes to Callers
The AddMenuItems method should not call AddRef on either the piDataObject pointer or the piCallback pointer, nor should it call the methods of those interfaces after returning. Instead, it should make all necessary calls to the methods of those interfaces before returning. If any of these items is selected, you will be given back the pointer to IDataObject in IExtendContextMenu::Command, so do not keep this pointer after this method returns. You will not be notified if the menu is dismissed without any of your items being selected. In addition, do not query for alternate interfaces from piCallback because the one method, IContextMenuCallback::AddItem, should be sufficient.Requirements 
  Version: Requires MMC 1.0. Available as a redistributable for Windows 95/98 and Windows NT 4.0 SP1.
  Header: Declared in Mmc.h.
  Library: Use Mmc.lib.

解决方案 »

  1.   

    AddMenuItems 在vb里怎么用啊?
      

  2.   

    先建一个控件数组,设计时隐藏,需要时load menu1(i),在设为可见!
      

  3.   

    to happybeyond(马丁) 
    那它的子菜单怎么创建啊?他的子菜单的子菜单呢?
      

  4.   

    不太喜欢VB,但喜欢蹭分。调用API时,先要声明,但不要忘了声明必要的常量参数
      

  5.   

    我建议你还是不要用API来得效率高,而且方便简洁。happybeyond(马丁) 说得对,建立一个菜单数组,使用Load来创建菜单,Unload来销毁菜单就可以了。
    要创建子菜单的话你可以先用菜单编辑器事先编辑好一个菜单数组,如:
    mnuParent
    ...mnuChild
    你可以分别设置各个的visible=false
    在要创建动态父菜单的时候就可以 
    dim i as integer
    i=mnuparent.count '获取最大的索引数,如果菜单集合里的菜单索引号会无序地排列的话,你就得用for each mnu in mnuparent...来依次找出空缺的index来进行创建了
    load mnuParent(i)
    mnuparent(i).caption="父菜单"
    ...
    mnuparent(i).visible=true
    子菜单的话同上,只是Load的对象不同而已,如下
    ...
    load mnuchild(i)
      

  6.   

    to ztchen(ZT) 
    你有没有试过啊?我这里这么做得好象是不可以啊。load的子菜单出现在原是菜单下面了。
      

  7.   

    更正:load的子菜单出现在原始菜单下面了。
      

  8.   

    AppendMenu VB声明 
    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 
    说明 
    在指定的菜单里添加一个菜单项 
    返回值 
    Long,非零表示成功,零表示失败。会设置GetLastError 
    参数表 
    参数 类型及说明 
    hMenu Long,菜单句柄 
    wFlags Long,参考ModifyMenu函数中的菜单常数标志定义表,其中列出了允许使用的所有常数 
    wIDNewItem Long,指定菜单条目的新命令ID。如果在wFlags参数中指定了MF_POPUP字段,那么这应该是指向一个弹出式菜单的句柄 
    lpNewItem String(相应的vb声明见注解),如果在wFlags参数中指定了MF_STRING标志,这就代表在菜单中设置的字串。如设置了MF_BITMAP标志,这就代表一个Long型变量,其中包含了一个位图句柄。如设置了MF_OWNERDRAW,这个值就会包括在DRAWITEMSTRUCT和MEASUREITEMSTRUCT结构中,在条目需要重画的时候由windows发送出去 
    ====================================================================
    InsertMenu VB声明 
    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 
    说明 
    在菜单的指定位置处插入一个菜单条目,并根据需要将其他条目向下移动 
    返回值 
    Long,非零表示成功,零表示失败。会设置GetLastError 
    参数表 
    参数 类型及说明 
    hMenu Long,菜单的句柄 
    nPosition Long,定义了新条目插入点的一个现有菜单条目的标志符。如果在wFlags中指定了MF_BYCOMMAND标志,这个参数就代表欲改变的菜单条目的命令ID。如设置的是MF_BYPOSITION标志,这个参数就代表菜单条目在菜单中的位置,第一个条目的位置为零 
    wFlags Long,一系列常数标志的组合。参考ModifyMenu 
    wIDNewItem Long,指定菜单条目的新菜单ID。如果在wFlags中指定了MF_POPUP标志,就应该指定弹出式菜单的一个句柄 
    lpNewItem 如果在wFlags参数中设置了MF_STRING标志,就代表要设置到菜单中的字串(String)。如设置的是MF_BITMAP标志,就代表一个Long型变量,其中包含了一个位图句柄 
      

  9.   

    再响应WM_Command消息处理菜单点击
    一切OK!
      

  10.   

    Receiving MessagesVisual Basic controls and forms constantly receive messages and translate them into events or attributes according to instructions in the messages. For example, let’s say that you used the Menu Editor to define a File menu named mnuFile with items mnuNew, mnuOpen, and so on. When the user of your application chooses Open, the mnuOpen_Click event occurs, and the code in the mnuOpen_Click event procedure executes.
    But what really happens behind the scenes? Windows gets a signal from the mouse port that a click has occurred at a certain screen location (or possibly it discovers that the Alt, F, and O keys have been pressed consecutively). Windows checks its internal information to find out which window owns that location and discovers that the click occurred on the Open item of the File menu. It then sends a slew of messages related to this event, culminating in a WM_COMMAND message with the ID number of the Open item.
    Meanwhile, the window procedure for the form containing the menu has a Select Case block similar to the one shown here:Select Case iMessageCase WM_COMMAND    Select Case wParam    Case IDM_NEW        mnuNew_Click    Case IDM_OPEN        mnuOpen_Click    §    End Select§End SelectThis code intercepts the WM_COMMAND message, and the mnuOpen_Click event starts executing. In short, Windows sends messages; Visual Basic turns them into events.
    Now consider the system menu. By default, Visual Basic puts the standard system menu on all forms whose ControlBox property is set to True. When a user chooses from a system menu, Windows sends the WM_SYSCOMMAND message to the window (form). The window procedure for a form works this way:‘ Set default return valueWindowProc = 0&‘ Handle messagesSelect Case iMessageCase WM_DOHOPEVENT    Form.HopEvent_ClickCase WM_GETJUMPDATA    Form.JumpData = wParamCase Else    ‘ Let default window procedure handle the rest    WindowProc = DefWindowProc(hWnd, iMessage, wParam, lParam)End SelectBecause the WM_SYSCOMMAND message isn’t handled by a specific Case statement, it falls through to the Case Else statement to be handled by DefWindow­Proc. The default window procedure knows how to do the standard operations—move, size, minimize, maximize, close, and switch to—which are the same for any window.
    But what if you add an About item to the system menu? Visual Basic won’t help you do this, but neither will it stand in your way. It’s a simple matter to add an item to the system menu using Windows API calls:Const IDM_ABOUT = 1010Dim hSysMenu As Long‘ Get handle of system menuhSysMenu = GetSystemMenu(hWnd, 0&)‘ Append separator and menu item with ID IDM_ABOUTCall AppendMenu(hSysMenu, MF_SEPARATOR, 0&, 0&)Call AppendMenu(hSysMenu, MF_STRING, IDM_ABOUT, “About...”)When the user selects this new menu item, Windows sends a WM_SYS­COMMAND message with the IDM_ABOUT value to the window procedure. Having no clue what to do with this message, the window procedure passes it off to Def­WindowProc, which also hasn’t a clue. Your message rides off into the sunset.
      

  11.   

    to zyl910(910:分儿,我来了!) 
    你用的是那个版本的msdn啊?
    这么厉害。
    还是你自己翻译的。
      

  12.   

    大哥你好,最近颇困惑:)
    请问大哥可否赐教关于用VB画图的技巧!尤其是菱形!
    显示在敞口的一个图形匡里显示个菱形
    拜托了!很急的!
    我的QQ4081531
    邮箱[email protected]