void CMyTreeView::DoItemMenu(HTREEITEM hItem, CPoint& ptScreen) { TVITEM tvItem = {0}; tvItem.mask = TVIF_PARAM; tvItem.hItem = hItem; if (GetItem(tvItem)) { HRESULT hr; TreeItemData* pInfo = (TreeItemData*)tvItem.lParam; CContextMenu cm; CShellFolder sf; if (pInfo->GetParentFolder().GetIShellFolder() != NULL) sf = pInfo->GetParentFolder(); else sf.SHGetDesktopFolder(); if (sf.GetIShellFolder()) { hr = sf.GetUIObjectOf(m_hWnd, 1, &(pInfo->GetRelCpidl()), IID_IContextMenu, 0, cm); if (SUCCEEDED(hr)) { CMenu Popup; Popup.CreatePopupMenu(); if (Popup.GetHandle()) { hr = cm.QueryContextMenu(Popup, 0, 1, 0x7fff, CMF_NORMAL | CMF_EXPLORE); if (SUCCEEDED(hr)) { cm.QueryInterface(IID_IContextMenu2, m_ccm2); UINT idCmd = Popup.TrackPopupMenu(TPM_LEFTALIGN | TPM_RETURNCMD | TPM_RIGHTBUTTON , ptScreen.x, ptScreen.y, this, NULL); //A Treeview control sometimes requires this to end the // TrackPopupMenu properly GetParent()->PostMessage(WM_CANCELMODE, 0, 0); if (idCmd) { CMINVOKECOMMANDINFO cmi = {0}; cmi.cbSize = sizeof(CMINVOKECOMMANDINFO); cmi.hwnd = m_hWnd; cmi.lpVerb = (LPCSTR)(INT_PTR)(idCmd - 1); cmi.nShow = SW_SHOWNORMAL; cm.InvokeCommand(cmi); } m_ccm2.Release(); } } } } } }
IFACEMETHODIMP CClassFactory::CreateInstance(IUnknown *pUnkOuter, REFIID riid, void **ppvObject) { HRESULT hr = CLASS_E_NOAGGREGATION; if (pUnkOuter == NULL) { hr = E_OUTOFMEMORY; // Create the COM component. CContextMenu *pExt = new (std::nothrow) CContextMenu(); if (pExt) { // Query the specified interface. hr = pExt->QueryInterface(riid, ppvObject); pExt->Release(); } } return hr; }