I have created a window usign CreateWindow() and would like to Drag a File from Explorer and Drop it onto the window.
Upon Dropping, i would like a MessageBox() to open which gives the Name of the file that i Dragged.
How can i do this.
Currently i have the code below, which does not work correctly, it just displays a MessageBox with balnk..
#include <oleidl.h>
#include "CIDropTarget.h"
//*************************************************************
// CIDropTarget
//*************************************************************
CIDropTarget::CIDropTarget()
{
OleInitialize(NULL);
// required: these MUST be strong locked
CoLockObjectExternal(this, TRUE, 0);
}
CIDropTarget::~CIDropTarget()
{
// unlock
CoLockObjectExternal(this, FALSE, 0);
// bye bye COM
OleUninitialize();
}
HRESULT CIDropTarget::QueryInterface(REFIID iid, void **ppvObject)
{
if(ppvObject == NULL)
return E_FAIL;
if (iid == IID_IUnknown)
{
AddRef();
(*ppvObject) = this;
return S_OK;
}
// compare guids fast and dirty
if (iid == IID_IDropTarget)
{
AddRef();
(*ppvObject) = this;
return S_OK;
}
return E_FAIL;
}
ULONG CIDropTarget::AddRef(void)
{
return 1;
}
ULONG CIDropTarget::Release(void)
{
return 1;
}
//*************************************************************
// Register
// Called by whom implements us so we can serve
//*************************************************************
BOOL CIDropTarget::Register(HWND* pWnd, UINT pDataType)
{
int m = MessageBox(*pWnd, "inside regester drag n drop", "test", MB_OK);
if(NULL == pWnd)
return E_FAIL;
if(0L == pDataType)
return E_FAIL;
// this is ok, we have it
DWORD hRes = ::RegisterDragDrop(*pWnd, this);
if(SUCCEEDED(hRes))
return TRUE;
// wont accept data now
return FALSE;
}
//*************************************************************
// Revoke
// Unregister us as a target
//*************************************************************
void CIDropTarget::Revoke()
{
//RevokeDragDrop(m_DropTargetWnd->m_hWnd);
}
//*************************************************************
// DragEnter
//*************************************************************
HRESULT CIDropTarget:
ragEnter(struct IDataObject *pDataObject, unsigned long grfKeyState, struct _POINTL pMouse, unsigned long * pDropEffect)
{
// int m = MessageBox(NULL, "inside Drag Enter", "test", MB_OK);
if(pDataObject == NULL)
return E_FAIL; // must have data
return S_OK;
}
//*************************************************************
// DragOver
// Coming over!
//*************************************************************
HRESULT CIDropTarget:
ragOver(unsigned long grfKeyState, struct _POINTL pMouse, unsigned long *pEffect)
{
return S_OK;
}
//*************************************************************
// DragLeave
// Free! At last!
//*************************************************************
HRESULT CIDropTarget:
ragLeave(void)
{
return S_OK;
}
//*************************************************************
// Drop
// Released stuff here. We check for text, but this could
// also be mem, a clipboard, ... Not real clean for a baseclass
//*************************************************************
HRESULT CIDropTarget:
rop(struct IDataObject *pDataObject, unsigned long grfKeyState, struct _POINTL pMouse, unsigned long *pdwEffect)
{
int m;// = MessageBox(NULL, "inside Drop", "test", MB_OK);
// do final effect
*pdwEffect = DROPEFFECT_COPY;
// Check the data
FORMATETC iFormat;
ZeroMemory(&iFormat, sizeof(FORMATETC));
STGMEDIUM iMedium;
ZeroMemory(&iMedium, sizeof(STGMEDIUM));
iFormat.cfFormat = CF_TEXT; // its my type
iFormat.dwAspect = DVASPECT_CONTENT;
iFormat.lindex = -1; // give me all baby
iFormat.tymed = TYMED_FILE; // need the file name and path
HRESULT hRes = pDataObject->GetData(&iFormat, &iMedium);
LPWSTR test = iMedium.lpszFileName;
m = MessageBox(NULL, (const char *)test, "test", MB_OK);
if(NULL == pDataObject)
return E_FAIL;
return S_OK;
}
//*************************************************************
// Stub implementation
// Real stuff would be done in parent
//*************************************************************
void CIDropTarget::GotDrop()
{
}
DWORD CIDropTarget::GotDrag(void)
{
return DROPEFFECT_LINK;
}
void CIDropTarget::GotLeave(void)
{
}
DWORD CIDropTarget::GotEnter(void)
{
return DROPEFFECT_LINK;
}
Upon Dropping, i would like a MessageBox() to open which gives the Name of the file that i Dragged.
How can i do this.
Currently i have the code below, which does not work correctly, it just displays a MessageBox with balnk..
#include <oleidl.h>
#include "CIDropTarget.h"
//*************************************************************
// CIDropTarget
//*************************************************************
CIDropTarget::CIDropTarget()
{
OleInitialize(NULL);
// required: these MUST be strong locked
CoLockObjectExternal(this, TRUE, 0);
}
CIDropTarget::~CIDropTarget()
{
// unlock
CoLockObjectExternal(this, FALSE, 0);
// bye bye COM
OleUninitialize();
}
HRESULT CIDropTarget::QueryInterface(REFIID iid, void **ppvObject)
{
if(ppvObject == NULL)
return E_FAIL;
if (iid == IID_IUnknown)
{
AddRef();
(*ppvObject) = this;
return S_OK;
}
// compare guids fast and dirty
if (iid == IID_IDropTarget)
{
AddRef();
(*ppvObject) = this;
return S_OK;
}
return E_FAIL;
}
ULONG CIDropTarget::AddRef(void)
{
return 1;
}
ULONG CIDropTarget::Release(void)
{
return 1;
}
//*************************************************************
// Register
// Called by whom implements us so we can serve
//*************************************************************
BOOL CIDropTarget::Register(HWND* pWnd, UINT pDataType)
{
int m = MessageBox(*pWnd, "inside regester drag n drop", "test", MB_OK);
if(NULL == pWnd)
return E_FAIL;
if(0L == pDataType)
return E_FAIL;
// this is ok, we have it
DWORD hRes = ::RegisterDragDrop(*pWnd, this);
if(SUCCEEDED(hRes))
return TRUE;
// wont accept data now
return FALSE;
}
//*************************************************************
// Revoke
// Unregister us as a target
//*************************************************************
void CIDropTarget::Revoke()
{
//RevokeDragDrop(m_DropTargetWnd->m_hWnd);
}
//*************************************************************
// DragEnter
//*************************************************************
HRESULT CIDropTarget:
{
// int m = MessageBox(NULL, "inside Drag Enter", "test", MB_OK);
if(pDataObject == NULL)
return E_FAIL; // must have data
return S_OK;
}
//*************************************************************
// DragOver
// Coming over!
//*************************************************************
HRESULT CIDropTarget:
{
return S_OK;
}
//*************************************************************
// DragLeave
// Free! At last!
//*************************************************************
HRESULT CIDropTarget:
{
return S_OK;
}
//*************************************************************
// Drop
// Released stuff here. We check for text, but this could
// also be mem, a clipboard, ... Not real clean for a baseclass
//*************************************************************
HRESULT CIDropTarget:
{
int m;// = MessageBox(NULL, "inside Drop", "test", MB_OK);
// do final effect
*pdwEffect = DROPEFFECT_COPY;
// Check the data
FORMATETC iFormat;
ZeroMemory(&iFormat, sizeof(FORMATETC));
STGMEDIUM iMedium;
ZeroMemory(&iMedium, sizeof(STGMEDIUM));
iFormat.cfFormat = CF_TEXT; // its my type
iFormat.dwAspect = DVASPECT_CONTENT;
iFormat.lindex = -1; // give me all baby
iFormat.tymed = TYMED_FILE; // need the file name and path
HRESULT hRes = pDataObject->GetData(&iFormat, &iMedium);
LPWSTR test = iMedium.lpszFileName;
m = MessageBox(NULL, (const char *)test, "test", MB_OK);
if(NULL == pDataObject)
return E_FAIL;
return S_OK;
}
//*************************************************************
// Stub implementation
// Real stuff would be done in parent
//*************************************************************
void CIDropTarget::GotDrop()
{
}
DWORD CIDropTarget::GotDrag(void)
{
return DROPEFFECT_LINK;
}
void CIDropTarget::GotLeave(void)
{
}
DWORD CIDropTarget::GotEnter(void)
{
return DROPEFFECT_LINK;
}