DropTargetImpl.h
上传用户:hy_wanghao
上传日期:2007-01-08
资源大小:279k
文件大小:4k
源码类别:

Shell编程

开发平台:

Visual C++

  1. #if !defined(AFX_DROPTARGETIMPL_H__20010406_CE55_D769_D3CA_0080AD509054__INCLUDED_)
  2. #define AFX_DROPTARGETIMPL_H__20010406_CE55_D769_D3CA_0080AD509054__INCLUDED_
  3. #pragma once
  4. //////////////////////////////////////////////////////////////////////////////
  5. // IDropTargetImpl
  6. class ATL_NO_VTABLE IDropTargetImpl : public IDropTarget
  7. {
  8. public:
  9.    IDropTargetImpl() : m_pFolder(NULL), m_hwndTarget(NULL), m_fAcceptFmt(false)
  10.    { 
  11.    }
  12.    ~IDropTargetImpl()
  13.    {
  14.       if( m_pFolder!=NULL ) m_pFolder->Release();
  15.    }
  16.    HRESULT _Init(CFolder *pFolder, HWND hWnd)
  17.    {
  18.       ATLTRACE(_T("CDropTarget::_Initn"));
  19.       ATLASSERT(pFolder);
  20.       m_pFolder = pFolder;
  21.       m_pFolder->AddRef();
  22.       m_hwndTarget = hWnd;
  23.       return S_OK;
  24.    }
  25. // Attributes
  26. public:
  27.    CFolder *m_pFolder;
  28.    HWND m_hwndTarget;
  29.    bool m_fAcceptFmt;
  30. // IDropTarget
  31. public:
  32.    STDMETHOD(DragEnter)(LPDATAOBJECT pDataObj, 
  33.                         DWORD dwKeyState, 
  34.                         POINTL, 
  35.                         LPDWORD pdwEffect)
  36.    {
  37.       ATLTRACE(_T("CDropTarget::DragEntern"));
  38.       ATLASSERT(pDataObj);
  39.       // Does the drag source provide our CF types?
  40.       m_fAcceptFmt = false;
  41.       if( !m_fAcceptFmt ) {
  42.          FORMATETC fe = { CF_HDROP, NULL, DVASPECT_CONTENT, -1, TYMED_HGLOBAL };
  43.          m_fAcceptFmt = (S_OK == pDataObj->QueryGetData(&fe));
  44.       }
  45.       if( !m_fAcceptFmt ) {
  46.          FORMATETC fe = { _Module.m_CFSTR_FILEDESCRIPTOR, NULL, DVASPECT_CONTENT, -1, TYMED_HGLOBAL };
  47.          m_fAcceptFmt = (S_OK == pDataObj->QueryGetData(&fe));
  48.       }
  49.       *pdwEffect = _QueryDrop(dwKeyState, pdwEffect);
  50.       return S_OK;
  51.    }
  52.    STDMETHOD(DragOver)(DWORD dwKeyState, POINTL /*pt*/, LPDWORD pdwEffect)
  53.    {
  54.       *pdwEffect = _QueryDrop(dwKeyState, pdwEffect);
  55.       return S_OK;
  56.    }
  57.    
  58.    STDMETHOD(DragLeave)(VOID)
  59.    {
  60.       ATLTRACE(_T("CDropTarget::DragLeaven"));
  61.       m_fAcceptFmt = false;
  62.       return S_OK;
  63.    }
  64.    STDMETHOD(Drop)(LPDATAOBJECT pDataObj,
  65.                    DWORD dwKeyState,
  66.                    POINTL /*pt*/,
  67.                    LPDWORD pdwEffect)
  68.    {
  69.       ATLTRACE(_T("CDropTarget::Dropn"));
  70.       ATLASSERT(pDataObj);
  71.       ATLASSERT(pdwEffect);
  72.       ATLASSERT(m_pFolder);
  73.       // Determine drop effect...
  74.       DWORD dwDropEffect = _QueryDrop(dwKeyState, pdwEffect);
  75.       *pdwEffect = DROPEFFECT_NONE; // default to failed/cancelled
  76.       // Did we accept this drop effect?
  77.       if( dwDropEffect==DROPEFFECT_NONE ) return S_OK;
  78.       // Drop data...
  79.       HRESULT Hr;
  80.       if( SUCCEEDED(Hr = m_pFolder->_DoDrop(pDataObj, dwDropEffect) ) ) {
  81.          // Our DoDrop() method returns S_FALSE if something was cancelled.
  82.          // In this case, we let the droptype fall back to DROPEFFECT_COPY
  83.          // so nothing gets deleted.
  84.          *pdwEffect = ( Hr==S_OK ? dwDropEffect : DROPEFFECT_COPY );
  85.       }
  86.       m_pFolder->_SendViewMessage(m_hwndTarget, WM_COMMAND, ID_VIEW_REFRESH);
  87.       return S_OK;
  88.    }
  89. // Implementation
  90. private:
  91.    DWORD _QueryDrop(DWORD dwKeyState, LPDWORD pdwEffect)
  92.    {
  93.       ATLASSERT(pdwEffect);
  94.       if( !m_fAcceptFmt ) return DROPEFFECT_NONE;
  95.       DWORD dwMask = _GetDropEffectFromKeyState(dwKeyState);
  96.       if( (*pdwEffect) & dwMask ) return (*pdwEffect) & dwMask;
  97.       // Map common alternatives
  98.       if( (*pdwEffect) & DROPEFFECT_COPY ) return DROPEFFECT_COPY;
  99.       if( (*pdwEffect) & DROPEFFECT_MOVE ) return DROPEFFECT_MOVE;
  100.       return DROPEFFECT_NONE;
  101.    }
  102.    DWORD _GetDropEffectFromKeyState(DWORD dwKeyState)
  103.    {
  104.       // "Move" is the default.
  105.       // We don't support DROPEFFECT_LINK operations.
  106.       DWORD dwDropEffect = DROPEFFECT_MOVE;
  107.       if( dwKeyState & MK_CONTROL ) dwDropEffect = DROPEFFECT_COPY;
  108.       return dwDropEffect;
  109.    }
  110. };
  111. #endif // !defined(AFX_DROPTARGETIMPL_H__20010406_CE55_D769_D3CA_0080AD509054__INCLUDED_)