一个最基础的拖放问题,请教各位大虾^_^

Lenic__ 2008-01-21 01:38:13
在Delphi 7.0的编程中,如何让一个文本框记录拖过来的Windows文件的绝对路径。谢谢!
...全文
209 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
Lenic__ 2008-03-02
  • 打赏
  • 举报
回复
混啊,怎么忘记结贴了,该抽!抱歉,一时兴起忘记了,呵呵,原谅我这次吧。。。
jfjy_0 2008-02-18
  • 打赏
  • 举报
回复
学习了!
wxsan 2008-02-16
  • 打赏
  • 举报
回复
楼上的非常详细,佩服!
Lenic__ 2008-02-16
  • 打赏
  • 举报
回复
非常抱歉,在发完帖子之后就回家了。上网不便,耽误了不少时间,现把分数送上。同时也非常感谢,在搞了一个星期之后终于得到了答案。
pengxuan 2008-01-21
  • 打赏
  • 举报
回复

在Delphi中接受文件拖放



很不爽的是,用Delphi封装在Form里的那些东西是没办法接受用户从我的电脑里拖放到你的Form上的文件的,但在做软件的时候这又是很必要的,我昨天研究了一晚上终于解决了这个问题。
首先,给你的Project加一个Unit,代码如下:
unit untDrag;

interface

//用来告诉Windows你的Form可以接受文件拖放
{$EXTERNALSYM DragAcceptFiles}procedure DragAcceptFiles(hWnd: Cardinal; fAccept: Boolean); stdcall;
//得到拖放文件名和文件个数的API
{$EXTERNALSYM DragQueryFile}
function DragQueryFile(hDrop: Cardinal; iFile: Cardinal; lpszFile: PChar; cch: Integer): Integer; stdcall;

//释放Windows分配给拖放操作的内存
{$EXTERNALSYM DragFinish}
procedure DragFinish(hDrop: Cardinal); stdcall;
//得到拖放的文件个数
function GetDragFileCount(hDrop: Cardinal): Integer;
//得到拖放的文件名,通过FileIndex来指定文件编号,默认为第一个文件
function GetDragFileName(hDrop: Cardinal; FileIndex: Integer = 1): string;

implementation

procedure DragAcceptFiles; external 'Shell32';
function DragQueryFile; external 'Shell32';
procedure DragFinish; external 'Shell32';

function GetDragFileCount(hDrop: Cardinal): Integer;
const
DragFileCount=High(Cardinal);
begin
Result:= DragQueryFile(hDrop, DragFileCount, nil, 0);
end;

function GetDragFileName(hDrop: Cardinal; FileIndex: Integer = 1): string;
const
Size=255;
var
Len: Integer;
FileName: string;
begin
SetLength (FileName, Size);
Len:= DragQueryFile(hDrop, FileIndex-1, PChar(FileName), Size);
SetLength (FileName, Len);
Result:= FileName;
end;

end.

然后,在你需要处理拖放的Form的OnCreate里面加上这么一句:
DragAcceptFiles (Handle, True);

在TForm1的public里面加上如下声明:
procedure MyDrag (var Msg: TWMDropFiles); message WM_DropFiles;

下面是此过程的实现:
procedure TForm1.MyDrag (var Msg: TWMDropFiles);
var
hDrop: Cardinal;
...
begin
hDrop:= Msg.Drop; //这个是拖放句柄
...(在这里可以用GetDragFileName和GetDragFileCount)
//最后记得要用这两句话:
DragFinish (hDrop);
Msg.Result:= 0;
end;

当然,要在Form的Unit上面加上
uses untDrag;

5,390

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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