4
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

外部アプリから Delphi のコンポーネントへドラッグ&ドロップする方法【TDropFileクラス公開】

Last updated at Posted at 2025-05-30

✅ はじめに

Delphi では、ファイルをフォームやコンポーネントにドラッグ&ドロップで渡す WM_DROPFILES メッセージが使えますが、外部アプリからのドロップに対応したクラス設計の情報は少なく、実用的なサンプルもほとんど見つかりません。

そこで本記事では、VCL の任意の TWinControl 派生コンポーネントに対応した汎用 D&D クラス TDropFile を紹介します。

🎯 対象読者

  • Delphi(VCL)でアプリを作成している方
  • 外部アプリからのファイルドロップを簡単に実装したい方
  • 複数コンポーネントに対応した共通クラス設計を求めている方

📦 機能概要

  • 外部アプリ(例:エクスプローラー)からのファイルドロップに対応
  • 任意の TWinControlAttach して処理を追加できる
  • イベントでドロップされたファイルの一覧を受け取れる
  • 元の WindowProc を保存・復元するため他の処理と干渉しにくい
  • デバッグ用ログ出力(OutputDebugString)あり

🧪 使用例

2025/7にユニットを修正したことに合わせてサンプルを修正

unit MainForm;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs,DropFile, Vcl.StdCtrls, Vcl.ExtCtrls;

type
  TFormMain = class(TForm)
    ListBox1: TListBox;
    ListBox2: TListBox;
    Panel1: TPanel;
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  private
    { Private 宣言 }
    FDrop1          : TDropFile;
    FDrop2          : TDropFile;
    procedure OnSelfDropFile1(Sender: TObject; Control: TWinControl; const FileNames: TArray<string>);
    procedure OnSelfDropFile2(Sender: TObject; Control: TWinControl; const FileNames: TArray<string>);
  public
    { Public 宣言 }
  end;

var
  FormMain: TFormMain;

implementation

{$R *.dfm}

procedure TFormMain.FormCreate(Sender: TObject);
begin
  FDrop1 := TDropFile.Create;
  FDrop1.OnDropReceived  := OnSelfDropFile1;
  FDrop1.Attach(ListBox1);
  FDrop2 := TDropFile.Create;
  FDrop2.OnDropReceived  := OnSelfDropFile2;
  FDrop2.Attach(ListBox2);
end;

procedure TFormMain.FormDestroy(Sender: TObject);
begin
  FDrop2.Detach(ListBox2);
  FDrop2.Free;
  FDrop1.Detach(ListBox1);
  FDrop1.Free;
end;

procedure TFormMain.OnSelfDropFile1(Sender: TObject; Control: TWinControl;
  const FileNames: TArray<string>);
var
  s : string;
begin
  ListBox1.Clear;
  for s in FileNames do
    ListBox1.Items.Add(s);

end;

procedure TFormMain.OnSelfDropFile2(Sender: TObject; Control: TWinControl;
  const FileNames: TArray<string>);
var
  s : string;
begin
  ListBox2.Clear;
  for s in FileNames do
    ListBox2.Items.Add(s);

end;

end.

💡 注意点・応用

  • WM_DROPFILES によるファイルドロップは、あくまで「ファイルパス」だけを受け取る形式です。テキストや画像そのものは扱えません。
  • 複数コンポーネントに Attach してもOK。Detach を忘れずに。
  • ドロップ処理中にアクセス違反が出る場合、Control.Handle がすでに解放されていないか注意してください。

📘 まとめ

この TDropFile クラスを使えば、Delphi の標準コンポーネントに手軽に外部ファイルドロップを組み込めます。フォーム、リストボックス、パネルなど、どんな TWinControl でも対応可能です。

実装例やご質問などあればコメントでどうぞ!

🖇 参考

4
3
3

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?