ドラッグ&ドラッグとフォーム

概要

hmV8の例の1つとなります。

ドラッグアンドドロップ

フォームへのドラッグ&ドロップに対応する例となります。

#JS = loaddll( hidemarudir + @"\hmV8.dll" );

#_ = dllfuncw(#JS, "DoString", R"JS(

let LibForms = host.lib("System.Windows.Forms");
let LibDrawing = host.lib("System.Drawing");

class DragAndDropForm {
    constructor() {
        this.form = new LibForms.System.Windows.Forms.Form();
        let lb = new LibForms.System.Windows.Forms.ListBox();
        lb.AllowDrop = true;
        lb.FormattingEnabled = true;
        lb.ItemHeight = 12;
        lb.Location = new LibDrawing.System.Drawing.Point(12, 19);
        lb.Size = new LibDrawing.System.Drawing.Size(413, 208);
        lb.TabIndex = 0;

        // thisの問題を回避するためには、以下のように一度アロー関数を挟んでおくとよい。
        lb.DragEnter.connect( (sender, e) => this.DragEnter(sender, e) ); 
        lb.DragDrop.connect( (sender, e) => this.DragDrop(sender, e) );

        this.listBox = lb;
        this.form.AllowDrop = true;
        this.form.ClientSize = new LibDrawing.System.Drawing.Size(440, 250);
        this.form.Controls.Add(this.listBox);
        this.form.Text = "Drag and Drop Form";
    }

    // リストにドラッグ&ドロップで入ってきた
    DragEnter(sender, e) {
        // ファイルドロップなら、可能な効果を全て表示
        if (e.Data.GetDataPresent(LibForms.System.Windows.Forms.DataFormats.FileDrop)) {
            e.Effect = LibForms.System.Windows.Forms.DragDropEffects.All;
        // それ以外は消す
        } else {
            e.Effect = LibForms.System.Windows.Forms.DragDropEffects.None;
        }
    }

    // 実際にドロップした
    DragDrop(sender, e) {
        // データ取り出し
        let data = e.Data.GetData(LibForms.System.Windows.Forms.DataFormats.FileDrop, false);
        // let count = data.Count();

        // リストに加える
        for ( let d of data ) {
            this.listBox.Items.Add(d);
        }
    }

    ShowDialog() {
        this.form.ShowDialog();
    }
}

let dNd = new DragAndDropForm();
dNd.ShowDialog();

)JS"
);

// 解放
freedll(#JS);

PICTURE