Delphi使窗口支持文件拖放的简单例子,附源代码

  使窗口支持Windows Shell的文件拖放功能简单的方式是使用Windows API: DragAcceptFiles,然后使用Delphi的VCL消息函数重载机制处理WM_DROPFILES消息,调用DragQueryFile即可.

 

DragAcceptFiles Function

--------------------------------------------------------------------------------

Registers whether a window accepts dropped files.

Syntax

VOID DragAcceptFiles(          HWND hWnd,
    BOOL fAccept
);
Parameters

hWnd
Identifier of the window that is registering whether it will accept dropped files.
fAccept
Value that indicates if the window identified by the hWnd parameter accepts dropped files. This value is TRUE to accept dropped files or FALSE to discontinue accepting dropped files.
Return Value

No return value.

Remarks

An application that calls DragAcceptFiles with the fAccept parameter set to TRUE has identified itself as able to process the WM_DROPFILES message from File Manager.

Function Information

Minimum DLL Version shell32.dll version 4.0 or later
Custom Implementation No
Header shellapi.h
Import library shell32.lib
Minimum operating systems Windows NT 3.1, Windows 95

 

DragQueryFile Function

--------------------------------------------------------------------------------

Retrieves the names of dropped files that result from a successful drag-and-drop operation.

Syntax

UINT DragQueryFile(          HDROP hDrop,
    UINT iFile,
    LPTSTR lpszFile,
    UINT cch
);
Parameters

hDrop
Identifier of the structure containing the file names of the dropped files.
iFile
Index of the file to query. If the value of the iFile parameter is 0xFFFFFFFF, DragQueryFile returns a count of the files dropped. If the value of the iFile parameter is between zero and the total number of files dropped, DragQueryFile copies the file name with the corresponding value to the buffer pointed to by the lpszFile parameter.
lpszFile
Address of a buffer to receive the file name of a dropped file when the function returns. This file name is a null-terminated string. If this parameter is NULL, DragQueryFile returns the required size, in characters, of the buffer.
cch
Size, in characters, of the lpszFile buffer.
Return Value

When the function copies a file name to the buffer, the return value is a count of the characters copied, not including the terminating null character.

If the index value is 0xFFFFFFFF, the return value is a count of the dropped files. Note that the index variable itself returns unchanged, and will therefore remain 0xFFFFFFFF.

If the index value is between zero and the total number of dropped files and the lpszFile buffer address is NULL, the return value is the required size, in characters, of the buffer, not including the terminating null character.

Windows 95/98/Me: DragQueryFile is supported by the Microsoft Layer for Unicode. To use this, you must add certain files to your application, as outlined in Microsoft Layer for Unicode on Windows Me/98/95 Systems.

 

Function Information

Minimum DLL Version shell32.dll version 4.0 or later
Custom Implementation No
Header shellapi.h
Import library shell32.lib
Minimum operating systems Windows NT 3.1, Windows 95
Unicode Implemented as ANSI and Unicode versions. 

 

unit fm_DropFiles;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs,ShellApi,ShlObj, ExtCtrls, StdCtrls,ActiveX;

type
TfmDropFiles = class(TForm)
DropFileList: TListBox;
procedure FormCreate(Sender: TObject);
private
procedure WMDROPFILES(var Msg:TWMDROPFILES);message WM_DROPFILES;
public
{ Public declarations }
end;

var
fmDropFiles: TfmDropFiles;

implementation

{$R *.dfm}

procedure TfmDropFiles.FormCreate(Sender: TObject);
begin
DragAcceptFiles(Handle,True);
end;

procedure TfmDropFiles.WMDROPFILES(var Msg: TWMDROPFILES);
var
DropFileName:string;
DropCount:integer;
I:integer;
begin
inherited;
SetLength(DropFileName,MAX_PATH);
DropCount:=DragQueryFile(Msg.Drop,$FFFFFFFF,nil,0);
for I := 0 to DropCount-1 do
begin
DragQueryFile(Msg.Drop,I,PChar(DropFileName),MAX_PATH);
DropFileList.Items.Add(DropFileName);
end;
DragFinish(Msg.Drop);
end;

end.

 

下载:http://www.ctdisk.com/file/5490633

posted @ 2012-03-22 23:09  Max Woods  阅读(897)  评论(0编辑  收藏  举报