外部文件拖到edit里显示路径和文件名的问题(100分)

  • 主题发起人 主题发起人 21816588
  • 开始时间 开始时间
2

21816588

Unregistered / Unconfirmed
GUEST, unregistred user!
在Form中只放一个Edit1,当外部文件拖到edit1里时,显示外部文件的路径和文件名怎样实现?希望能说详细点,或是给个例子,谢谢!
(我很菜)
另外我看了一个,但不行
Procedure FinishDropped ( VAR Msg : TMessage );Message WM_DropFiles ;

Var
hDrop : THandle ;
FileName :pchar;
CountOfFiles : integer ;
FileIndex : integer ;
begin
hDrop := Msg.WParam ;
getmem(FileName ,255);
try
CountOfFiles := DragQueryFile(hDrop,0,FileName ,254);
if (CountOfFiles<>0) then
Edit1.Text:=strPas(FileName );
finally
FreeMem(FileName );
end;
DragFinish ( hDrop);
end ;

已在private加入
procedure DropFiles(var Msg: TMessage); message WM_DROPFILES;
 
怎么没人回呀?
 
好像要在FORM1.Create里要加入DragAcceptFiles (Handle, True)吧!
 
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, shellapi;

type
TForm1 = class(TForm)
Edit1: TEdit;
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
procedure FinishDropped(var Msg: TMessage); message WM_DropFiles;
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FinishDropped(var Msg: TMessage);
var
hDrop: THandle;
FileName: pchar;
CountOfFiles: integer;
FileIndex: integer;
begin
hDrop := Msg.WParam;
getmem(FileName, 255);
try
CountOfFiles := DragQueryFile(hDrop, 0, FileName, 254);
if (CountOfFiles <> 0) then
Edit1.Text := strPas(FileName);
finally
FreeMem(FileName);
end;
DragFinish(hDrop);
end;

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

end.
这段代码没有问题 在我的机器上
 
还是不行,出错的是在Edit1.Text:=strPas(FileName );,意思是Edit1没声明吧,不知道为什么(Edit1是没问题的);
 
是不是你没有uses全啊
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, shellapi;
 
cdj000的代码,我这也没有问题
delphi7的
 
找到问题了,是Edit的名称错了,谢谢各位。
 
找到就好
呵呵
 
为什么我拖动‘我的电脑’的时候不显示路径呢?
 
如果Form中有多个Edit如何判断我拖到的是那个Edit,并在不同的Edit中显示(即文件拖到那个Edit那个显示,其它的Edit不显示)
 
老大,找一个DragDrop控件恐怕什么都解决了
 
如果Form中有多个Edit如何判断我拖到的是那个Edit,并在不同的Edit中显示(即文件拖到那个Edit那个显示,其它的Edit不显示)

可能只能使用窗口过程子类化了
代码如下:
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, shellapi;

type
TForm1 = class(TForm)
edt1: TEdit;
edt2: TEdit;
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
OldWndProc1,OldWndProc2: Pointer;
implementation

{$R *.DFM}


function Edit1WindowProc(hWnd:HWND;Msg,wParam,lParam:LongInt):LongInt;stdcall;
var
hDrop: THandle;
FileName: pchar;
CountOfFiles: integer;
FileIndex: integer;
begin
if Msg = WM_DropFiles then
begin
hDrop := wParam;
getmem(FileName, 255);
try
CountOfFiles := DragQueryFile(hDrop, 0, FileName, 254);
if (CountOfFiles <> 0) then
SetWindowText(hWnd,FileName);
finally
FreeMem(FileName);
end;
DragFinish(hDrop);
end;
Result:= CallWindowProc(OldWndProc1,hWnd,Msg,wParam,lParam);
end;


function Edit2WindowProc(hWnd:HWND;Msg,wParam,lParam:LongInt):LongInt;stdcall;
var
hDrop: THandle;
FileName: pchar;
CountOfFiles: integer;
FileIndex: integer;
begin
if Msg = WM_DropFiles then
begin
hDrop := wParam;
getmem(FileName, 255);
try
CountOfFiles := DragQueryFile(hDrop, 0, FileName, 254);
if (CountOfFiles <> 0) then
SetWindowText(hWnd,FileName);
finally
FreeMem(FileName);
end;
DragFinish(hDrop);
end;
Result:= CallWindowProc(OldWndProc2,hWnd,Msg,wParam,lParam);
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
OldWndProc1 := Pointer(SetWindowLong(Edt1.Handle,GWL_WNDPROC,Integer(@Edit1WindowProc)));
DragAcceptFiles(Edt1.Handle, True);

OldWndProc2 := Pointer(SetWindowLong(Edt2.Handle,GWL_WNDPROC,Integer(@Edit2WindowProc)));
DragAcceptFiles(Edt2.Handle, True);
end;

end.
 
谢谢:大唐电信
 

Similar threads

I
回复
0
查看
750
import
I
S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
I
回复
0
查看
1K
import
I
后退
顶部