- UID
- 1
- 积分
- 15891
- 精华
- 贡献
-
- 威望
-
- 活跃度
-
- D豆
-
- 在线时间
- 小时
- 注册时间
- 2002-1-3
- 最后登录
- 1970-1-1
|
发表于 2002-11-8 19:23:08
|
显示全部楼层
给你贴篇用ACTIVEX创建POP菜单的资料,你看看,讲的比较详细了。
你的问题解决后,希望把完整的代码贴到论坛和大家分享。
- [FONT=courier new]
- How to create a pull-down popup menu through ActiveX?
- ID 17818
- Applies to: AutoCAD
-
- This document is part of ObjectARX COM-ActiveX Interfaces Menus and Toolbars MFC
- Question
- I'm trying to access the menu groups and popup menus exposed through Automation,
- but there are so many different objects, menu groups, menu group, popup menus,
- and popup menu item. Is there a client sample in C++?
- Answer
- It can be confusing to sort through so many objects and use them. A good way to
- understand their relationships is to look at the Automation Object Model because
- it depicts a hierarchical relationship among the menu objects as well as with
- others.
- The following code creates a dialog-based client application, assigns a button
- to handle "Start Acad", and assigns a button to handle "Add a menu item". (The
- following code assumes you know how to create a dialog-based MFC application.)
- In the dialog's header file, the following member variables were declared (you
- need to import interface classes from the acad.tlb file using the ClassWizard,
- Add class..., choose From type library. Be sure not to use #import).
- IAcadApplication m_IApp;
- IAcadDocument m_IDoc;
- IAcadModelSpace m_IMSpace;
-
- IAcadMenuGroups m_IMenuGrps;
- IAcadMenuGroup m_IMenuGrp;
- IAcadPopupMenus m_IPopupMenus;
- IAcadPopupMenu m_IPopupMenu;
- IAcadPopupMenuItem m_IPopupMenuItem;
- In the dialog's cpp file, add the following (2) handlers:
- //
- // This function starts Acad and initialize the appropriate
- // interface objects
- //
- void CClientDlg::OnStartAcad()
- {
- HRESULT hr = NOERROR;
- CLSID clsid;
- LPUNKNOWN pUnk = NULL;
- LPDISPATCH pDisp = NULL;
- BeginWaitCursor();
- CoInitialize(NULL);
- hr = ::CLSIDFromProgID(L"AutoCAD.Application", &clsid);
- if (SUCCEEDED(hr))
- {
- if(::GetActiveObject(clsid, NULL, &pUnk) == S_OK)
- {
- VERIFY(pUnk->QueryInterface(IID_IDispatch, (LPVOID*)
- &pDisp) == S_OK);
- m_IApp.AttachDispatch(pDisp);
- pUnk->Release();
- }
- else
- VERIFY(m_IApp.CreateDispatch(clsid) == TRUE);
-
- m_IApp.SetVisible(TRUE);
- pDisp = m_IApp.GetActiveDocument();
- m_IDoc.AttachDispatch(pDisp);
- pDisp = m_IDoc.GetModelSpace();
- m_IMSpace.AttachDispatch(pDisp);
- }
- else
- AfxMessageBox("Acad.Application is not registered!");
- // remember the you need to call CoUninitialize(); somewhere
- // I would do it in the destructor of the dialog
- EndWaitCursor();
- }
- //
- // This function adds a popup menu to the exist row of popup menus
- // displayed in the menu bar.
- //
- // Note: The popup menus and menu items that we are adding
- // through Automation, are in memory only. They are not
- // saved in any menu files, no persistent in another word.
- //
- void CClientDlg::OnAddMenuButton()
- {
- // get all the menu groups
- LPDISPATCH pDisp = m_IApp.GetMenuGroups();
- m_IMenuGrps.AttachDispatch(pDisp);
- // get to the menu group that we're interested
- VARIANT var;
- var.vt = VT_I4;
- var.lVal = 0;
- pDisp = m_IMenuGrps.Item(var);
- m_IMenuGrp.AttachDispatch(pDisp);
- // get the popup menus in the menu group
- pDisp = m_IMenuGrp.GetMenus();
- m_IPopupMenus.AttachDispatch(pDisp);
- long cnt = m_IPopupMenus.GetCount();
- VARIANT vtIndex;
- vtIndex.vt = VT_I4;
- vtIndex.lVal = cnt + 1;
- // append a popup menu to the popup menus
- const char myPopupMenuName[] = "TestPopupMenu";
- pDisp = m_IPopupMenus.Add(myPopupMenuName);
- m_IPopupMenu.AttachDispatch(pDisp);
- m_IPopupMenus.InsertMenuInMenuBar(myPopupMenuName, vtIndex);
- // append a menu item to the popup menu we added
- // notice the last parameter of AddMenuItem is for
- // a command
- vtIndex.lVal = 0;
- pDisp = m_IPopupMenu.AddMenuItem(vtIndex, "TestItem", "Regen ");
- m_IPopupMenuItem.AttachDispatch(pDisp);
- }
- Removing menu bar and items are just as easy. You can call
- RemoveMenuFromMenuBar() to remove a popup menu, for example.[/FONT]
|
|