文件夹拖放(10)

  • 主题发起人 主题发起人 zqssoft
  • 开始时间 开始时间
Z

zqssoft

Unregistered / Unconfirmed
GUEST, unregistred user!
Delphi程序 让文件夹拖放到程序上获取该文件夹的路径!
 
procedure WMDROPFILES(var Msg: TMessage); message WM_DROPFILES;procedure TFrmmain.FormCreate(Sender: TObject);var i: Integer;begin DragAcceptFiles(Handle, True);end;procedure TFrmMain.WMDROPFILES(var Msg: TMessage);var FilesCount: Integer; // 文件总数 i: Integer; //f: textfile; S: string; FileName: array[0..255] of Char; // 文件名 Temp_FileName: string;begin FilesCount := DragQueryFile(Msg.WParam, $FFFFFFFF, nil, 0); Temp_FileName := ''; for i := 0 to FilesCount - 1 do begin DragQueryFile(Msg.WParam, i, FileName, 256); Temp_FileName := FileName; //ShowMessage(Temp_FileName); end; if Trim(Temp_FileName) <> '' then begin PriFileName := Temp_FileName; mmo_Tmp.Lines.LoadFromFile(Temp_FileName); if self.hexToBin(mmo_Tmp.Text, s) then redt_File.Text := s else MessageBox(Handle, '文件格式错误', '警告', MB_OK + MB_ICONWARNING + MB_DEFBUTTON2); mmo_Tmp.Text := redt_File.Text end; DragFinish(Msg.WParam);end;
 
楼上的兄弟,我说的是文件夹,不是文件。你给的代码是文件拖放的代码呀。
 
LZ典型的受应试教育的毒害者。。文件和文件夹不是一样的?对于系统内部来说,文件夹就是文件的一种。。。试下直接SHOW出来就能看到了 FilesCount := DragQueryFile(Msg.WParam, $FFFFFFFF, nil, 0); for i := 0 to FilesCount - 1 do begin DragQueryFile(Msg.WParam, i, FileName, 256); ShowMessage(FileName); end;
 
delphi 实现拖拽打开文件 收藏 步骤如下,delphi 7测试通过1、在uses 中增加 ShellAPI2、增加拖拽的消息处理函数 //声明 protected procedure WMDROPFILES(var Msg : TMessage); message WM_DROPFILES;//实现procedure TForm1.WMDROPFILES(var Msg: TMessage);var a : Integer; Buffer: Array[0..255] of Char;begin a := DragQueryFile(Msg.wParam, $FFFFFFFF, Buffer, 255); for a := 0 to a - 1 do begin //处理选择多个文件 DragQueryFile(Msg.wParam, a, Buffer, 255); //文件名就在Buffer里面了, ShowMessage(Buffer); end; DragFinish(Msg.wParam);end;3、指定接收拖拽的窗口句柄procedure TForm1.FormCreate(Sender: TObject);begin DragAcceptFiles(Handle, True);//使窗口句柄可以接收拖拽操作end;说明:上面代码可以使窗口的任何部位都可以接收拖拽文件,包含菜单、按钮、工具条等,photo shop就是窗口的任何部分都可以接收拖拽文件。可以修改FormCreate中的DragAcceptFiles(Handle, True)函数的第一个参数,这样可以指定一个小的区域来接收拖拽文件,比如一个listbox或memo等,这时需要修改procedure WMDROPFILES(var Msg : TMessage); message WM_DROPFILES; 因为该函数只是处理窗口的拖拽事件,而不处理子窗口的拖拽事件,一个变通的方式是做一个能够处理所有消息的函数,然后从中过滤出拖拽事件。本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/webouse/archive/2008/09/22/2961828.aspx
 
[Delphi]从外部拖拽文件作者:德罗德 来源:博客园 发布时间:2009-07-16 09:07 阅读:105 次 原文链接 [收藏] 1unit Unit1; 2 3interface 4 5uses 6 Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, 7 Dialogs, ComCtrls, StdCtrls; 8 9type10 TForm1 = class(TForm)11 ListView1: TListView;12 procedure FormCreate(Sender: TObject);13 private14 { Private declarations }15 procedure WMDropFiles(var Msg: TWMDropFiles); message WM_DROPFILES;16 procedure AppOnMessage(var Msg: TMsg; var Handled: Boolean);17 public18 { Public declarations }19 end;2021var22 Form1: TForm1;2324implementation2526uses ShellAPI;2728{$R *.dfm}2930procedure TForm1.AppOnMessage(var Msg: TMsg; var Handled: Boolean);31var 32 WMD: TWMDropFiles;33begin 34 if Msg.message = WM_DROPFILES then35 begin36 WMD.Msg := Msg.message;37 WMD.Drop := Msg.wParam;38 WMD.Unused := Msg.lParam;39 WMD.Result := 0;40 WMDropFiles(WMD); 41 Handled := TRUE;42 end;43end;4445procedure TForm1.FormCreate(Sender: TObject);46begin47 DragAcceptFiles(listview1.Handle, True);48 Application.OnMessage := AppOnMessage;49end;5051procedure TForm1.WMDropFiles(var Msg: TWMDropFiles);52var 53 N: Word;54 buffer: array[0..180] of Char;55 item: TListItem;56begin57 with Msg do58 begin59 for N := 0 to DragQueryFile(Drop, $FFFFFFFF, buffer, 1) - 1 do60 begin61 DragQueryFile(Drop, N, Buffer, 80);62 Item := ListView1.Items.Add;63 item.Caption := StrPas(Buffer);64 end;65 DragFinish(Drop);66 end;67end;6869end. 1.引用 ShellAPI单元2.定义AppOnMessage,拦截处理拖拽文件操作3.设置接收拖拽文件的对象。DragAcceptFiles(listview1.Handle, True);4.定义对拖拽文件的具体操作WMDropFiles(var Msg: TWMDropFiles);示例下载
 
答案: 想实现ListView接受外部的拖放,如从资源管理器中拖一个文件到当前程序的ListView中,请问怎么实现?? --------------------------------------------------------------- unit Unit1; interface uses Windows, ShellAPI, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, ComCtrls; type TForm1 = class(TForm) ListView1: TListView; procedure FormCreate(Sender: TObject); private { Private declarations } procedure WMDropFiles(var Msg: TWMDropFiles); message WM_DROPFILES; procedure AppOnMessage(var Msg: TMsg; var Handled: Boolean); public { Public declarations } end; var Form1: TForm1; implementation {$R *.dfm} procedure TForm1.AppOnMessage(var Msg: TMsg; var Handled: Boolean); var WMD: TWMDropFiles; begin if Msg.message = WM_DROPFILES then begin // MessageBeep(0); WMD.Msg := Msg.message; WMD.Drop := Msg.wParam; WMD.Unused := Msg.lParam; WMD.Result := 0; WMDropFiles(WMD); Handled := TRUE; end; end; procedure TForm1.FormCreate(Sender: TObject); begin DragAcceptFiles(listview1.Handle, True); // DragAcceptFiles(Handle, True); // DragAcceptFiles(Application.Handle, True); Application.OnMessage := AppOnMessage; end; procedure TForm1.WMDropFiles(var Msg: TWMDropFiles); var N: Word; buffer: array[0..180] of Char; item: TListItem; begin with Msg do begin for N := 0 to DragQueryFile(Drop, $FFFFFFFF, buffer, 1) - 1 do begin DragQueryFile(Drop, N, Buffer, 80); Item := ListView1.Items.Add; item.Caption := StrPas(Buffer); end; DragFinish(Drop); end; end; end.
 
多人接受答案了。
 
后退
顶部