윈도우 explorer의 contextmenu에 사용자 메뉴항목을 추가하는 방법

윈도우 explorer의 contextmenu에 사용자 메뉴항목을 추가하는 방법.


1. ATL COM AppWizard를 이용 프로젝트를 생성한다.

2. New ATL Object로 Simple Object를 추가한다.

3. 추가한 Simple Object의 헤더 파일을 보면 아래와 비슷한 항목들이 있는데 적당히 추가한다.

 

#include "IContextMenuImpl.h"  //추가선언

class ATL_NO_VTABLE 클래스 이름:

 ......

 public IShellExtInit,
 public IContextMenuImpl,

 ......


//IContextMenu
STDMETHOD(GetCommandString)(UINT, UINT, UINT*, LPTSTR, UINT);
STDMETHOD(InvokeCommand)(LPCMINVOKECOMMANDINFO);
STDMETHOD(QueryContextMenu)(HMENU, UINT, UINT, UINT, UINT);
//IShellExtInit
STDMETHOD(Initialize)(LPCITEMIDLIST, LPDATAOBJECT, HKEY);

 

 ......

 

BEGIN_COM_MAP(CExeMenu)
 
 ......

 

COM_INTERFACE_ENTRY(IShellExtInit)
COM_INTERFACE_ENTRY(IContextMenu)

END_COM_MAP()
  

 

 

4. 이번엔 cpp파일에 추가할 내용이다.

//이 shell extension은 contextmenu에 list를 추가하기 위한 것 이다.
HRESULT 클래스이름::QueryContextMenu(HMENU hmenu, UINT indexMenu, UINT idComdFirst, UINT idComdLast, UINT uFlags)
{
      UINT idCmd = idComdFirst;
 
      //context menu insert 자세한 내용은 MSDN참조.
      InsertMenu(hmenu, indexMenu, MF_STRING | MF_BYPOSITION, idCmd++, __TEXT("메뉴에 표시될 Text"));
      m_FileState = TRUE;
 
      return MAKE_HRESULT(SEVERITY_SUCCESS, FACILITY_NULL, idCmd - idComdFirst);
}

 

 

//Initialize
HRESULT 클래스이름::Initialize(LPCITEMIDLIST pidlFolder, LPDATAOBJECT lpdobj, HKEY hKeyProgID)
{
      if(lpdobj == NULL)
            return E_INVALIDARG;

      STGMEDIUM medium;
      //CD_HDROP형으로 된 데이터를 구한다.
      FORMATETC fe = {CF_HDROP, NULL, DVASPECT_CONTENT, -1, TYMED_HGLOBAL};
      HRESULT hr = lpdobj->GetData(&fe, &medium);

      if(FAILED(hr))
            return E_INVALIDARG;

      //선택된 파일이름을 구한다.
      DragQueryFile(reinterpret_cast<HDROP>(medium.hGlobal), 0, m_szFile, MAX_PATH);

      ReleaseStgMedium(&medium);

return hr;
}

 

 

//contextmenu에 추가한 항목에 관한 상태바에 설명을 표시하는 부분이다.
HRESULT CExeMenu::GetCommandString (UINT idCmd, UINT uFlags, UINT* pwReserved, LPSTR pszName, UINT cchMax)
{
// If Explorer is asking for a help string, copy our string into the
// supplied buffer.
if ( uFlags & GCS_HELPTEXT )
lstrcpyn(pszText, _T("This is the simple shell extension's help"), ccgMax);

return S_OK;
}
 

 //menu가 선택되었을시 작업하는 영역이다.
HRESULT CExeMenu::InvokeCommand ( LPCMINVOKECOMMANDINFO pCmdInfo )
{
// If lpVerb really points to a string, ignore this function call and bail out.
if ( 0 != HIWORD( pCmdInfo->lpVerb ))
return E_INVALIDARG;

 

....처리할 내용 ....

 

// ex) dlg.DoModal() 을 하여 다이얼로그를 생성 이후 작업은 다이얼로그에서 처리가 가능하다.

 

return S_OK;
}

 

 

참고>codeproject.com의 예제 프로그램이다. 참고하여 같이보자~~ ^,.^

http://www.codeproject.com/shell/shellextguide2.asp

위로 스크롤