全國最多中醫師線上諮詢網站-台灣中醫網
發文 回覆 瀏覽次數:2001
推到 Plurk!
推到 Facebook!

檔案拖放的路徑

答題得分者是:jow
雪貓
一般會員


發表:7
回覆:18
積分:5
註冊:2006-08-10

發送簡訊給我
#1 引用回覆 回覆 發表時間:2007-11-17 11:30:33 IP:61.59.xxx.xxx 訂閱
程式在未啟動狀態時.我將外部檔案拖放至程式的ICON上
程式就會自動開啟.並將外部檔案以參數的方式讀取
我用 Label 來顯示


procedure TForm1.FormCreate(Sender: TObject);
var
i: Integer;
begin
If ParamCount >0 then
Begin
for i := 0 to ParamCount do
begin
Label1.caption :=ParamStr(i);
end;
end;

即可顯示 拖入的檔案路徑

那麼..程式開啟後..我若拖放檔案至 FORM 的空白處
該如何 抓取顯示拖入的檔案路徑?
是否請先進們指導一段簡易的程式碼?(或者給我一個最簡易的 SAMPLE)

檔案拖放至 FORM 的空白處
form.caption 即可顯示檔案路徑

這樣.我就可以自行判斷拖入檔案的屬性
再決定用何種方式處理後續
(論談裡.我根據其他先進們的方式試...似乎都變成 程式裡..自己物件的拖移..而不是外部檔案的拖入)
編輯記錄
雪貓 重新編輯於 2007-11-17 11:32:17, 註解 無‧
jow
尊榮會員


發表:66
回覆:751
積分:1253
註冊:2002-03-13

發送簡訊給我
#2 引用回覆 回覆 發表時間:2007-11-17 16:29:39 IP:123.193.xxx.xxx 訂閱
覺得有趣的主題, 上網爬文, 寫個小程式

提供提問者參考並與大家分享....

參考資料:

How to Drop Images from Windows Explorer to a TImage control
http://delphi.about.com/od/adptips2005/qt/dropontimage.htm



測試碼下載: http://delphi.ktop.com.tw/download.php?download=upload/473eaa53bf0b8_Test030.zip

[code delphi]
unit fMain;

interface
uses
Windows, Messages, Controls, SysUtils, Graphics, Forms, Jpeg, ExtCtrls, StdCtrls;

type
TForm1 = class(TForm)
procedure FormCreate(Sender: TObject);
private
P: TControl;
_OLD_WindowProc_: TWndMethod;
function CreateImage(var image: TImage): Boolean;
function ShowContent(const FileName: string): TControl;
function ShowBmp(const FileName: string): TControl;
function ShowJpg(const FileName: string): TControl;
function ShowTxt(const FileName: string): TControl;
protected
procedure FormWindowProc (var Msg: TMessage);
procedure FilesDrop(Msg: TWMDropFiles) ;
end;

var
Form1: TForm1;

[/code]


[code delphi]

implementation

uses ShellApi;

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
P := nil;
WindowState := wsMaximized;
if ParamCount > 0 then
ShowContent(ParamStr(1));
_OLD_WindowProc_ := WindowProc;
WindowProc := FormWindowProc;
DragAcceptFiles(Handle, True) ;
end;

function TForm1.ShowContent(const FileName: string): TControl;
var
ext: string;
begin
if FileExists(FileName) then
begin
if P <> nil then FreeAndNil(P);
ext := LowerCase(ExtractFileExt(FileName));
if ext = '.bmp' then P := ShowBmp(FileName)
else if ext = '.jpg' then P := ShowJpg(FileName)
else if ext = '.jpeg' then P := ShowJpg(FileName)
else if ext = '.txt' then P := ShowTxt(FileName);
end;
Result := P;
end;

function TForm1.CreateImage(var image: TImage): Boolean;
begin
image := TImage.Create(Self);
image.Parent := Self;
image.Align := alClient;
image.Stretch := True;
Result := image <> nil;
end;

function TForm1.ShowBmp(const FileName: string): TControl;
var
image: TImage;
begin
Result := nil;
if FileExists(FileName) and
CreateImage(image) then
begin
image.Picture.LoadFromFile(FileName);
Result := image;
end;
end;

function TForm1.ShowJpg(const FileName: string): TControl;
var
g: TJPEGImage;
image: TImage;
begin
Result := nil;
if FileExists(FileName) and
CreateImage(image) then
begin
g := TJPEGImage.Create;
try
g.LoadFromFile(FileName);
image.Picture.Assign(g);
finally
FreeAndNil(g);
end;
Result := image;
end;
end;

function TForm1.ShowTxt(const FileName: string): TControl;
begin
Result := nil;
if FileExists(FileName) then
begin
Result := TListBox.Create(Self);
with TListBox(Result) do
begin
Parent := Self;
Align := alClient;
Items.LoadFromFile(FileName);
end;
end;
end;

procedure TForm1.FilesDrop(Msg: TWMDropFiles);
var
n: Longint;
fn: array[0..255] of Char;
begin
FillChar(fn, SizeOf(fn), 0);
n := DragQueryFile(Msg.Drop, $FFFFFFFF, nil, 0);
if n = 1 then //只能有一個檔案
if DragQueryFile(Msg.Drop, 0, @fn, sizeof(fn)) > 0 then
ShowContent(Trim(fn));
end;

procedure TForm1.FormWindowProc(var Msg: TMessage);
begin
if Msg.Msg = WM_DROPFILES then FilesDrop(TWMDropFiles(Msg))
else _OLD_WindowProc_(Msg) ;
end;

end.


[/code]
編輯記錄
jow 重新編輯於 2007-11-17 16:31:25, 註解 無‧
jow 重新編輯於 2007-11-17 16:47:23, 註解 上傳測試碼‧
系統時間:2024-04-29 15:22:58
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!