请不要小看这个问题(关于消息发送)(200分)

  • 主题发起人 love4myxsg
  • 开始时间
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 :pchar;
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.

 
下面是unit1.dfm
object Form1: TForm1
Left = 120
Top = 96
Width = 544
Height = 375
Caption = 'Form1'
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'MS Sans Serif'
Font.Style = []
OldCreateOrder = False
OnClose = FormClose
OnCreate = FormCreate
PixelsPerInch = 96
TextHeight = 13
object Button1: TButton
Left = 416
Top = 16
Width = 75
Height = 25
Caption = 'Button1'
TabOrder = 0
OnClick = Button1Click
end
object Memo1: TMemo
Left = 8
Top = 8
Width = 401
Height = 321
Lines.Strings = (
'Memo1')
TabOrder = 1
end
end
 
dragdrop用到com接口了
从这突破吧
 
DragAcceptFiles(Form1.Handle,True);
和这句话有没有一点关系呢?我原来做文件拖放的时候,是用了
DragAcceptFiles(Form1.Handle,True);
DragAcceptFiles(Appliction.Handle,True);
好像才成功的.
 
其实问题并不大,你将“GlobalFree(MemHandle);”一句去掉就可以了[8D]
 
多人接受答案了。
 
错误,已删除
 
to独帅: 你用过那个给你的程序发送消息成功了吗?
我发现只doDropfiles有问题。第一个参数好像只有本应用的子窗口才能成功
如果是其他程序窗口,结果不正确;想来是因为对每个程序分配的是不同的内存空间所至,
该问题如何解决是好,哎
 
看来确实是我画蛇添足了[:D]
我试了一下你的程序,如果将“GlobalFree(MemHandle);”一句去掉就可以了,其他的地方不用变。
我用它给我的程序发消息结果也是正确的。
至于MemHandle的释放问题,我就不清楚了。
而且我也不明白为什么用SendMessage时只有本程序的窗口才可以。而对别人的程序只能用PostMessage发送。
 

Similar threads

I
回复
0
查看
667
import
I
I
回复
0
查看
496
import
I
顶部