Home > Back-end >  On past Form outside drag and drop files to the problem of resource manager
On past Form outside drag and drop files to the problem of resource manager

Time:10-02

I'm doing a small example, drag and drop probably described as follows:

A Form has A Label, the Label of the Caption to A local file (file A) the full path, drag the Label to the resource manager E dish to loosen, the file will be copied to A disc E, I realized the IDataObject and IDropSource interface, don't know if implementation problems, always display ban drop when drag and drop ICONS,

complete code can be downloaded here:
http://pan.baidu.com/s/1eQcUbPS
The code is as follows:
MyDataObject. H
 
#include
# include "IDragDemo. H"

The class MyDataObject: public IDataObject
{
Public:
//IUnknown implementation
ULONG __stdcall AddRef ();
ULONG __stdcall Release ();
STDMETHODIMP QueryInterface (REFIID riid, void * * ppvObject);

//IDataObject members
STDMETHODIMP GetData (FORMATETC * pformatetcIn, STGMEDIUM * pmedium);
STDMETHODIMP GetDataHere (FORMATETC * pformatetc, STGMEDIUM * pmedium);
STDMETHODIMP QueryGetData (FORMATETC * pformatetc);
STDMETHODIMP GetCanonicalFormatEtc (FORMATETC * pformatectIn, FORMATETC * pformatetcOut);
STDMETHODIMP SetData (FORMATETC * pformatetc, STGMEDIUM * pmedium, BOOL fRelease);
STDMETHODIMP EnumFormatEtc (DWORD dwDirection IEnumFORMATETC * * ppenumFormatEtc);
STDMETHODIMP DAdvise (FORMATETC * pformatetc, dwords advf IAdviseSink * pAdvSink, dwords * pdwConnection);
STDMETHODIMP DUnadvise (DWORD dwConnection);
* * ppenumAdvise STDMETHODIMP EnumDAdvise (IEnumSTATDATA);
Public:
MyDataObject vFileName (char *);
~ MyDataObject ();
Private:
LONG refcount;

//custom formatting
FORMATETC m_AcceptFormat;
STGMEDIUM m_StorageMedium;

HGLOBAL DupGlobalMem (HGLOBAL hMem);
};


MyDataObject. CPP
 
# include "MyDataObject. H"
# include "MyDropSource. H"
#include

//Constructors
MyDataObject: : MyDataObject vFileName (char *) : refcount (1)
{
//set the custom accept the format for
M_AcceptFormat. CfFormat=CF_HDROP;
M_AcceptFormat. DwAspect=DVASPECT_CONTENT;
M_AcceptFormat. Tymed=TYMED_HGLOBAL;

//create a DROPFILES instance
DROPFILES tDropFiles;
TDropFiles. As fNC=false;
TDropFiles. FWide=true;
TDropFiles. PFiles=sizeof (DROPFILES);
TDropFiles. Pt. X=0;
TDropFiles. Pt. Y=0;

//produce custom storage media
M_StorageMedium. Tymed=TYMED_HGLOBAL;
M_StorageMedium. PUnkForRelease=NULL;
M_StorageMedium. HGlobal=& amp; TDropFiles;

//set the file name format
HGLOBAL hGblFiles;
LPSTR lpData.
HGblFiles=GlobalAlloc (GMEM_ZEROINIT | GMEM_MOVEABLE | GMEM_DDESHARE, sizeof (DROPFILES) + strlen (vFileName) + 2);

//for hGblFiles allocates memory and initialization
Char tFileName [256].
LpData=https://bbs.csdn.net/topics/(LPSTR) GlobalLock (hGblFiles);
Memcpy (lpData, & amp; TDropFiles, sizeof (DROPFILES));
Strcpy (lpData + sizeof (DROPFILES), vFileName);
GlobalUnlock (hGblFiles);

//call SetData
SetData (& amp; M_AcceptFormat, & amp; M_StorageMedium, true);

//called DoDragDrop
MyDropSource tDropSource;
DWORD dwEffect;

DoDragDrop (this, & amp; TDropSource DROPEFFECT_COPY, & amp; DwEffect);

Fprintf (stdout, Initiate a data object. \n");
}

//Destructors
MyDataObject: : ~ MyDataObject ()
{
Fprintf (stdout, Destruct a data object. \n");
}

//IUnkown implementation
ULONG __stdcall MyDataObject: : AddRef ()
{
Return InterlockedIncrement (& amp; Refcount);
}

ULONG __stdcall MyDataObject: : Release ()
{
ULONG nRefCount=InterlockedDecrement (& amp; Refcount);

If (nRefCount==0)
Delete this.

Return nRefCount;
}

STDMETHODIMP MyDataObject: : QueryInterface (REFIID riid, void * * ppvObject) {
if (! PpvObject) return E_POINTER;

If (riid==IID_IUnknown) {
* ppvObject=static_cast & lt; IUnknown * & gt; (this);
AddRef ();
Return S_OK.
{} else if (riid==IID_IDropSource)
* ppvObject=static_cast & lt; IDataObject * & gt; (this);
AddRef ();
Return S_OK.
} else {
* ppvObject=NULL;
Return E_NOINTERFACE;
}
}

//Data Object implement
//present specified pformatetcIn structure described in the data, and sending it to the pmedium
STDMETHODIMP MyDataObject: : GetData (FORMATETC * pformatetcIn, STGMEDIUM * pmedium)
{
If (pformatetcIn==NULL | | pmedium==NULL) return E_INVALIDARG;

//to empty medium
Memset (pmedium, 0, sizeof (STGMEDIUM));

//type better if accepted
//if ((m_AcceptFormat tymed & amp; PformatetcIn - & gt; Tymed) & amp; &
//(m_AcceptFormat dwAspect==pformatetcIn - & gt; DwAspect) & amp; &
//(m_AcceptFormat cfFormat==pformatetcIn - & gt; CfFormat)) {

//to qualified data filling into pmedium
Pmedium - & gt; HGlobal=DupGlobalMem (m_StorageMedium. HGlobal);

//retrieves the hr=CopyStgMedium (pmedium, & amp; M_StorageMedium);
//
//if (S_OK!=hr)
//return DV_E_FORMATETC;
//
//if (pmedium - & gt; Tymed=={TYMED_HGLOBAL)
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related