如何获取其他窗体拖放来的文字?

coolwudi 2003-06-25 02:54:49
我现在写了一个程序,需要获取从其他程序(如 IE Word)中拖放到我的窗体(视窗体或是其它控件,但不能是 TMemo TRichEdit)。怎么办哪?急死我了! 求求各位大侠帮忙啊!先谢谢各位了!
...全文
58 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
goldencity 2003-06-27
  • 打赏
  • 举报
回复
up
naughtyboy 2003-06-26
  • 打赏
  • 举报
回复
下面是怎么用,拖放URL
unit DragDropUrl;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs,activex,DragDrop,shellapi, StdCtrls;

type
TForm1 = class(TForm)
Memo1: TMemo;
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
private
{ Private declarations }
procedure DoDroped(Sender: TObject; Msg: Pchar);
procedure WMDropFiles(var msg: TWMDropFiles);message wm_dropfiles;
public
{ Public declarations }

end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.DoDroped(Sender: TObject; Msg: Pchar);
begin
memo1.Lines.Add(msg)
end;

procedure TForm1.FormCreate(Sender: TObject);
var
dd: TTMyDrop;
res : HResult;
res1: HResult;
begin
OleInitialize(NIL);
dd := TTMyDrop.Create;
dd.OnDroped:=DoDroped;
res1 := CoLockObjectExternal(dd, true, false);
res := RegisterDragDrop(Handle, IDropTarget(dd));
//DragAcceptFiles(handle,True);
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
RevokeDragDrop(Handle);
OleUninitialize;
end;

procedure TForm1.WMDropFiles(var msg: TWMDropFiles);
var
numfile: integer;
i: integer;
buffer: array[0..255] of char;
begin
numfile := dragqueryfile(msg.Drop,0,nil,0);
for i := 0 to numfile-1 do
DragQueryFile(msg.Drop,i,@buffer,sizeof(buffer));
Memo1.Lines.Add(buffer);
end;

end.

naughtyboy 2003-06-26
  • 打赏
  • 举报
回复
unit DragDrop;

interface

uses
Windows, ActiveX, ComObj,Dialogs,Sysutils;

type
TDropEvent = procedure(Sender:TObject;Msg:Pchar)of object;
TTMyDrop = class(TComObject, IDropTarget)
private
FOnDroped: TDropEvent;
procedure SetOnDroped(const Value: TDropEvent);
protected
{Declare IDropTarget methods here}
function DragEnter(const dataObj: IDataObject; grfKeyState: Longint;
pt: TPoint; var dwEffect: Longint): HResult; stdcall;
function DragOver(grfKeyState: Longint; pt: TPoint;
var dwEffect: Longint): HResult; stdcall;
function DragLeave: HResult; stdcall;
function Drop(const dataObj: IDataObject; grfKeyState: Longint; pt: TPoint;
var dwEffect: Longint): HResult; stdcall;
public
property OnDroped:TDropEvent read FOnDroped write SetOnDroped;
end;

const
Class_TMyDrop: TGUID = '{846C94F8-7649-11D2-9836-0000E82EA1B1}';

implementation

uses ComServ,DragDropUrl;

{ TTMyDrop }

function TTMyDrop.DragEnter(const dataObj: IDataObject;
grfKeyState: Integer; pt: TPoint; var dwEffect: Integer): HResult;
var
enumFormatEtc: IEnumFormatEtc;
f:TFORMATETC;
count:Integer;
Found:boolean;
begin
dataObj.EnumFormatEtc(DATADIR_GET,enumFormatEtc);
Found:=false;
while (enumFormatEtc.Next(1,f,@count)=S_OK)and (count>0) do
begin
if (f.cfFormat=CF_TEXT) then
begin
Found:=true;
Break;
end;
end;
if Found then
Result:=S_OK
else
begin
result:=E_INVALIDARG;
dwEffect:=DROPEFFECT_NONE;
end;
end;

function TTMyDrop.DragLeave: HResult;
begin
result := S_OK;
end;

function TTMyDrop.DragOver(grfKeyState: Integer; pt: TPoint;
var dwEffect: Integer): HResult;
begin
result := S_OK;
end;

function TTMyDrop.Drop(const dataObj: IDataObject; grfKeyState: Integer;
pt: TPoint; var dwEffect: Integer): HResult;
var
enumFormatEtc: IEnumFormatEtc;
f:TFORMATETC;
count:Integer;
Found:boolean;
medium: TStgMedium;
begin
dataObj.EnumFormatEtc(DATADIR_GET,enumFormatEtc);
Found:=false;
while (enumFormatEtc.Next(1,f,@count)=S_OK)and (count>0) do
begin
if (f.cfFormat=CF_TEXT) then
begin
Found:=true;
Break;
end;
end;
if not Found then
begin
result:=E_INVALIDARG;
dwEffect:=DROPEFFECT_NONE;
Exit;
end;
dataObj.GetData(f,medium);
if medium.tymed =1 then
begin
if Assigned(fOnDroped) then
begin
fOnDroped(Self,PChar(GlobalLock(medium.hglobal)));
GlobalUnLock(medium.hglobal);
end;
result := S_OK;
end;
end;

procedure TTMyDrop.SetOnDroped(const Value: TDropEvent);
begin
FOnDroped := Value;
end;

initialization
TComObjectFactory.Create(ComServer, TTMyDrop, Class_TMyDrop,
'TMyDrop', '', ciMultiInstance{, tmApartment});
end.
hansion3406 2003-06-26
  • 打赏
  • 举报
回复
不会..
jun_01 2003-06-26
  • 打赏
  • 举报
回复
我关心的是WINAMP的拖放,不过也帮你UP!
DWGZ 2003-06-25
  • 打赏
  • 举报
回复
有一套控件drag&drop 功能很是强大去下载吧

1,184

社区成员

发帖
与我相关
我的任务
社区描述
Delphi Windows SDK/API
社区管理员
  • Windows SDK/API社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧