拖放文件(10分)

  • 主题发起人 主题发起人 visual_cjiajia
  • 开始时间 开始时间
V

visual_cjiajia

Unregistered / Unconfirmed
GUEST, unregistred user!
拖放文件<br>以下代码只能保存一个拖动文件到<br>MEMO1控件,如何同时保存多个文件?<br>unit Unit1;<br><br>interface<br><br>uses<br>&nbsp; Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,<br>&nbsp; Dialogs, StdCtrls,Types,ShlObj,ShellApi;<br><br>type<br>&nbsp; Tfrmdropfile = class(TForm)<br>&nbsp; &nbsp; Memo1: TMemo;<br>&nbsp; &nbsp; Edit1: TEdit;<br>&nbsp; &nbsp; Memo2: TMemo;<br>&nbsp; &nbsp; procedure FormCreate(Sender: TObject);<br>&nbsp; private<br>&nbsp; &nbsp; { Private declarations }<br>&nbsp; public<br>&nbsp; &nbsp; { Public declarations }<br>&nbsp; &nbsp; procedure DropFile(var Msg :TWMDropFiles );message WM_DROPFILES;<br>&nbsp; end;<br><br>var<br>&nbsp; frmdropfile: Tfrmdropfile;<br><br>implementation<br><br>{$R *.dfm}<br>procedure Tfrmdropfile.DropFile(var Msg :TWMDropFiles );<br>var<br>&nbsp; FileName,<br>&nbsp; Extension:string;<br>&nbsp; AcceptStr:TStringList;<br>&nbsp; i,j,DropCount:Integer;<br>begin<br>SetLength(FileName,256);<br><br>AcceptStr:= TStringList.Create ;<br>&nbsp;Memo1.Clear() ;<br>&nbsp;Memo2.Clear() ;<br>DropCount:=DragQueryFile(HDROP(Msg.Drop),$FFFFFFFF,nil,0 );<br>for i:=0 to DropCount-1 do<br>begin file://1<br>&nbsp;DragQueryFile(HDROP(Msg.Drop),0,PChar(FileName),Length(FileName) );<br>&nbsp;Memo2.Lines.Add(FileName);<br>&nbsp;Extension:=ExtractFileExt(FileName);<br>&nbsp;Extension:=UpperCase(Extension) ;<br>&nbsp;SetLength(Extension,4);<br>&nbsp;if(Extension='.TXT') then<br>&nbsp;begin//2<br>&nbsp; AcceptStr.LoadFromFile(FileName);<br>&nbsp; for j:=0 to AcceptStr.Count-1 do<br>&nbsp; begin//3<br>&nbsp; &nbsp; Memo1.Lines.Add(AcceptStr.Strings[j]);<br>&nbsp; end;//3<br>&nbsp;end;//2<br>end;//1<br>&nbsp;Edit1.Text :=IntToStr(DropCount);<br>&nbsp;DragFinish(HDROP(Msg.Drop));<br>end;<br>procedure Tfrmdropfile.FormCreate(Sender: TObject);<br>begin<br>DragAcceptFiles(frmdropfile.Handle ,true);<br>end;<br><br>end.<br><br>
 
先对选择的文件进行排队,然后放到一个Tstrings里面,最后再进行托放的操作
 
后退
顶部