BOOL CRecBinViewer::ExecCommand (LPCONTEXTMENU pCtxMenu, LPCTSTR lpszCommand) { UINT uiID = UINT (-1); UINT uiCommand = 0; UINT uiMenuFirst = 1; UINT uiMenuLast = 0x00007FFF; HMENU hmenuCtx; int iMenuPos = 0; int iMenuMax = 0; TCHAR szMenuItem[MAX_PATH]; TCHAR verb[MAX_PATH] ; hmenuCtx = CreatePopupMenu(); HRESULT hr = pCtxMenu->QueryContextMenu(hmenuCtx, 0, uiMenuFirst, uiMenuLast, CMF_NORMAL); iMenuMax = GetMenuItemCount(hmenuCtx); for (iMenuPos = 0 ; iMenuPos < iMenuMax; iMenuPos++) { GetMenuString(hmenuCtx, iMenuPos, szMenuItem, MAX_PATH, MF_BYPOSITION) ; uiID = GetMenuItemID(hmenuCtx, iMenuPos) ; if ((uiID == -1) || (uiID == 0)) { } else { hr = pCtxMenu->GetCommandString(uiID - 1, GCS_VERB, NULL, (LPSTR)verb, MAX_PATH); if (FAILED (hr)) { verb[0] = TCHAR ('\0') ; } else { if (0 == _tcsicmp (verb, lpszCommand)) uiCommand = uiID - 1; } } } if ((UINT)-1 != uiCommand) { CMINVOKECOMMANDINFO cmi; ZeroMemory(&cmi, sizeof(CMINVOKECOMMANDINFO)); cmi.cbSize = sizeof(CMINVOKECOMMANDINFO); cmi.fMask = CMIC_MASK_FLAG_NO_UI; cmi.hwnd = m_hWnd; cmi.lpVerb = (LPSTR)MAKEINTRESOURCE (uiCommand); cmi.nShow = SW_SHOWNORMAL; hr = pCtxMenu->InvokeCommand(&cmi); if (SUCCEEDED (hr)) return TRUE; } return false; }
void CShellContextMenu::InvokeCommand (LPCONTEXTMENU pContextMenu, UINT idCommand) { CMINVOKECOMMANDINFO cmi = {0}; cmi.cbSize = sizeof (CMINVOKECOMMANDINFO); cmi.lpVerb = (LPSTR) MAKEINTRESOURCE (idCommand); cmi.nShow = SW_SHOWNORMAL; pContextMenu->InvokeCommand (&cmi); }
// Shows standard Win95 Property Dlg for selected Item BOOL DirList_PropertyDlg(HWND hwnd, int iItem) { LV_ITEM lvi; LPLV_ITEMDATA lplvid; LPCONTEXTMENU lpcm; CMINVOKECOMMANDINFO cmi; BOOL bSuccess = TRUE; static const char* lpVerb = "properties"; if (iItem == -1) { if (ListView_GetSelectedCount(hwnd)) iItem = ListView_GetNextItem(hwnd, -1, LVNI_ALL | LVNI_SELECTED); else return FALSE; } lvi.mask = LVIF_PARAM; lvi.iItem = iItem; lvi.iSubItem = 0; if (!ListView_GetItem(hwnd, &lvi)) return FALSE; lplvid = (LPLV_ITEMDATA)lvi.lParam; if (NOERROR == lplvid->lpsf->GetUIObjectOf(GetParent(hwnd), // Owner 1, // Number of objects (LPCITEMIDLIST*)&lplvid->pidl, // pidl IID_IContextMenu, NULL, (void**)&lpcm)) { cmi.cbSize = sizeof(CMINVOKECOMMANDINFO); cmi.fMask = 0; cmi.hwnd = GetParent(hwnd); cmi.lpVerb = lpVerb; cmi.lpParameters = NULL; cmi.lpDirectory = NULL; cmi.nShow = SW_SHOWNORMAL; cmi.dwHotKey = 0; cmi.hIcon = NULL; if (NOERROR != lpcm->InvokeCommand(&cmi)) bSuccess = FALSE; lpcm->Release(); } else bSuccess = FALSE; return bSuccess; }
// Shows standard Win95 Property Dlg for selected Drive BOOL DriveBox_PropertyDlg(HWND hwnd) { COMBOBOXEXITEM cbei; LPDC_ITEMDATA lpdcid; int iItem; LPCONTEXTMENU lpcm; CMINVOKECOMMANDINFO cmi; BOOL bSuccess = TRUE; static const char* lpVerb = "properties"; iItem = (int)SendMessage(hwnd, CB_GETCURSEL, 0, 0); if (iItem == CB_ERR) return FALSE; cbei.mask = CBEIF_LPARAM; cbei.iItem = iItem; SendMessage(hwnd, CBEM_GETITEM, 0, (LPARAM)&cbei); lpdcid = (LPDC_ITEMDATA)cbei.lParam; if (NOERROR == lpdcid->lpsf->GetUIObjectOf(GetParent(hwnd), // Owner 1, // Number of objects (LPCITEMIDLIST*)&lpdcid->pidl, // pidl IID_IContextMenu, NULL, (void**)&lpcm)) { cmi.cbSize = sizeof(CMINVOKECOMMANDINFO); cmi.fMask = 0; cmi.hwnd = GetParent(hwnd); cmi.lpVerb = lpVerb; cmi.lpParameters = NULL; cmi.lpDirectory = NULL; cmi.nShow = SW_SHOWNORMAL; cmi.dwHotKey = 0; cmi.hIcon = NULL; if (NOERROR != lpcm->InvokeCommand(&cmi)) bSuccess = FALSE; lpcm->Release(); } else bSuccess = FALSE; return bSuccess; }
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ void ShellContext::Invoke(int nCmd) { HRESULT hr = S_OK; if (19 == nCmd) // rename { TCHAR oldName[256]; TCHAR newName[256]; WCHAR newNameW[256]; sh_get_displayname(psfFolder, pidlItem, SHGDN_NORMAL, oldName); if (IDOK == EditBox(_T("bbLean"), _T("Enter new name:"), oldName, newName, 256)) { #ifdef UNICODE wcsncpy_s(newNameW, 256, newName, _TRUNCATE); #else MultiByteToWideChar (CP_ACP, 0, newName, -1, newNameW, 256); #endif hr = psfFolder->SetNameOf(NULL, pidlItem, newNameW, SHGDN_NORMAL, NULL); } } else { CMINVOKECOMMANDINFO ici; ici.cbSize = sizeof(ici); ici.fMask = 0;//CMIC_MASK_FLAG_NO_UI; ici.hwnd = NULL; ici.lpVerb = (const CHAR*)(nCmd - MIN_SHELL_ID); ici.lpParameters = NULL; ici.lpDirectory = NULL; ici.nShow = SW_SHOWNORMAL; ici.dwHotKey = 0; ici.hIcon = NULL; hr = pContextMenu->InvokeCommand(&ici); } if (0==SUCCEEDED(hr)) { ;//MessageBeep(MB_OK); } }
BOOL showContextMenu (HWND hDlg, TCHAR* FName, WNDPROC menuProc) { TCHAR* FileName = PathFindFileName (FName); TCHAR FilePath [MAX_PATH]; lstrcpy (FilePath, FName); *PathFindFileName (FilePath) = L'\0'; LPSHELLFOLDER DesktopFolder; if (NOERROR != SHGetDesktopFolder (&DesktopFolder)) { return FALSE; } LPITEMIDLIST ParentPidl; ULONG Eaten; if (S_OK != DesktopFolder->ParseDisplayName (hDlg, 0, FilePath, &Eaten, &ParentPidl, 0)) { return FALSE; } LPSHELLFOLDER ParentFolder; if (S_OK != DesktopFolder->BindToObject (ParentPidl, 0, IID_IShellFolder, (void**)&ParentFolder)) { return FALSE; } LPITEMIDLIST Pidl; if (S_OK != ParentFolder->ParseDisplayName (hDlg, 0, FileName, &Eaten, &Pidl, 0)) { return FALSE; } LPCONTEXTMENU CM; if (S_OK != ParentFolder->GetUIObjectOf (hDlg, 1, (LPCITEMIDLIST*)&Pidl, IID_IContextMenu, 0, (void**)&CM)) { return FALSE; } HMENU hMenu = CreatePopupMenu (); if (hMenu == NULL) { return FALSE; } CM->QueryContextMenu (hMenu, 0, 1, 0x7FFF, CMF_EXTENDEDVERBS | CMF_EXPLORE); WNDPROC defWndProc = (WNDPROC) SetWindowLong (hDlg, GWL_WNDPROC, (LONG)menuProc); SetProp (hDlg, L"defWndProc", (HANDLE) defWndProc); POINT pt; GetCursorPos (&pt); int Cmd = TrackPopupMenu (hMenu, TPM_LEFTALIGN | TPM_LEFTBUTTON | TPM_RIGHTBUTTON | TPM_RETURNCMD, pt.x, pt.y, 0, hDlg, 0); SetWindowLong (hDlg, GWL_WNDPROC, (LONG) RemoveProp (hDlg, L"defWndProc")); if (Cmd) { // Set up a CMINVOKECOMMANDINFO structure. CMINVOKECOMMANDINFO CI; ZeroMemory (&CI, sizeof(CMINVOKECOMMANDINFO)); CI.cbSize = sizeof (CMINVOKECOMMANDINFO); CI.hwnd = hDlg; CI.lpVerb = (LPCSTR) MAKEINTRESOURCE(Cmd - 1); CI.lpParameters = ""; CI.lpDirectory = ""; CI.nShow = SW_SHOWNORMAL; CM->InvokeCommand (&CI); } return DestroyMenu (hMenu); }
/**************************************************************************** * * FUNCTION: DoTheMenuThing(HWND hwnd, * LPSHELLFOLDER lpsfParent, * LPITEMIDLIST lpi, * LPPOINT lppt) * * PURPOSE: Displays a popup context menu, given a parent shell folder, * relative item id and screen location. * * PARAMETERS: * hwnd - Parent window handle * lpsfParent - Pointer to parent shell folder. * lpi - Pointer to item id that is relative to lpsfParent * lppt - Screen location of where to popup the menu. * * RETURN VALUE: * Returns TRUE on success, FALSE on failure * ****************************************************************************/ BOOL CShellPidl::DoTheMenuThing(HWND hwnd, LPSHELLFOLDER lpsfParent, LPITEMIDLIST lpi, LPPOINT lppt) { LPCONTEXTMENU lpcm; HRESULT hr; char szTemp[64]; CMINVOKECOMMANDINFO cmi; DWORD dwAttribs=0; int idCmd; HMENU hMenu; BOOL bSuccess=TRUE; hr=lpsfParent->GetUIObjectOf(hwnd, 1, //Number of objects to get attributes of (const struct _ITEMIDLIST **)&lpi, IID_IContextMenu, 0, (LPVOID *)&lpcm); if (SUCCEEDED(hr)) { hMenu = CreatePopupMenu(); if (hMenu) { hr=lpcm->QueryContextMenu(hMenu, 0, 1, 0x7fff, CMF_EXPLORE); if (SUCCEEDED(hr)) { idCmd=TrackPopupMenu(hMenu, TPM_LEFTALIGN | TPM_RETURNCMD | TPM_RIGHTBUTTON, lppt->x, lppt->y, 0, hwnd, NULL); if (idCmd) { cmi.cbSize = sizeof(CMINVOKECOMMANDINFO); cmi.fMask = 0; cmi.hwnd = hwnd; cmi.lpVerb = MAKEINTRESOURCE(idCmd-1); cmi.lpParameters = NULL; cmi.lpDirectory = NULL; cmi.nShow = SW_SHOWNORMAL; cmi.dwHotKey = 0; cmi.hIcon = NULL; hr=lpcm->InvokeCommand(&cmi); if (!SUCCEEDED(hr)) { wsprintf(szTemp, "InvokeCommand failed. hr=%lx", hr); AfxMessageBox(szTemp); } } } else bSuccess = FALSE; DestroyMenu(hMenu); } else bSuccess = FALSE; lpcm->Release(); } else { wsprintf(szTemp, "GetUIObjectOf failed! hr=%lx", hr); AfxMessageBox(szTemp ); bSuccess = FALSE; } return bSuccess; }
LRESULT CSEShellView::OnNotify( LPNMHDR pNmhdr ) { TRACE_FUNCTION(); if( pNmhdr ) { if( pNmhdr->code == NM_DBLCLK ) { int iIndex = ListView_GetNextItem( m_hWnd, -1, LVNI_FOCUSED ); if (iIndex == -1) { return S_OK; } LV_ITEM item = {0}; item.iItem = iIndex; item.iSubItem = 0; item.mask = LVIF_PARAM; ListView_GetItem( m_hWnd, &item ); if ( item.lParam ) { LPITEMIDLIST pItem = m_pidlManager.Copy( ITEMLISTPTR(item.lParam) ); if( m_pidlManager.GetItemType( pItem ) == CPidlManager::FOLDER ) { LOG(_T("%s 打开文件夹 %s"), __TFUNCTION__, m_pidlManager.GetItemName( pItem ) ); //打开文件夹 m_pShellBrowser->BrowseObject( pItem , SBSP_DEFBROWSER | SBSP_RELATIVE ); } else { TCHAR szPath[MAX_PATH] = {0}; DWORD dwLen = MAX_PATH; LPITEMIDLIST pFullItem = m_pidlManager.Concatenate(m_pFolder->GetRootPath(), pItem ); m_pidlManager.GetFullName( pFullItem, szPath, &dwLen ); m_pidlManager.ReplaceRoot( szPath ); m_pidlManager.Delete( pFullItem ); //打开文件 LONG ret = (LONG)(LONG_PTR)ShellExecute(NULL, _T("open"), szPath, NULL, NULL, SW_SHOWNORMAL); if ( ret > 32 ) { return TRUE; } switch ( ret ) { case ERROR_BAD_FORMAT: { ::MessageBox( NULL, _T("文件已损坏"), _T("提示"), MB_OK ); } break; case SE_ERR_ASSOCINCOMPLETE: case SE_ERR_NOASSOC: { TCHAR szRunParam[MAX_PATH<<1] = {0}; _sntprintf( szRunParam, MAX_PATH<<1, _T("shell32.dll,OpenAs_RunDLL \"%s\""), szPath ); ShellExecute(NULL, NULL, _T("rundll32.exe "), szRunParam, NULL, SW_SHOW); } break; default: { } } } m_pidlManager.Delete( pItem ); pItem = NULL; } } else if( pNmhdr->code == NM_RCLICK ) { LPITEMIDLIST* ppIdl = NULL; int nSelCount = ListView_GetSelectedCount(m_hWnd); if ( nSelCount > 0 ) { ppIdl = (LPITEMIDLIST*)_Module.m_Allocator.Alloc( sizeof(LPITEMIDLIST)*nSelCount ); int iIndex = ListView_GetNextItem( m_hWnd, -1, LVIS_SELECTED ); LV_ITEM item = {0}; item.iSubItem = 0; item.mask = LVIF_PARAM; int _i = 0; while( iIndex >= 0 && _i < nSelCount ) { item.iItem = iIndex; ListView_GetItem( m_hWnd, &item ); ppIdl[_i] = m_pidlManager.Copy( ITEMLISTPTR(item.lParam) ); ++_i; iIndex = ListView_GetNextItem( m_hWnd, -1, LVIS_SELECTED ); } } LPCONTEXTMENU pMenu = NULL; HRESULT hr = E_FAIL; hr = m_pFolder->GetUIObjectOf( m_hWnd, nSelCount, \ (LPCITEMIDLIST*)ppIdl, IID_IContextMenu, NULL, (void**)&pMenu ); if ( FAILED(hr) || !pMenu) { LOG(_T("IID_IContextMenu Failed")); if ( nSelCount > 0 ) { for ( int _i = 0; _i < nSelCount; ++_i ) { m_pidlManager.Delete( ppIdl[_i] ); } _Module.m_Allocator.Free( ppIdl ); } return S_OK; } HMENU hMenu = ::CreatePopupMenu(); if ( !hMenu ) { if ( nSelCount > 0 ) { for ( int _i = 0; _i < nSelCount; ++_i ) { m_pidlManager.Delete( ppIdl[_i] ); } _Module.m_Allocator.Free( ppIdl ); } return S_OK; } hr = pMenu->QueryContextMenu( hMenu, 0, 10, 32767, CMF_NORMAL | CMF_EXPLORE ); if ( FAILED(hr) ) { if ( nSelCount > 0 ) { for ( int _i = 0; _i < nSelCount; ++_i ) { m_pidlManager.Delete( ppIdl[_i] ); } _Module.m_Allocator.Free( ppIdl ); } DestroyMenu( hMenu ); return hr; } POINT point = {0}; GetCursorPos( &point ); UINT command = (UINT)TrackPopupMenu ( hMenu, TPM_RETURNCMD | TPM_LEFTALIGN, point.x, point.y, 0, m_hWnd, NULL ); CMINVOKECOMMANDINFO cmi = {0}; cmi.cbSize = sizeof (CMINVOKECOMMANDINFO); cmi.lpVerb = (LPSTR) MAKEINTRESOURCE (command-10); cmi.nShow = SW_SHOWNORMAL; pMenu->InvokeCommand( &cmi ); pMenu->Release(); if ( nSelCount > 0 ) { for ( int _i = 0; _i < nSelCount; ++_i ) { m_pidlManager.Delete( ppIdl[_i] ); } _Module.m_Allocator.Free( ppIdl ); } DestroyMenu( hMenu ); } } TRACE_RETURN S_OK; }
BOOL COXShellNamespaceNavigator::InvokeCommand(const LPSHELLFOLDER lpParentFolder, const LPITEMIDLIST lpRelativeIDL, UINT nCmdID, DWORD dwFlags) const { // If any command from popup menu was selected then it should be // applied to corresponding shell object. // So we have to fill CMINVOKECOMMANDINFO structure CMINVOKECOMMANDINFO cmici={ sizeof(CMINVOKECOMMANDINFO) }; cmici.fMask=0; cmici.hwnd=m_pOwnerWnd!=NULL ? m_pOwnerWnd->GetSafeHwnd() : NULL; cmici.lpVerb=LPCSTR((UINT_PTR)nCmdID-1); cmici.lpParameters=NULL; cmici.lpDirectory=NULL; cmici.nShow=SW_SHOWNORMAL; cmici.dwHotKey=0; cmici.hIcon=NULL; // We can request IContextMenu interface using // IShellFolder::GetUIObjectOf method. // /* HRESULT GetUIObjectOf( HWND hwndOwner, UINT cidl, LPCITEMIDLIST *apidl, REFIID riid, UINT *prgfInOut, LPVOID *ppvOut ); Retrieves an OLE interface that can be used to carry out actions on the specified file objects or folders. Returns NOERROR if successful, E_NOINTERFACE if the interface is not supported, or an OLE-defined error value otherwise. hwndOwner - Handle to the owner window that the client should specify if it displays a dialog box or message box. cidl - Number of file objects or subfolders specified in the apidl parameter. apidl - Address of an array of pointers to ITEMIDLIST structures, each of which uniquely identifies a file object or subfolder relative to the parent folder. Each item identifier list must contain exactly one SHITEMID structure followed by a terminating zero. riid - Identifier of the COM interface object to return. This can be any valid interface identifier that can be created for an item. The most common identifiers used by the shell are listed in the comments at the end of this reference. prgfInOut - Reserved. ppvOut - Address that receives the interface pointer. If an error occurs, a NULL pointer is returned in this address. If cidl is greater than one, the GetUIObjectOf implementation should only succeed if it can create one object for all items specified in apidl. If the implementation cannot create one object for all items, this method should fail. The following are the most common interface identifiers the shell uses when requesting an interface from this method. The list also indicates if cidl can be greater than one for the requested interface. Interface Identifier Allowed cidl Value IContextMenu The cidl parameter can be greater than or equal to one. IContextMenu2 The cidl parameter can be greater than or equal to one. IDataObject The cidl parameter can be greater than or equal to one. IDropTarget The cidl parameter can only be one. IExtractIcon The cidl parameter can only be one. IQueryInfo The cidl parameter can only be one. */ LPCONTEXTMENU lpcm; HRESULT hResult=lpParentFolder-> GetUIObjectOf(m_pOwnerWnd!=NULL ? m_pOwnerWnd->GetSafeHwnd() : NULL, 1, (const struct _ITEMIDLIST**)&(lpRelativeIDL), IID_IContextMenu, 0, (LPVOID*)&lpcm); if(FAILED(hResult)) return FALSE; // In order to apply specified command we use // IContextMenu::InvokeCommand() method. /* HRESULT InvokeCommand( LPCMINVOKECOMMANDINFO lpici ); Carries out the command associated with a context menu item. Returns NOERROR if successful, or an OLE-defined error code otherwise. lpici - Address of a CMINVOKECOMMANDINFO structure containing information about the command. typedef struct _CMInvokeCommandInfo{ DWORD cbSize; DWORD fMask; HWND hwnd; LPCSTR lpVerb; LPCSTR lpParameters; LPCSTR lpDirectory; int nShow; DWORD dwHotKey; HANDLE hIcon; } CMINVOKECOMMANDINFO, *LPCMINVOKECOMMANDINFO; Contains information about a context menu command. cbSize - Contains the size of this structure, in bytes. fMask - Zero, or one or more of the following flags: CMIC_MASK_HOTKEY - The dwHotKey member is valid. CMIC_MASK_ICON - The hIcon member is valid. CMIC_MASK_FLAG_NO_UI- The system is prevented from displaying user interface elements (for example, error messages) while carrying out a command. hwnd - Handle of the window that is the owner of the context menu. An extension can also use this handle as the owner of any message boxes or dialog boxes it displays. lpVerb - A 32-bit value that contains zero in the high-order word and a menu-identifier offset of the command to carry out in the low-order word. The shell specifies this value (using the MAKEINTRESOURCE macro) when the user chooses a menu command. If the high-order word is not zero, this member is the address of a null-terminated string specifying the language-independent name of the command to carry out. This member is typically a string when a command is being activated by an application. The system provides predefined constant values for the following command strings. Value String CMDSTR_NEWFOLDER "NewFolder" CMDSTR_VIEWDETAILS "ViewDetails" CMDSTR_VIEWLIST "ViewList" lpParameters - Optional parameters. This member is always NULL for menu items inserted by a shell extension. lpDirectory - Optional working directory name. This member is always NULL for menu items inserted by a shell extension. nShow - Set of SW_ values to pass to the ShowWindow function if the command displays a window or starts an application. dwHotKey - Optional hot key to assign to any application activated by the command. If the fMask parameter does not specify CMIC_MASK_HOTKEY, this member is ignored. hIcon - Icon to use for any application activated by the command. If the fMask member does not specify CMIC_MASK_ICON, this member is ignored. The shell calls this method when the user chooses a command that the handler added to a context menu. This method may also be called by an application without any corresponding user action. */ HMENU hMenu=::CreatePopupMenu(); if(hMenu==NULL) { TRACE(_T("COXShellNamespaceNavigator::GetObjectContextMenu: CreatePopupMenu() failed\n")); return NULL; } hResult=lpcm->QueryContextMenu(hMenu,0,1,0x7fff,dwFlags); if(FAILED(hResult)) { TRACE(_T("COXShellNamespaceNavigator::GetObjectContextMenu: QueryContextMenu() failed\n")); ::DestroyMenu(hMenu); return NULL; } hResult=lpcm->InvokeCommand(&cmici); ::DestroyMenu(hMenu); lpcm->Release(); if(FAILED(hResult)) { TRACE(_T("COXShellNamespaceNavigator::InvokeCommand: InvokeCommand() failed\n")); return FALSE; } return TRUE; }