| 導購 | 订阅 | 在线投稿
分享
 
 
 

把其它程序(如網頁等)的文字拖放到Memo裏

來源:互聯網網民  2008-06-01 01:33:04  評論

原作者:Victor Chen

來源:C++ 愛好者

網址: http://www.cppfans.com/

Email: victor@cppfans.com

需要用 COM 接口的 OLE 拖放目標, 程序新建一個支持拖放目標的UnitTMyDropTarget 單元, 在這個單元裏創建一個 TMyDropTarget 類。

這個程序可以使從寫字板或者網頁等定義的文本塊用鼠標拖動, 放到程序的 Memo 裏, 插入到 Memo 的拖放的位置。

首先看看如何使用這個 TMyDropTarget 類:

在 Form 的頭文件裏 (例如 Unit1.h) 需要增加的內容(藍色部分是新增內容):

#include "UnitTMyDropTarget.h"

class TForm1 : public TForm

{

private: // User declarations

TMyDropTarget *DropMemo1; //自定義的拖放目標類

void __fastcall DropMemo1Text(IDropTarget *Sender, AnsiString AText, DWord KeyState, TPoint pt); //拖放事件

public: // User declarations

__fastcall TForm1(TComponent* Owner);

__fastcall ~TForm1();

};

在 From 的 .cpp 文件裏 (例如 Unit1.cpp), 需要增加的內容(藍色部分是新增內容):

__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;

}

//---------------------------------------------------------------------------

TMyDropTarget 類的實現:

頭文件 UnitTMyDropTarget.h 的內容:

//---------------------------------------------------------------------------

#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

//---------------------------------------------------------------------------

UnitTMyDropTarget.cpp 的內容:

//---------------------------------------------------------------------------

#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)

{

&n

 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
  原作者:Victor Chen 來 源:C++ 愛好者 網址: http://www.cppfans.com/ Email: victor@cppfans.com 需要用 COM 接口的 OLE 拖放目標, 程序新建一個支持拖放目標的UnitTMyDropTarget 單元, 在這個單元裏創建一個 TMyDropTarget 類。 這個程序可以使從寫字板或者網頁等定義的文本塊用鼠標拖動, 放到程序的 Memo 裏, 插入到 Memo 的拖放的位置。 首先看看如何使用這個 TMyDropTarget 類: 在 Form 的頭文件裏 (例如 Unit1.h) 需要增加的內容(藍色部分是新增內容): #include "UnitTMyDropTarget.h" class TForm1 : public TForm { private: // User declarations TMyDropTarget *DropMemo1; //自定義的拖放目標類 void __fastcall DropMemo1Text(IDropTarget *Sender, AnsiString AText, DWord KeyState, TPoint pt); //拖放事件 public: // User declarations __fastcall TForm1(TComponent* Owner); __fastcall ~TForm1(); }; 在 From 的 .cpp 文件裏 (例如 Unit1.cpp), 需要增加的內容(藍色部分是新增內容): __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; } //--------------------------------------------------------------------------- TMyDropTarget 類的實現: 頭文件 UnitTMyDropTarget.h 的內容: //--------------------------------------------------------------------------- #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 //--------------------------------------------------------------------------- UnitTMyDropTarget.cpp 的內容: //--------------------------------------------------------------------------- #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) { &n
󰈣󰈤
王朝萬家燈火計劃
期待原創作者加盟
 
 
 
>>返回首頁<<
 
 
 
 
 
 熱帖排行
 
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有