Drag and drop text from other programs (such as Web pages) into memo

Source: Internet
Author: User
Tags bool

You need to use the OLE drop target of the COM interface, the program creates a unittmydroptarget unit that supports the drag-and-drop target, creating a Tmydroptarget class in this unit.

This program enables you to drag a text block, such as WordPad or a Web page, into the Memo of the program and insert it into the Memo drag-and-drop position.

First look at how to use this Tmydroptarget class:

What you need to add to the header file (for example, Unit1.h) in the Form (the Blue section is new):

#include "UnitTMyDropTarget.h"
Class Tform1:public Tform
{
Private://User declarations
Tmydroptarget *dropmemo1; Custom drag-and-drop target classes
void __fastcall Dropmemo1text (idroptarget *sender, ansistring atext, DWORD keystate, Tpoint PT); Drag-and-drop events
Public://User declarations
__fastcall TForm1 (tcomponent* Owner);
__fastcall ~tform1 ();
};
In the From. cpp file (for example, Unit1.cpp), what you need to add (the Blue section is new):
__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::D ropmemo1text (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;
}
//---------------------------------------------------------------------------
Implementation of the Tmydroptarget class:
Contents of header File 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 the;} 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 &A MP;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
//---------------------------------------------------------------------------
Content of 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::D ragenter (IDataObject *pdataobject, DWORD grfkeystate, Pointl PT, DWORD *pdweffect)
{
_lpdataobj = pDataObject;
Fdataformatok (Grfkeystate,pt,*pdweffect);
return S_OK;
}
//---------------------------------------------------------------------------
HRESULT __stdcall Tmydroptarget::D ragover (DWORD grfkeystate, Pointl PT, DWORD *pdweffect)
{
Fdataformatok (Grfkeystate,pt,*pdweffect);
return S_OK;
}
//---------------------------------------------------------------------------
HRESULT __stdcall Tmydroptarget::D ragleave (void)
{
return S_OK;
}
//---------------------------------------------------------------------------
HRESULT __stdcall Tmydroptarget::D rop (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);
}
}
}
}
//---------------------------------------------------------------------------

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.