Re: Simple Drag-Drop question
Hit a snag, I've gotten the drag working (I think), and I've gotten a
custom droptarget (kinda), and a custom rich edit control (again,
kinda). I was shooting for basic functionality here, and my problem
is that not only does the subclassed richedit not show up at all in
the class wizard (the subclassed listbox does), NOR does it ever
create itself (after I manually inserted it into the AFX_MAP. As soon
as I attempt to access anything in regards to it, it throws an assert
fail on IsWindow.
I'm really not sure what the problem is... I can think of plenty of
reasons it might fail once I try to drag drop to it, but I can't think
of a reason it would fail to initialize.
//my attempt at a subclassed RichEdit
// OLERichEditCtrl.cpp : implementation file
//
#include "stdafx.h"
#include "rtexec2.h"
#include "OLERichEditCtrl.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// COLERichEditCtrl
IMPLEMENT_DYNAMIC(COLERichEditCtrl, CRichEditCtrl)
COLERichEditCtrl::COLERichEditCtrl()
{
}
COLERichEditCtrl::~COLERichEditCtrl()
{
RevokeDragDrop(m_hWnd);
delete oleInterface;
}
BEGIN_MESSAGE_MAP(COLERichEditCtrl, CRichEditCtrl)
//{{AFX_MSG_MAP(COLERichEditCtrl)
// NOTE - the ClassWizard will add and remove mapping macros here.
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
BOOL COLERichEditCtrl::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Add your specialized code here and/or call the base class
oleInterface=new CRicheditOLEDROP();
//oleInterface->Register(m_hWnd);
oleInterface->Register(this);
return CRichEditCtrl::PreCreateWindow(cs);
}
#if !
defined(AFX_OLERICHEDITCTRL_H__1BEC3619_EE76_4761_B2F7_D4C6D0DBD2A7__INCLUDED_)
#define
AFX_OLERICHEDITCTRL_H__1BEC3619_EE76_4761_B2F7_D4C6D0DBD2A7__INCLUDED_
#include "RicheditOLEDROP.h"
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
// OLERichEditCtrl.h : header file
//
/////////////////////////////////////////////////////////////////////////////
// COLERichEditCtrl window
class COLERichEditCtrl : public CRichEditCtrl
{
DECLARE_DYNAMIC(COLERichEditCtrl)
// Construction
public:
COLERichEditCtrl();
protected:
//virtual void PreSubclassWindow();
//virtual DROPEFFECT OnDragEnter(CWnd* pWnd,COleDataObject*
pDataObject,DWORD dwKeyState,CPoint point);
//virtual DROPEFFECT OnDragOver(CWnd* pWnd,COleDataObject*
pDataObject,DWORD dwKeyState,CPoint point);
//virtual void OnDragLeave(CWnd* pWnd);
//virtual BOOL OnDrop(CWnd* pWnd,COleDataObject*
pDataObject,DROPEFFECT dropEffect,CPoint point);
// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(COLERichEditCtrl)
protected:
virtual BOOL PreCreateWindow(CREATESTRUCT& cs);
//}}AFX_VIRTUAL
// Implementation
public:
virtual ~COLERichEditCtrl();
// Generated message map functions
protected:
CRicheditOLEDROP* oleInterface;
//{{AFX_MSG(COLERichEditCtrl)
// NOTE - the ClassWizard will add and remove member functions here.
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
/////////////////////////////////////////////////////////////////////////////
//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations
immediately before the previous line.
#endif // !
defined(AFX_OLERICHEDITCTRL_H__1BEC3619_EE76_4761_B2F7_D4C6D0DBD2A7__INCLUDED_)
My attempt at a customized Drop.
#if !
defined(AFX_RICHEDITOLEDROP_H__37CEE5B2_9F70_4217_A1A1_6286904BB721__INCLUDED_)
#define
AFX_RICHEDITOLEDROP_H__37CEE5B2_9F70_4217_A1A1_6286904BB721__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
// RicheditOLEDROP.h : header file
//
/////////////////////////////////////////////////////////////////////////////
// CRicheditOLEDROP window
#define COPYTHEDATA 29347234 //some completely random, unused value.
class CRicheditOLEDROP : public COleDropTarget
{
// Construction
public:
CRicheditOLEDROP();
// Attributes
public:
// Operations
public:
// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CRicheditOLEDROP)
public:
virtual DROPEFFECT OnDragEnter(COleDataObject* pDataObject, DWORD
dwKeyState, CPoint point);
virtual void OnDragLeave();
virtual DROPEFFECT OnDragOver(COleDataObject* pDataObject, DWORD
dwKeyState, CPoint point);
virtual BOOL OnDrop(COleDataObject* pDataObject, DROPEFFECT
dropEffect, CPoint point);
//}}AFX_VIRTUAL
// Implementation
public:
virtual ~CRicheditOLEDROP();
// Generated message map functions
protected:
//{{AFX_MSG(CRicheditOLEDROP)
// NOTE - the ClassWizard will add and remove member functions here.
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
/////////////////////////////////////////////////////////////////////////////
//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations
immediately before the previous line.
#endif // !
defined(AFX_RICHEDITOLEDROP_H__37CEE5B2_9F70_4217_A1A1_6286904BB721__INCLUDED_)
// RicheditOLEDROP.cpp : implementation file
//
#include "stdafx.h"
#include "rtexec2.h"
#include "RicheditOLEDROP.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CRicheditOLEDROP
CRicheditOLEDROP::CRicheditOLEDROP()
{
}
CRicheditOLEDROP::~CRicheditOLEDROP()
{
}
BEGIN_MESSAGE_MAP(CRicheditOLEDROP, COleDropTarget)
//{{AFX_MSG_MAP(CRicheditOLEDROP)
// NOTE - the ClassWizard will add and remove mapping macros here.
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CRicheditOLEDROP message handlers
DROPEFFECT CRicheditOLEDROP::OnDragEnter(COleDataObject* pDataObject,
DWORD dwKeyState, CPoint point)
{
// TODO: Add your specialized code here and/or call the base class
//if the data is the kind we want
if (pDataObject->IsDataAvailable(CF_TEXT))
{
//here we can take bring window to top
//since what is attached to the COleDropTarget object is
//most likely a child window, you would probably have to
//send it a message so that it can activate it's parent
//or something.
SetFocus(m_hWnd);
//we can handle copy and move of this data
return DROPEFFECT_COPY|DROPEFFECT_MOVE;
}
//we can't handle this type of data
return DROPEFFECT_NONE;
}
void CRicheditOLEDROP::OnDragLeave()
{
// TODO: Add your specialized code here and/or call the base class
//return S_OK;
// COleDropTarget::OnDragLeave();
}
DROPEFFECT CRicheditOLEDROP::OnDragOver(COleDataObject* pDataObject,
DWORD dwKeyState, CPoint point)
{
// TODO: Add your specialized code here and/or call the base class
//if the data is the kind we want
if (pDataObject->IsDataAvailable(CF_TEXT))
{
//we can handle copy and move of this data
return DROPEFFECT_COPY|DROPEFFECT_MOVE;
}
//we can't handle this type of data
return DROPEFFECT_NONE;
}
BOOL CRicheditOLEDROP::OnDrop(COleDataObject* pDataObject, DROPEFFECT
dropEffect, CPoint point)
{
// TODO: Add your specialized code here and/or call the base class
BOOL Ret = FALSE;
//if it is a format that we handle
if (pDataObject->IsDataAvailable(CF_TEXT))
{
//grab the data
HGLOBAL hGlobal = pDataObject->GetGlobalData(CF_TEXT);
//Do something with the data
//here you would normally send a message back to the
//window registered to the COleDropTarget to tell it
//to do something with the data
SendMessage(m_hWnd,COPYTHEDATA,(unsigned int)hGlobal,0);
Ret = TRUE;
}
return Ret;
//return COleDropTarget::OnDrop(pDataObject, dropEffect, point);
return false;
}