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

概要

IronPythonの例の1つとなります。www.ironpython.infoにある例題と同じです。

ドラッグアンドドロップ

フォームに適当にドラッグ&ドロップし、最後に秀丸にリストを返すといった例にしてみましょう。

以下のファイルをdraganddrop.pyとして保存します。

# coding : cp932

import clr
clr.AddReference('System.Windows.Forms')
clr.AddReference('System.Drawing')

from System.Drawing import Point, Size
from System.Windows.Forms import Application, DragDropEffects, DataFormats, Form, ListBox

class DragAndDropForm(Form):
    def __init__(self):

        self._listBox1 = ListBox()
        self._listBox1.AllowDrop = True
        self._listBox1.FormattingEnabled = True
        self._listBox1.ItemHeight = 12
        self._listBox1.Location = Point(12, 19)
        self._listBox1.Size = Size(413, 208)
        self._listBox1.TabIndex = 0
        self._listBox1.DragEnter += self._drag_enter
        self._listBox1.DragDrop += self._drag_drop


        self.AllowDrop = True
        self.ClientSize = Size(440, 250)
        self.Controls.Add(self._listBox1)

        self.Text = 'Drag and Drop Form'


    def _drag_enter(self, sender, e):
        if e.Data.GetDataPresent(DataFormats.FileDrop):
            e.Effect = DragDropEffects.All
        else:
            e.Effect = DragDropEffects.None


    def _drag_drop(self, sender, e):
        data = e.Data.GetData(DataFormats.FileDrop, False)
        for s in data:
            print self._listBox1.Items.Add(s)

listcnt = 0
listref = []
dNd = DragAndDropForm()
try:
    dNd.ShowDialog()
    hm.debuginfo(dNd._listBox1.Items.Count)
    for i in dNd._listBox1.Items:
        listref.append(i)
        hm.debuginfo(i)
    listcnt = dNd._listBox1.Items.Count
except:
    dNd.Close()
finally:
    dNd.Close()

.macからの呼び出し

本来は、IronPython内で「hm.Macro.Var[...]で処理した方が楽ですが、
ここでは秀丸マクロからIronPythonの値を取得する形で記述してみましょう。
「リストの個数」を得て、Get***ItemOfListで取得していく、というモノです。

#PY = loaddll( hidemarudir + "\\hmPy.dll");

#_ = dllfuncw( #PY, "DoFile", currentmacrodirectory + "\\draganddrop.py");

#listcnt = dllfuncw( #PY, "GetNumVar", "listcnt" );

##i=0;
while(##i < #listcnt) {
    $$item = dllfuncstrw( #PY, "GetStrItemOfList", "listref", ##i );
    message($$item);
    ##i = ##i + 1;
}

freedll( #PY );

PICTURE