首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 更多程式語言 >> Delphi

Delphi TListView 實現檔案拖放功能完整指南

     編輯:Delphi

需求背景

標準 TListView 不支援直接接收檔案總管拖曳的檔案。本文示範如何透過 Windows API (DragAcceptFiles, WM_DROPFILES) 擴充 TListView 使其能接收檔案拖放,並在報表檢視模式下顯示檔名與路徑。

核心原理

  1. 呼叫 DragAcceptFiles(ListView.Handle, True) 讓控件接受 WM_DROPFILES 訊息。
  2. 在 Application.OnMessage 事件中攔截 WM_DROPFILES。
  3. 使用 DragQueryFile 取得檔案數量與完整路徑。
  4. 呼叫 DragFinish 結束拖放作業。
  5. 將檔名與路徑分別加入 ListView 欄位。

完整實作程式碼

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  ShellAPI, ComCtrls;

type
  TForm1 = class(TForm)
    ListView1: TListView;
    procedure FormCreate(Sender: TObject);
  private
    procedure AppMessage(var Msg: TMsg; var Handled: Boolean);
  public
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.FormCreate(Sender: TObject);
begin
  // 設定報表檢視雙欄
  ListView1.ViewStyle := vsReport;
  ListView1.Columns.Add.Caption := 'FileName';
  ListView1.Columns.Add.Caption := 'Path';

  // 允許拖放檔案
  DragAcceptFiles(ListView1.Handle, True);
  // 掛鉤應用程式訊息迴圈
  Application.OnMessage := AppMessage;
end;

procedure TForm1.AppMessage(var Msg: TMsg; var Handled: Boolean);
var
  FileCount, I: Integer;
  FileName: string;
  Item: TListItem;
begin
  // 僅處理送給 ListView1 的 WM_DROPFILES
  if (Msg.Message = WM_DROPFILES) and (Msg.Hwnd = ListView1.Handle) then
  begin
    FileCount := DragQueryFile(Msg.WParam, $FFFFFFFF, nil, 0);
    try
      for I := 0 to FileCount - 1 do
      begin
        SetLength(FileName, MAX_PATH);
        DragQueryFile(Msg.WParam, I, PChar(FileName), MAX_PATH);
        FileName := PChar(FileName);  // 修正長度

        Item := ListView1.Items.Add;
        Item.Caption := ExtractFileName(FileName);
        Item.SubItems.Add(ExtractFilePath(FileName));
      end;
    finally
      DragFinish(Msg.WParam);
    end;
    Handled := True;
  end;
end;

end.

測試步驟

  1. 編譯執行專案。
  2. 開啟檔案總管,選取一或多個檔案拖曳至 ListView1 區域。
  3. 滑鼠游標變為拖放圖示,放開後檔案資訊即出現在列表中。

注意事項

  • 需在 uses 加入 ShellAPI。
  • AppMessage 會攔截所有訊息,邏輯務必精簡,避免影響效能。
  • 適用於 Delphi 5 以上版本,Windows 2000/XP/後續版本均可運作。
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved