如何接受拖放文件
在Delphi中接受拖放並不難,只要在FormCreate說明一下,然後處理WM_DROPFILES就可以了,以下是簡單的例子。
Type tFiles = array of string; TForm1 = class(TForm) ... private procedure OnAcceptfiles(Sender: tobject; var Files: tFiles); procedure AcceptFiles(var msg : TMessage); message WM_DROPFILES; ... end; ... implementation uses ShellAPI; procedure TForm1.FormCreate(Sender: TObject); begin DragAcceptFiles(Handle, True); end; procedure TPoemForm.AcceptFiles(var msg: TMessage); const cnMaxFileNameLen = 255; var i, n: integer; acFileName: array [0..cnMaxFileNameLen] of char; Files: tFiles; begin // 首先求得檔案數目 n:=DragQueryFile(msg.WParam, $FFFFFFFF, acFileName, cnMaxFileNameLen); Setlength(Files, n); try // 遂一求得檔案名 for i:=0 to n-1 do begin DragQueryFile(msg.WParam, i, acFileName, cnMaxFileNameLen); Files[i]:=acFileName; end; finally // 結束拖放處理 DragFinish(msg.WParam); end; if n>0 then OnAcceptfiles(Self, Files); end; procedure TPoemForm.OnAcceptfiles(Sender: tobject; var Files: tFiles); begin // 做想做的事 end; .... end.
上面例子中整個Form都可接受拖放文件,如果想只要Form的一部分接受文件,改變DragAcceptFiles所用的Handle即可,也可以多次使用DragAcceptFiles,使得多個控制元件接受拖放。
沒有留言:
發佈留言