__fastcall
TForm1::TForm1(TComponent*
Owner)
: TForm(Owner)
{
OleInitialize(NULL);
DropMemo1 = new TMyDropTarget(Memo1);
DropMemo1->OnProcessText = DropMemo1Text;
}
//---------------------------------------------------------------------------
__fastcall TForm1::~TForm1()
{
DropMemo1->Release();//delete
DropMemo1;
OleUninitialize();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::DropMemo1Text(IDropTarget
*Sender, AnsiString AText, DWORD KeyState, TPoint pt)
{
POINTS p={pt.x,pt.y};
*(long*)&p=SendMessage(Memo1->Handle,EM_CHARFROMPOS,0,*(long*)&p);
Memo1->SelStart = p.x;
Memo1->SelLength = 0;
Memo1->SelText = AText;
}
//---------------------------------------------------------------------------
--------------------next---------------------
//---------------------------------------------------------------------------
#ifndef UnitTMyDropTargetH
#define UnitTMyDropTargetH
//---------------------------------------------------------------------------
#include "oleidl.h"
#include "controls.hpp"
//---------------------------------------------------------------------------
class TMyDropTarget: public
IDropTarget
{
public:
__property void __fastcall
(__closure *OnGetFormat)(IDropTarget *Sender,
IDataObject *DataObject, DWORD KeyState, TPoint pt, DWORD &dpe,
bool &bDataOK) = { read=_FGetFormat, write=_FGetFormat
};
__property void __fastcall
(__closure *OnProcessData)(IDropTarget *Sender,
IDataObject *DataObject, DWORD KeyState, TPoint pt) = { read=_FProcessData,
write=_FProcessData };
__property
void __fastcall (__closure *OnProcessText)(IDropTarget
*Sender, AnsiString AText, DWORD KeyState, TPoint pt) = { read=_FProcessText,
write=_FProcessText };
TMyDropTarget(TWinControl
*AOwner);
~TMyDropTarget();
HRESULT __stdcall
QueryInterface(const GUID &iid, void
**ppv);
ULONG __stdcall
AddRef(void) { return
++_iRefCount; }
ULONG __stdcall
Release(void) { if(--_iRefCount==0){delete
this;} return
_iRefCount; }
HRESULT __stdcall
DragEnter(IDataObject *pDataObject, DWORD grfKeyState, POINTL
pt, DWORD *pdwEffect);
HRESULT __stdcall
DragOver(DWORD grfKeyState, POINTL pt, DWORD *pdwEffect);
HRESULT __stdcall DragLeave(void);
HRESULT __stdcall Drop(IDataObject
*pDataObject, DWORD grfKeyState, POINTL pt, DWORD * pdwEffect);
private:
ULONG
_iRefCount;
IDataObject *_lpDataObj;
TWinControl *_OwnerControl;
void
__fastcall (__closure *_FGetFormat)(IDropTarget
*Sender, IDataObject *DataObject, DWORD KeyState, TPoint pt,
DWORD &dpe, bool &bDataOK);
void
__fastcall (__closure *_FProcessData)(IDropTarget
*Sender, IDataObject *DataObject, DWORD KeyState, TPoint pt);
void __fastcall (__closure
*_FProcessText)(IDropTarget *Sender, AnsiString AText, DWORD
KeyState, TPoint pt);
bool
__fastcall FDataFormatOK(DWORD KeyState, POINTL pt,
DWORD &dpe);
void __fastcall
FProcessData(DWORD KeyState, POINTL pt);
};
//---------------------------------------------------------------------------
#endif
//---------------------------------------------------------------------------
--------------------next---------------------
//---------------------------------------------------------------------------
#include "UnitTMyDropTarget.h"
//---------------------------------------------------------------------------
TMyDropTarget::TMyDropTarget(TWinControl *AOwner)
{
_OwnerControl = AOwner;
_iRefCount
= 0;
_lpDataObj
= NULL;
_FGetFormat = NULL;
_FProcessData = NULL;
_FProcessText
= NULL;
RegisterDragDrop(_OwnerControl->Handle,this);
}
//---------------------------------------------------------------------------
TMyDropTarget::~TMyDropTarget()
{
RevokeDragDrop(_OwnerControl->Handle);
}
//---------------------------------------------------------------------------
HRESULT __stdcall TMyDropTarget::QueryInterface(const
GUID &iid, void **ppv)
{
if((iid==IID_IUnknown)||(iid==IID_IDropTarget))
{
*ppv=this;
AddRef();
}
else
{
*ppv=NULL;
return
E_NOINTERFACE;
}
return S_OK;
}
//---------------------------------------------------------------------------
HRESULT __stdcall TMyDropTarget::DragEnter(IDataObject
*pDataObject, DWORD grfKeyState, POINTL pt, DWORD *pdwEffect)
{
_lpDataObj =
pDataObject;
FDataFormatOK(grfKeyState,pt,*pdwEffect);
return S_OK;
}
//---------------------------------------------------------------------------
HRESULT __stdcall TMyDropTarget::DragOver(DWORD
grfKeyState, POINTL pt, DWORD *pdwEffect)
{
FDataFormatOK(grfKeyState,pt,*pdwEffect);
return S_OK;
}
//---------------------------------------------------------------------------
HRESULT __stdcall TMyDropTarget::DragLeave(void)
{
return
S_OK;
}
//---------------------------------------------------------------------------
HRESULT __stdcall TMyDropTarget::Drop(IDataObject
*pDataObject, DWORD grfKeyState, POINTL pt, DWORD * pdwEffect)
{
_lpDataObj =
pDataObject;
if(FDataFormatOK(grfKeyState,pt,*pdwEffect))
FProcessData(grfKeyState,pt);
return S_OK;
}
//---------------------------------------------------------------------------
bool __fastcall TMyDropTarget::FDataFormatOK(DWORD
KeyState, POINTL pt, DWORD &dpe)
{
FORMATETC fetc={CF_TEXT,0,DVASPECT_CONTENT,-1,DWORD(-1)};
bool bOK=_lpDataObj->QueryGetData(&fetc)==S_OK;
dpe = bOK?DROPEFFECT_COPY:DROPEFFECT_NONE;
if(_FGetFormat)
{
TPoint
point(pt.x,pt.y);
::ScreenToClient(_OwnerControl->Handle,&point);
_FGetFormat(this,_lpDataObj,KeyState,point,dpe,bOK);
}
return
bOK;
}
//---------------------------------------------------------------------------
void __fastcall TMyDropTarget::FProcessData(DWORD
KeyState, POINTL pt)
{
if(_FProcessData)
{
TPoint
point(pt.x,pt.y);
::ScreenToClient(_OwnerControl->Handle,&point);
_FProcessData(this,_lpDataObj,KeyState,point);
}
else
if(_FProcessText)
{
STGMEDIUM smed;
FORMATETC fetc={CF_TEXT,0,DVASPECT_CONTENT,-1,DWORD(-1)};
BOOL bValue=(_lpDataObj->GetData(&fetc,&smed)==S_OK);
if(bValue)
{
HGLOBAL
hData = GlobalLock(smed.hGlobal);
if(hData)
{
TPoint
point(pt.x,pt.y);
::ScreenToClient(_OwnerControl->Handle,&point);
_FProcessText(this,(char*)hData,KeyState,point);
GlobalUnlock(hData);
GlobalFree(hData);
}
}
}
}
//---------------------------------------------------------------------------
--------------------next---------------------
阅读(1655) | 评论(0) | 转发(0) |