2
1

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用 ドラッグ&ドロップクラス DropAgent の紹介

Last updated at Posted at 2025-09-29

はじめに

Delphiでドラッグ&ドロップ処理を簡潔に扱うための汎用クラス「DropAgent」について紹介します。従来の TDropFile クラスをベースに、より柔軟かつイベント指向で設計し直したものです。


DropAgent の特徴

  • コントロール単位での柔軟なアタッチ処理
  • 複数形式(ファイル、テキスト、画像など)に対応可能
  • ファイルドロップ時のハンドラ OnDropFiles を提供
  • 自動受信または手動制御の選択が可能
procedure TForm1.FormCreate(Sender: TObject);
begin
  FDrop := TDropAgent.Create(Self);
  FDrop.OnDropFiles := HandleDropFiles;
  FDrop.Attach(Edit1);
end;

利用可能イベント

イベント名 説明
OnDropFiles ファイルがドロップされた時
OnDropText テキストがドロップされた時
OnDropHtml 装飾付きテキストがドロップされた時

ドロップ判定の種類

受け入れるデータ種別は FAcceptKinds プロパティにより制御可能です:

  • dakFiles: ファイルドロップ(HDROP
  • dakText: テキスト(CF_TEXT / CF_UNICODETEXT)
  • dakHtml: 装飾付きテキスト(CF_HTML)

装飾付きテキストがドロップされると OnDropTextOnDropHtml が同時に発火します。 OnDropHtmlには装飾用のタグ付きのテキストが OnDropTextには装飾用のタグが無いテキストが渡されるので 用途で使い分けます

FDrop.AcceptKinds := [dakFiles, dakText];

サンプル

sample.pas
unit MainForm;

interface

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

type
  TFormMain = class(TForm)
    Memo1: TMemo;
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  private
    FDropAgent: TDropAgent;
    procedure OnDropText(Sender: TObject; Control: TWinControl; const Text: string);
    procedure OnDropHtml(Sender: TObject; Control: TWinControl; const Text: string);
    procedure OnDropFiles(Sender: TObject; Control: TWinControl; const FileNames: TArray<string>);
  public
  end;

var
  FormMain: TFormMain;

implementation

{$R *.dfm}

procedure TFormMain.FormCreate(Sender: TObject);
begin
  FDropAgent := TDropAgent.Create;

  // フォームにドロップ機能をアタッチ
  FDropAgent.Attach(Self);

  // 受け入れるドロップ種別を指定(テキストとファイル両方)
  FDropAgent.AcceptKinds := [dakText, dakFiles];

  // イベントハンドラの登録
  FDropAgent.OnDropText := OnDropText;
  FDropAgent.OnDropHtml := OnDropHtml;
  FDropAgent.OnDropFiles := OnDropFiles;
end;

procedure TFormMain.FormDestroy(Sender: TObject);
begin
  FDropAgent.Free;
end;

// テキストがドロップされたときの処理
procedure TFormMain.OnDropText(Sender: TObject; Control: TWinControl; const Text: string);
begin
  Memo1.Lines.Add('--- Text Dropped ---');
  Memo1.Lines.Add(Text);
end;

// HTMLがドロップされたときの処理(dakText に含まれる HTML の場合)
procedure TFormMain.OnDropHtml(Sender: TObject; Control: TWinControl; const Text: string);
begin
  Memo1.Lines.Add('--- Html Dropped ---');
  Memo1.Lines.Add(Text);
end;

// ファイルがドロップされたときの処理
procedure TFormMain.OnDropFiles(Sender: TObject; Control: TWinControl; const FileNames: TArray<string>);
var
  FileName: string;
begin
  Memo1.Lines.Add('--- Files Dropped ---');
  for FileName in FileNames do
    Memo1.Lines.Add(FileName);
end;

end.

ソースコードと配布先

GitHub でソースコードを公開しています。MITライセンスで自由に利用可能です。

相互リンク

この記事は下記の内容を発展・再構成したものです:

旧記事では基礎的な構成を、こちらの記事では応用・発展例を扱っています。

2
1
0

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
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?