如何让我的窗体接受从资源管理器拖过来的文件 ?
解决方案 »
- 如何判断pagecontrol下的TabSheet是否打开还是关闭
- 字符串保存數據,前台以日期型處理
- 用SetLength分配的动态数组,在程序结束时需要人工释放内存吗?
- delphi中如何调用bartender条码打印程序?
- 请问怎么在chart里显示百分比?
- 与众不同的弹出式菜单问题 送50分
- 求购电子病历编辑器控件,急!!!
- 请问斑竹,我今天上午发的两个送分111的帖子怎么都没有了?拜托帮俺找找啊,俺是送给帮俺的朋友的//cry
- image1.picture.loadfromfile(xyz.jpg);这么简单的一句都出错!!!!
- delphi dll窗体调用
- 尘云MM生日,放分祝福~~~~~~~~嘻嘻
- mapx里查找多个图元的问题
unit UDropFiles;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs,ShellAPI;type
TForm1 = class(TForm)
procedure FormCreate(Sender: TObject);
procedure FormPaint(Sender: TObject);
private
{ Private declarations }
Dropped : boolean;
filelist: array [0..MAX_PATH-1] of string; procedure WmDropFiles(var msg: TWmDropFiles); message WM_DROPFILES;
public
{ Public declarations }
end;var
Form1: TForm1;
implementation{$R *.dfm}procedure TForm1.FormCreate(Sender: TObject);
begin
DragAcceptFiles(handle,true);
end;procedure TForm1.WmDropFiles(var msg: TWmDropFiles);
var
i,count: integer;
filename: array [0..MAX_PATH-1] of Char;
begin
Dropped:=true;
count:= DragQueryFile(msg.Drop ,$FFFFFFFF,nil,0);
for i:=0 to count-1 do begin
DragQueryFile(msg.Drop,i,@filename,sizeof(filename));
self.Canvas.TextOut(0,i*16,filename);
filelist[i]:=filename;
end;
end;procedure TForm1.FormPaint(Sender: TObject);
var i: integer;
begin
for i:=low(filelist) to high(filelist) do begin
self.Canvas.TextOut(0,i*16,filelist[i]);
end;
Dropped:=false;
end;end.
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. 使用时:
uses UntDrag;
public
procedure MyDrag(var Msg: TWMDropFiles); message WM_DropFiles;
procedure TForm1.FormCreate(Sender:TObject);
begin
DragAcceptFiles(Handle, True);
end;procedure TForm1.MyDrag(var Msg:TWMDropFiles);
var
hDrop:Cardinal;
begin
hDrop:=Msg.Drop; //这个是拖放句柄
//...(在这里可以用GetDragFileName和GetDragFileCount) //最后记得要用这两句话:
DragFinish(hDrop);
Msg.Result:= 0;
end;