当前位置:七道奇文章资讯编程技术VC/C++编程
日期:2011-03-22 13:54:00  来源:本站整理

<b>把别的程序(如网页等)的文字拖放到Memo里</b>[VC/C++编程]

赞助商链接



  本文“<b>把别的程序(如网页等)的文字拖放到Memo里</b>[VC/C++编程]”是由七道奇为您精心收集,来源于网络转载,文章版权归文章作者所有,本站不对其观点以及内容做任何评价,请读者自行判断,以下是其具体内容:

需求用 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)
{
   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);
       }
     }
   }
}
//---------------------------------------------------------------------------


  以上是“<b>把别的程序(如网页等)的文字拖放到Memo里</b>[VC/C++编程]”的内容,如果你对以上该文章内容感兴趣,你可以看看七道奇为您推荐以下文章:
  • <b>hosts是什么 hosts文件在什么位置 若何改正hosts</b>
  • <b>在 Windows 8 中手动安装语言包</b>
  • <b>五个常见 PHP数据库问题</b>
  • Windows中Alt键的12个高效快速的利用本领介绍
  • <b>MySQL ORDER BY 的实现解析</b>
  • <b>详解MySQL存储历程参数有三种范例(in、out、inout)</b>
  • <b>Win8系统恢复出来经典的开始菜单的办法</b>
  • <b>Win8系统花屏怎么办 Win8系统花屏的办理办法</b>
  • <b>Windows 7系统下无线网卡安装</b>
  • <b>为什么 Linux不需求碎片整理</b>
  • <b>Windows 8中删除账户的几种办法(图)</b>
  • <b>教你如安在win7下配置路由器</b>
  • 本文地址: 与您的QQ/BBS好友分享!
    • 好的评价 如果您觉得此文章好,就请您
        0%(0)
    • 差的评价 如果您觉得此文章差,就请您
        0%(0)

    文章评论评论内容只代表网友观点,与本站立场无关!

       评论摘要(共 0 条,得分 0 分,平均 0 分) 查看完整评论
    Copyright © 2020-2022 www.xiamiku.com. All Rights Reserved .