Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem with Drag n Drop

Status
Not open for further replies.

tokool

Programmer
Nov 6, 2001
40
0
0
US
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 &quot;CIDropTarget.h&quot;

//*************************************************************
// 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, &quot;inside regester drag n drop&quot;, &quot;test&quot;, 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::DragEnter(struct IDataObject *pDataObject, unsigned long grfKeyState, struct _POINTL pMouse, unsigned long * pDropEffect)
{
// int m = MessageBox(NULL, &quot;inside Drag Enter&quot;, &quot;test&quot;, MB_OK);
if(pDataObject == NULL)
return E_FAIL; // must have data

return S_OK;
}

//*************************************************************
// DragOver
// Coming over!
//*************************************************************
HRESULT CIDropTarget::DragOver(unsigned long grfKeyState, struct _POINTL pMouse, unsigned long *pEffect)
{
return S_OK;
}

//*************************************************************
// DragLeave
// Free! At last!
//*************************************************************
HRESULT CIDropTarget::DragLeave(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::Drop(struct IDataObject *pDataObject, unsigned long grfKeyState, struct _POINTL pMouse, unsigned long *pdwEffect)
{
int m;// = MessageBox(NULL, &quot;inside Drop&quot;, &quot;test&quot;, 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, &quot;test&quot;, 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;
}

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top