DropTargetImpl.h
上传用户:hy_wanghao
上传日期:2007-01-08
资源大小:279k
文件大小:4k
- #if !defined(AFX_DROPTARGETIMPL_H__20010406_CE55_D769_D3CA_0080AD509054__INCLUDED_)
- #define AFX_DROPTARGETIMPL_H__20010406_CE55_D769_D3CA_0080AD509054__INCLUDED_
- #pragma once
- //////////////////////////////////////////////////////////////////////////////
- // IDropTargetImpl
- class ATL_NO_VTABLE IDropTargetImpl : public IDropTarget
- {
- public:
- IDropTargetImpl() : m_pFolder(NULL), m_hwndTarget(NULL), m_fAcceptFmt(false)
- {
- }
- ~IDropTargetImpl()
- {
- if( m_pFolder!=NULL ) m_pFolder->Release();
- }
- HRESULT _Init(CFolder *pFolder, HWND hWnd)
- {
- ATLTRACE(_T("CDropTarget::_Initn"));
- ATLASSERT(pFolder);
- m_pFolder = pFolder;
- m_pFolder->AddRef();
- m_hwndTarget = hWnd;
- return S_OK;
- }
- // Attributes
- public:
- CFolder *m_pFolder;
- HWND m_hwndTarget;
- bool m_fAcceptFmt;
- // IDropTarget
- public:
- STDMETHOD(DragEnter)(LPDATAOBJECT pDataObj,
- DWORD dwKeyState,
- POINTL,
- LPDWORD pdwEffect)
- {
- ATLTRACE(_T("CDropTarget::DragEntern"));
- ATLASSERT(pDataObj);
- // Does the drag source provide our CF types?
- m_fAcceptFmt = false;
- if( !m_fAcceptFmt ) {
- FORMATETC fe = { CF_HDROP, NULL, DVASPECT_CONTENT, -1, TYMED_HGLOBAL };
- m_fAcceptFmt = (S_OK == pDataObj->QueryGetData(&fe));
- }
- if( !m_fAcceptFmt ) {
- FORMATETC fe = { _Module.m_CFSTR_FILEDESCRIPTOR, NULL, DVASPECT_CONTENT, -1, TYMED_HGLOBAL };
- m_fAcceptFmt = (S_OK == pDataObj->QueryGetData(&fe));
- }
- *pdwEffect = _QueryDrop(dwKeyState, pdwEffect);
- return S_OK;
- }
- STDMETHOD(DragOver)(DWORD dwKeyState, POINTL /*pt*/, LPDWORD pdwEffect)
- {
- *pdwEffect = _QueryDrop(dwKeyState, pdwEffect);
- return S_OK;
- }
-
- STDMETHOD(DragLeave)(VOID)
- {
- ATLTRACE(_T("CDropTarget::DragLeaven"));
- m_fAcceptFmt = false;
- return S_OK;
- }
- STDMETHOD(Drop)(LPDATAOBJECT pDataObj,
- DWORD dwKeyState,
- POINTL /*pt*/,
- LPDWORD pdwEffect)
- {
- ATLTRACE(_T("CDropTarget::Dropn"));
- ATLASSERT(pDataObj);
- ATLASSERT(pdwEffect);
- ATLASSERT(m_pFolder);
- // Determine drop effect...
- DWORD dwDropEffect = _QueryDrop(dwKeyState, pdwEffect);
- *pdwEffect = DROPEFFECT_NONE; // default to failed/cancelled
- // Did we accept this drop effect?
- if( dwDropEffect==DROPEFFECT_NONE ) return S_OK;
- // Drop data...
- HRESULT Hr;
- if( SUCCEEDED(Hr = m_pFolder->_DoDrop(pDataObj, dwDropEffect) ) ) {
- // Our DoDrop() method returns S_FALSE if something was cancelled.
- // In this case, we let the droptype fall back to DROPEFFECT_COPY
- // so nothing gets deleted.
- *pdwEffect = ( Hr==S_OK ? dwDropEffect : DROPEFFECT_COPY );
- }
- m_pFolder->_SendViewMessage(m_hwndTarget, WM_COMMAND, ID_VIEW_REFRESH);
- return S_OK;
- }
- // Implementation
- private:
- DWORD _QueryDrop(DWORD dwKeyState, LPDWORD pdwEffect)
- {
- ATLASSERT(pdwEffect);
- if( !m_fAcceptFmt ) return DROPEFFECT_NONE;
- DWORD dwMask = _GetDropEffectFromKeyState(dwKeyState);
- if( (*pdwEffect) & dwMask ) return (*pdwEffect) & dwMask;
- // Map common alternatives
- if( (*pdwEffect) & DROPEFFECT_COPY ) return DROPEFFECT_COPY;
- if( (*pdwEffect) & DROPEFFECT_MOVE ) return DROPEFFECT_MOVE;
- return DROPEFFECT_NONE;
- }
- DWORD _GetDropEffectFromKeyState(DWORD dwKeyState)
- {
- // "Move" is the default.
- // We don't support DROPEFFECT_LINK operations.
- DWORD dwDropEffect = DROPEFFECT_MOVE;
- if( dwKeyState & MK_CONTROL ) dwDropEffect = DROPEFFECT_COPY;
- return dwDropEffect;
- }
- };
- #endif // !defined(AFX_DROPTARGETIMPL_H__20010406_CE55_D769_D3CA_0080AD509054__INCLUDED_)