📄 droptarget.cpp
字号:
#include "DropTarget.h"
extern UINT g_uiRefThisDll;
CDropTarget::CDropTarget(CShellFolder *psfParent)
{
g_uiRefThisDll++;
m_uiRefCount = 1;
m_psfParent = psfParent;
if(m_psfParent)
{
m_psfParent->AddRef();
}
m_pPidlMgr = new CPidlMgr();
SHGetMalloc(&m_pMalloc);
m_bAcceptFmt = FALSE;
// TODO : Register your own clipboard format here
m_cfPrivateData = CF_HDROP;
}
CDropTarget::~CDropTarget()
{
if(m_psfParent)
{
m_psfParent->Release();
}
if (m_pPidlMgr)
{
delete m_pPidlMgr;
}
if (m_pMalloc)
{
m_pMalloc->Release();
}
g_uiRefThisDll--;
}
///////////////////////////////////////////////////////////
// IUnknown Implementation
//
STDMETHODIMP CDropTarget::QueryInterface(REFIID riid, LPVOID *ppReturn)
{
*ppReturn = NULL;
//IUnknown
if(IsEqualIID(riid, IID_IUnknown))
{
*ppReturn = this;
}
//IDropTarget
else if(IsEqualIID(riid, IID_IDropTarget))
{
*ppReturn = (IDropTarget*)this;
}
if(*ppReturn)
{
(*(LPUNKNOWN*)ppReturn)->AddRef();
return S_OK;
}
return E_NOINTERFACE;
}
STDMETHODIMP_(DWORD) CDropTarget::AddRef(VOID)
{
return ++m_uiRefCount;
}
STDMETHODIMP_(DWORD) CDropTarget::Release(VOID)
{
if(--m_uiRefCount == 0)
{
delete this;
return 0;
}
return m_uiRefCount;
}
///////////////////////////////////////////////////////////
// IDropTarget implemenatation
//
STDMETHODIMP CDropTarget::DragEnter( LPDATAOBJECT pDataObj,
DWORD dwKeyState,
POINTL pt,
LPDWORD pdwEffect)
{
// TODO : Handle DragEnter here
FORMATETC fmtetc;
fmtetc.cfFormat = m_cfPrivateData;
fmtetc.ptd = NULL;
fmtetc.dwAspect = DVASPECT_CONTENT;
fmtetc.lindex = -1;
fmtetc.tymed = TYMED_HGLOBAL;
// QueryGetData for pDataObject for our format
m_bAcceptFmt = (S_OK == pDataObj->QueryGetData(&fmtetc)) ? TRUE : FALSE;
queryDrop(dwKeyState, pdwEffect);
return m_bAcceptFmt ? S_OK : E_FAIL;
}
STDMETHODIMP CDropTarget::DragOver(DWORD dwKeyState, POINTL pt, LPDWORD pdwEffect)
{
BOOL bRet = queryDrop(dwKeyState, pdwEffect);
return bRet ? S_OK : E_FAIL;
}
STDMETHODIMP CDropTarget::DragLeave(VOID)
{
m_bAcceptFmt = FALSE;
return S_OK;
}
STDMETHODIMP CDropTarget::Drop(LPDATAOBJECT pDataObj,
DWORD dwKeyState,
POINTL pt,
LPDWORD pdwEffect)
{
HRESULT hr = E_FAIL;
if (queryDrop(dwKeyState, pdwEffect))
{
FORMATETC fe;
STGMEDIUM stgmed;
fe.cfFormat = m_cfPrivateData;
fe.ptd = NULL;
fe.dwAspect = DVASPECT_CONTENT;
fe.lindex = -1;
fe.tymed = TYMED_HGLOBAL;
// Get the storage medium from the data object.
hr = pDataObj->GetData(&fe, &stgmed);
if (SUCCEEDED(hr))
{
BOOL bRet = doDrop(stgmed.hGlobal, DROPEFFECT_MOVE == *pdwEffect);
//release the STGMEDIUM
ReleaseStgMedium(&stgmed);
return bRet ? S_OK : E_FAIL;
}
}
*pdwEffect = DROPEFFECT_NONE;
return hr;
}
BOOL CDropTarget::queryDrop(DWORD dwKeyState, LPDWORD pdwEffect)
{
DWORD dwOKEffects = *pdwEffect;
*pdwEffect = DROPEFFECT_NONE;
if (m_bAcceptFmt)
{
*pdwEffect = getDropEffectFromKeyState(dwKeyState);
if(DROPEFFECT_LINK == *pdwEffect)
{
*pdwEffect = DROPEFFECT_NONE;
}
if(*pdwEffect & dwOKEffects)
{
return TRUE;
}
}
return FALSE;
}
// TODO : Modify to suit your needs
DWORD CDropTarget::getDropEffectFromKeyState(DWORD dwKeyState)
{
DWORD dwDropEffect = DROPEFFECT_MOVE;
if(dwKeyState & MK_CONTROL)
{
if(dwKeyState & MK_SHIFT)
{
dwDropEffect = DROPEFFECT_LINK;
}
else
{
dwDropEffect = DROPEFFECT_COPY;
}
}
return dwDropEffect;
}
// TODO : Modify to suit your needs
BOOL CDropTarget::doDrop(HGLOBAL hMem, BOOL bCut)
{
BOOL fSuccess = FALSE;
if(hMem)
{
// We support CF_HDROP and hence the global mem object
// contains a DROPFILES structure
LPDROPFILES pDropFiles = (LPDROPFILES)GlobalLock(hMem);
if (pDropFiles)
{
if (pDropFiles->fWide == FALSE)
{
// LPSTR pszFiles = (LPSTR)(pDropFiles+pDropFiles->pFiles);
LPSTR pszTotalFiles = (LPSTR) malloc(1);
pszTotalFiles[0] = 0;
DWORD dwLen = pDropFiles->pFiles;
LPSTR pszFiles = (LPSTR) pDropFiles;
while (dwLen)
{
pszFiles++;
dwLen--;
}
UINT uiTotFileSize = 0;
while (*pszFiles)
{
uiTotFileSize += strlen(pszFiles) + 2;
pszTotalFiles = (LPSTR) realloc(pszTotalFiles, uiTotFileSize);
strcat(pszTotalFiles, pszFiles);
strcat(pszTotalFiles, "\n");
size_t size = strlen(pszFiles);
pszFiles += size + 1;
}
if (bCut)
{
::MessageBox(NULL, pszTotalFiles, "Dragged with MOVE", MB_OK);
}
else
{
::MessageBox(NULL, pszTotalFiles, "Dragged with COPY", MB_OK);
}
free(pszTotalFiles);
}
else
{
LPSTR pszTotalFiles = (LPSTR) malloc(1);
pszTotalFiles[0] = 0;
LPWSTR pswFiles = (LPWSTR) ((BYTE*) pDropFiles + pDropFiles->pFiles);
UINT uiTotFileSize = 0;
while (*pswFiles)
{
TCHAR szFiles[MAX_PATH];
WideCharToMultiByte(CP_ACP,
0,
pswFiles,
-1,
szFiles,
MAX_PATH,
NULL,
NULL);
uiTotFileSize += strlen(szFiles) + 2;
pszTotalFiles = (LPSTR) realloc(pszTotalFiles, uiTotFileSize);
strcat(pszTotalFiles, szFiles);
strcat(pszTotalFiles, "\n");
size_t size = wcslen(pswFiles);
pswFiles += size + 1;
}
if (bCut)
{
::MessageBox(NULL, pszTotalFiles, "Dragged with MOVE", MB_OK);
}
else
{
::MessageBox(NULL, pszTotalFiles, "Dragged with COPY", MB_OK);
}
free(pszTotalFiles);
}
GlobalUnlock(hMem);
}
}
return fSuccess;
}
// EOF
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -