L
love4myxsg
Unregistered / Unconfirmed
GUEST, unregistred user!
问题是 怎么模拟发送drag & drop消息(从explorer to 应用程序)
延伸问题是哪里能找到没有过期的资料T.T
事情的起因是这样的
本来想做一个模拟发送消息的实现,不料
总不能成功,
查资料得到wm_dropfiles的说明是
The structure is DROPFILES:
typedef struct _DROPFILES {
DWORD pFiles;
POINT pt;
BOOL fNC;
BOOL fWide;
} DROPFILES, FAR * LPDROPFILES;
可是我做了以下的代码却不能得到正确答案,
怀疑是资料过期了。
难道真的不能搞定这个问题吗?
下面是unit.pas源代码
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls,shlobj ;
type
TForm1 = class(TForm)
Button1: TButton;
Memo1: TMemo;
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
private
{ Private declarations }
PROCEDURE FinishDropped(VAR Msg : TMessage);Message WM_DropFiles;
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
uses shellapi;
{$R *.dfm}
procedure DoDropFiles(Wnd: HWND; Files: TStringList);
var
Size: Cardinal;
DropFiles: PDropFiles;
Run: PChar;
MemHandle: THandle;
I: Integer;
begin
Size := 0;
for I := 0 to Files.Count - 1 do
begin
Inc(Size, Length(Files) + 1);
end;
if Size > 0 then
begin
Inc(Size, 1 + SizeOf(TDropFiles));
MemHandle := GlobalAlloc(GMEM_ZEROINIT,Size);
DropFiles := GlobalLock(MemHandle);
with DropFiles^ do
begin
pFiles := SizeOf(TDropFiles);
pt := Point(0, 0);
fNC := False;
fWide := False;
end;
Run := Pointer(DropFiles);
Inc(Run, SizeOf(TDropFiles));
for I := 0 to Files.Count - 1 do
begin
StrPCopy(Run, Files);
Inc(Run, Length(Files) + 1);
end;
Run^ := #0;
GlobalUnlock(MemHandle);
PostMessage(Wnd, WM_DROPFILES, MemHandle, 0);
GlobalFree(MemHandle);
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
List: TStringList;
begin
List := TStringList.Create;
try
List.Add('C:/Autoexec.bat');
List.add('C:/config.sys');
DoDropFiles(form1.Handle, List);
finally
List.Free;
end;
end;
procedure TForm1.FinishDropped(var Msg: TMessage);
Var
hDrop : THandle ;
iFile:Uint;
lpszFile char;
CountOfFiles : integer ;
FileIndex : integer ;
ReSults : string ;
begin
hDrop := Msg.WParam ;
getmem(lpszfile,2048);
try
iFile:=$FFFFFFFF;
CountOfFiles := DragQueryFile(hDrop,iFile,lpszFile,2048);
//CountOfFiles := DragQueryFile(hDrop,iFile,nil,2048);
ReSults := '' ;
for FileIndex := 0 to CountOfFiles-1 do
begin
iFile:=FileIndex;
DragQueryFile(hDrop,iFile,lpszFile,2048);
// Get the File names by order;
ReSults := ReSults + lpszFile ;
end ;
finally
FreeMem(lpszFile);
end;
memo1.Clear;
memo1.lines.add('拖放了'+IntToStr(CountOfFiles) + '个文件:' + ReSults );
DragFinish(hDrop);
end ;
procedure TForm1.FormCreate(Sender: TObject);
begin
DragAcceptFiles(Form1.Handle,True);
end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
DragAcceptFiles(form1.Handle,False);
end;
end.
延伸问题是哪里能找到没有过期的资料T.T
事情的起因是这样的
本来想做一个模拟发送消息的实现,不料
总不能成功,
查资料得到wm_dropfiles的说明是
The structure is DROPFILES:
typedef struct _DROPFILES {
DWORD pFiles;
POINT pt;
BOOL fNC;
BOOL fWide;
} DROPFILES, FAR * LPDROPFILES;
可是我做了以下的代码却不能得到正确答案,
怀疑是资料过期了。
难道真的不能搞定这个问题吗?
下面是unit.pas源代码
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls,shlobj ;
type
TForm1 = class(TForm)
Button1: TButton;
Memo1: TMemo;
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
private
{ Private declarations }
PROCEDURE FinishDropped(VAR Msg : TMessage);Message WM_DropFiles;
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
uses shellapi;
{$R *.dfm}
procedure DoDropFiles(Wnd: HWND; Files: TStringList);
var
Size: Cardinal;
DropFiles: PDropFiles;
Run: PChar;
MemHandle: THandle;
I: Integer;
begin
Size := 0;
for I := 0 to Files.Count - 1 do
begin
Inc(Size, Length(Files) + 1);
end;
if Size > 0 then
begin
Inc(Size, 1 + SizeOf(TDropFiles));
MemHandle := GlobalAlloc(GMEM_ZEROINIT,Size);
DropFiles := GlobalLock(MemHandle);
with DropFiles^ do
begin
pFiles := SizeOf(TDropFiles);
pt := Point(0, 0);
fNC := False;
fWide := False;
end;
Run := Pointer(DropFiles);
Inc(Run, SizeOf(TDropFiles));
for I := 0 to Files.Count - 1 do
begin
StrPCopy(Run, Files);
Inc(Run, Length(Files) + 1);
end;
Run^ := #0;
GlobalUnlock(MemHandle);
PostMessage(Wnd, WM_DROPFILES, MemHandle, 0);
GlobalFree(MemHandle);
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
List: TStringList;
begin
List := TStringList.Create;
try
List.Add('C:/Autoexec.bat');
List.add('C:/config.sys');
DoDropFiles(form1.Handle, List);
finally
List.Free;
end;
end;
procedure TForm1.FinishDropped(var Msg: TMessage);
Var
hDrop : THandle ;
iFile:Uint;
lpszFile char;
CountOfFiles : integer ;
FileIndex : integer ;
ReSults : string ;
begin
hDrop := Msg.WParam ;
getmem(lpszfile,2048);
try
iFile:=$FFFFFFFF;
CountOfFiles := DragQueryFile(hDrop,iFile,lpszFile,2048);
//CountOfFiles := DragQueryFile(hDrop,iFile,nil,2048);
ReSults := '' ;
for FileIndex := 0 to CountOfFiles-1 do
begin
iFile:=FileIndex;
DragQueryFile(hDrop,iFile,lpszFile,2048);
// Get the File names by order;
ReSults := ReSults + lpszFile ;
end ;
finally
FreeMem(lpszFile);
end;
memo1.Clear;
memo1.lines.add('拖放了'+IntToStr(CountOfFiles) + '个文件:' + ReSults );
DragFinish(hDrop);
end ;
procedure TForm1.FormCreate(Sender: TObject);
begin
DragAcceptFiles(Form1.Handle,True);
end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
DragAcceptFiles(form1.Handle,False);
end;
end.