|
//
// (C) Copyright by Victor Derks <vba64@xs4all.nl>
//
// See README.TXT for the details of the software licence.
//
#pragma once
#include <atlctl.h>
#include <vector>
#include <shlobj.h>
#include "stgmedium.h"
#include "catchhandler.h"
namespace MSF
{
class CFileList :
public CComObjectRootEx<CComSingleThreadModel>,
public ATL::IDataObjectImpl<CFileList>
{
public:
typedef std::vector<CString> CStrings;
DECLARE_NOT_AGGREGATABLE(CFileList)
BEGIN_COM_MAP(CFileList)
COM_INTERFACE_ENTRY(IDataObject)
END_COM_MAP()
static CComPtr<CFileList> CreateInstance()
{
CComObject<CFileList>* pinstance;
RaiseExceptionIfFailed(
CComObject<CFileList>::CreateInstance(&pinstance));
CComPtr<CFileList> filelist(pinstance);
return filelist;
}
virtual HRESULT IDataObject_GetData(FORMATETC* pformatetc, STGMEDIUM* pmedium)
{
try
{
ValidateFormatEtc(pformatetc);
pmedium->tymed = TYMED_HGLOBAL;
pmedium->pUnkForRelease = NULL;
pmedium->hGlobal = CreateData();
return S_OK;
}
MSF_COM_CATCH_HANDLER()
}
void Add(const TCHAR* tszFilename)
{
m_filenames.push_back(tszFilename);
}
public:
LPDATAADVISEHOLDER m_spDataAdviseHolder;
private:
void ValidateFormatEtc(const FORMATETC* pformatetc) const
{
RaiseExceptionIf(pformatetc->cfFormat != CF_HDROP, DV_E_FORMATETC);
RaiseExceptionIf(pformatetc->dwAspect != DVASPECT_CONTENT, DV_E_DVASPECT);
RaiseExceptionIf(pformatetc->lindex != -1, DV_E_LINDEX);
RaiseExceptionIf(pformatetc->tymed != TYMED_HGLOBAL, DV_E_TYMED);
}
HGLOBAL CreateData() const
{
HGLOBAL hg = GlobalAllocThrow(GetDataSize());
DROPFILES* pdropfiles = static_cast<DROPFILES*>(hg);
pdropfiles->pFiles = sizeof(DROPFILES);
#ifdef _UNICODE
pdropfiles->fWide = TRUE;
#else
pdropfiles->fWide = FALSE;
#endif
// copy filenames into data block.
TCHAR* pszBuf =
reinterpret_cast<TCHAR*>(reinterpret_cast<BYTE*>(pdropfiles) + sizeof(DROPFILES));
for (CStrings::const_iterator it = m_filenames.begin(); it != m_filenames.end(); ++it)
{
ATLVERIFY(lstrcpy(pszBuf, it->GetString()) != NULL);
pszBuf += (it->GetLength() + 1);
}
*pszBuf = _T('\0');
return hg;
}
size_t GetDataSize() const
{
UINT nchars = 0;
for (CStrings::const_iterator it = m_filenames.begin(); it != m_filenames.end(); ++it)
{
nchars += it->GetLength() + 1;
}
return sizeof(DROPFILES) + (sizeof(TCHAR) * (nchars + 1));
}
CStrings m_filenames;
};
} // end MSF namespace
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.
A list of licenses authors might use can be found here
Victor lives in Nijmegen, the oldest city in The Netherlands.
He studied Applied Physics in Delft and works Hitachi High-Tech Analytical Science.