ContextMenu.h
上传用户:hy_wanghao
上传日期:2007-01-08
资源大小:279k
文件大小:8k
- #if !defined(AFX_CONTEXTMENU_H__20010107_3BB7_5346_1433_0080AD509054__INCLUDED_)
- #define AFX_CONTEXTMENU_H__20010107_3BB7_5346_1433_0080AD509054__INCLUDED_
- #pragma once
- #include "PropDlg.h"
- //////////////////////////////////////////////////////////////////////////////
- // CContextMenu
- // The IContextMenu implementation for folders in the
- // Explorer's Folder tree.
- //
- // COM-wise this is not an easy class to manage - mainly because it would
- // be much more efficient if we could reuse code in the CView class. But
- // we have no way to get to the view.
- // Because of this, we only support a limited number of menu commands.
- class ATL_NO_VTABLE CContextMenu :
- public CComObjectRootEx<CComSingleThreadModel>,
- public IContextMenu
- {
- public:
- DECLARE_NOT_AGGREGATABLE(CContextMenu)
- DECLARE_PROTECT_FINAL_CONSTRUCT()
- BEGIN_COM_MAP(CContextMenu)
- COM_INTERFACE_ENTRY_IID(IID_IContextMenu,IContextMenu)
- END_COM_MAP()
- // Attributes
- public:
- CFolder *m_pFolder;
- HWND m_hWnd;
- CPidl m_pidl;
- typedef enum {
- IDM_OPEN = 0,
- IDM_PROPERTIES,
- IDM_DELETE,
- IDM_LAST
- } MENUITEMS;
- // CDropTarget
- public:
- HRESULT FinalConstruct()
- {
- m_pFolder = NULL;
- return S_OK;
- }
- void FinalRelease()
- {
- if( m_pFolder!=NULL ) m_pFolder->Release();
- }
- HRESULT _Init(CFolder *pFolder, HWND hWnd, LPCITEMIDLIST pidl)
- {
- ATLASSERT(pFolder);
- m_pFolder = pFolder;
- m_pFolder->AddRef();
- m_pidl.Copy(pidl);
- m_hWnd = hWnd;
- return S_OK;
- }
- // IContextMenu
- public:
- STDMETHOD(QueryContextMenu)(HMENU hMenu,
- UINT iIndexMenu,
- UINT idCmdFirst,
- UINT idCmdLast,
- UINT /*uFlags*/)
- {
- ATLTRACE(_T("CContextMenu::QueryContextMenun"));
- ATLASSERT(idCmdLast>idCmdFirst+IDM_LAST);
- CResString<64> sText;
- idCmdLast;
-
- // Open
- sText.LoadString(IDS_OPEN);
- ::InsertMenu(hMenu, iIndexMenu++, MF_STRING | MF_BYPOSITION, idCmdFirst + IDM_OPEN, sText);
- // Delete
- sText.LoadString(IDS_DELETE);
- ::InsertMenu(hMenu, iIndexMenu++, MF_STRING | MF_BYPOSITION, idCmdFirst + IDM_DELETE, sText);
- // -
- ::InsertMenu(hMenu, iIndexMenu++, MF_SEPARATOR | MF_STRING | MF_BYPOSITION, 0, _T(""));
- // Properties
- sText.LoadString(IDS_PROPERTIES);
- ::InsertMenu(hMenu, iIndexMenu++, MF_STRING | MF_BYPOSITION, idCmdFirst + IDM_PROPERTIES, sText);
-
- ::SetMenuDefaultItem(hMenu, 0, TRUE);
- return MAKE_HRESULT(SEVERITY_SUCCESS, 0, IDM_LAST);
- }
- STDMETHOD(InvokeCommand)(LPCMINVOKECOMMANDINFO pcmi)
- {
- ATLTRACE(_T("CContextMenu::InvokeCommandn"));
- ATLASSERT(m_pFolder);
- ATLASSERT(m_pidl);
- USES_CONVERSION;
- // The command is sent via a verb
- if( HIWORD(pcmi->lpVerb) ) {
- LPCMINVOKECOMMANDINFOEX pcmix = NULL;
- if( pcmi->cbSize>=sizeof(CMINVOKECOMMANDINFOEX)-sizeof(POINT) ) pcmix = (LPCMINVOKECOMMANDINFOEX)pcmi;
- LPCSTR pstr = pcmi->lpVerb;
- // If it's an UNICODE string, convert it to ANSI now
- if( (pcmix!=NULL) &&
- ((pcmix->fMask & CMIC_MASK_UNICODE)!=0) &&
- (pcmix->lpVerbW!=NULL) ) {
- pstr = W2CA(pcmix->lpVerbW);
- }
- // Any known string?
- if( strcmp(pstr, "open")==0 ) pcmi->lpVerb = (LPCSTR)IDM_OPEN;
- else if( strcmp(pstr, "properties")==0 ) pcmi->lpVerb = (LPCSTR)IDM_PROPERTIES;
- else if( strcmp(pstr, "delete")==0 ) pcmi->lpVerb = (LPCSTR)IDM_DELETE;
- else return E_INVALIDARG;
- }
- // Check that it's a valid command
- if( LOWORD(pcmi->lpVerb)>IDM_LAST ) return E_INVALIDARG;
-
- // Process our command
- switch( LOWORD(pcmi->lpVerb) ) {
- case IDM_OPEN:
- {
- CPidl pidl;
- pidl.Copy( m_pFolder->m_pidl );
- pidl.Concatenate( m_pFolder->m_pidlPath );
- pidl.Concatenate( m_pidl );
- SHELLEXECUTEINFO sei = { 0 };
- sei.cbSize = sizeof(sei);
- sei.fMask = SEE_MASK_IDLIST | SEE_MASK_CLASSNAME;
- sei.lpIDList = pidl;
- sei.lpClass = TEXT("folder");
- sei.hwnd = pcmi->hwnd;
- sei.nShow = SW_SHOWNORMAL;
- sei.lpVerb = NULL; TEXT("explore");
- BOOL bRes = ::ShellExecuteEx(&sei);
- bRes = bRes;
- /*
- CPidl pidl;
- pidl.Copy( m_pFolder->m_pidl );
- pidl.Concatenate( m_pFolder->m_pidlPath );
- SHELLEXECUTEINFO sei = { 0 };
- sei.cbSize = sizeof(sei);
- sei.fMask = SEE_MASK_IDLIST;
- sei.lpIDList = pidl;
- sei.hwnd = pcmi->hwnd;
- sei.nShow = SW_SHOWNORMAL;
- sei.lpVerb = TEXT("explore");
- ::ShellExecuteEx(&sei);
- */
- /*
- CPidl pidl;
- pidl.Copy( m_pFolder->m_pidl );
- pidl.Concatenate( m_pFolder->m_pidlPath );
- m_pShellBrowser->BrowseObject(pidl, SBSP_DEFBROWSER | SBSP_ABSOLUTE);
- */
- }
- break;
- case IDM_PROPERTIES:
- {
- CPropertiesDlg dlg;
- HRESULT Hr;
- HR( dlg._Init(m_pFolder, m_pidl) );
- dlg.DoModal();
- m_pFolder->_SendViewMessage(m_hWnd, WM_COMMAND, ID_VIEW_REFRESH);
- }
- break;
- case IDM_DELETE:
- {
- if( m_pidl==NULL ) return E_INVALIDARG; // Not for ADF file itself
- // Confirm first, then delete
- LPCITEMIDLIST pidl = m_pidl;
- DWORD dwFlags = 0;
- if( m_pFolder->_ConfirmDelete(&pidl, 1, &dwFlags)==IDYES ) {
- HRESULT Hr;
- HR( m_pFolder->_DeleteFiles(&pidl, 1) );
- m_pFolder->_SendViewMessage(m_hWnd, WM_COMMAND, ID_VIEW_REFRESH);
- }
- }
- break;
- default:
- ::MessageBeep((UINT)-1);
- return E_INVALIDARG;
- }
- return S_OK;
- }
- STDMETHOD(GetCommandString)(UINT idCmd, UINT uFlags, LPUINT, LPSTR pszName, UINT cchMax)
- {
- ATLTRACE(_T("CContextMenu::GetCommandStringn"));
- HRESULT hr = E_NOTIMPL;
- switch( uFlags ) {
- case GCS_VERBA:
- case GCS_VERBW:
- {
- LPCSTR pstr;
- if( idCmd==IDM_OPEN ) pstr="open";
- else if( idCmd==IDM_DELETE ) pstr="delete";
- else if( idCmd==IDM_PROPERTIES ) pstr="properties";
- else return E_INVALIDARG;
- ATLASSERT(strlen(pstr)*sizeof(WCHAR)<=cchMax);
- USES_CONVERSION;
- if( uFlags & GCS_UNICODE ) wcscpy((LPWSTR)pszName, A2CW(pstr)); else strcpy(pszName, pstr);
- return NOERROR;
- }
- break;
- case GCS_HELPTEXTA:
- case GCS_HELPTEXTW:
- {
- UINT idRes = (UINT)-1;
- switch( idCmd ) {
- case IDM_OPEN: idRes = ID_FILE_OPEN; break;
- case IDM_DELETE: idRes = ID_EDIT_DELETE; break;
- case IDM_PROPERTIES: idRes = ID_EDIT_PROPERTIES; break;
- }
- if( idRes==-1 ) return E_FAIL;
- if( uFlags==GCS_HELPTEXTA ) {
- ::LoadStringA(_Module.GetResourceInstance(), idRes, pszName, cchMax);
- }
- else {
- // BUG: ::LoadStringW() fails on Win95
- ::LoadStringW(_Module.GetResourceInstance(), idRes, (LPWSTR)pszName, cchMax);
- }
- return NOERROR;
- }
- break;
- case GCS_VALIDATE:
- hr = NOERROR;
- break;
- }
- return hr;
- }
- };
- #endif // !defined(AFX_CONTEXTMENU_H__20010107_3BB7_5346_1433_0080AD509054__INCLUDED_)