简体   繁体   中英

Winforms (with Python): Drag file object to Windows Explorer

I am using WinForms with Python / pytho.net. I have a WinForm object and want to drag the underlying file data to Windows Explorer.

I basically do this:

def on_mouse_down(self, sender, event):
   print('Mouse down')
   file_list = List[str]()
   file_list.Add('C:\\somefolder\\myfile.txt')
   do = WinForms.DataObject(WinForms.DataFormats.FileDrop, file_list)
   self.DoDragDrop(do, WinForms.DragDropEffects.Copy)

Dragging this on the Windows Explorer invokes the "Copy-File"-Cursor. But when I release the mouse button, nothing happens. It does not copy the file. Do you have any ideas what I am doing wrong?

Below is the full code I use. You can drag and drop files on/from the label.

import time

import clr
clr.AddReference('System.Windows.Forms')
clr.AddReference('System.Threading')
import System.Windows.Forms as WinForms
from System.Collections.Generic import List
from System.Threading import Thread, ThreadStart, ApartmentState


class Form(WinForms.Form):
    def __init__(self):
        lbl = WinForms.Label(self)
        lbl.Text ='Drag Me or Drop on me'
        self.Controls.Add(lbl)
        lbl.DragEnter += self.on_drag_enter
        lbl.DragDrop += self._on_drop
        lbl.MouseDown += self.on_mouse_down
        lbl.AllowDrop = True

    def _on_drop(self, sender, event):  # DROP
        print('drop')
        print([s for s in event.Data.GetData(WinForms.DataFormats.FileDrop)])

    def on_drag_enter(self, sender, event):  # DROP
        print('drag enter')
        event.Effect = WinForms.DragDropEffects.Copy

    def on_mouse_down(self, sender, event):
        print('Mouse down')
        file_list = List[str]()
        file_list.Add(r'C:\\somefolder\\myfile.txt')
        do = WinForms.DataObject(WinForms.DataFormats.FileDrop, file_list)
        self.DoDragDrop('dfdf', WinForms.DragDropEffects.Copy)


def create():
    app = WinForms.Application
    app.EnableVisualStyles()
    app.SetCompatibleTextRenderingDefault(False)
    Form().Show()
    app.Run()

thread = Thread(ThreadStart(create))
thread.SetApartmentState(ApartmentState.STA)
thread.Start()
thread.Join()

The problem was that I was passing the wrong datatype to the DataObject:

How not do it:

from System.Collections.Generic import List
def on_mouse_down(self, sender, event):
   file_list = Array[str](['C:\\myfolder\\myfile.zip'])
   file_list = List[str]()
   file_list.Add(String('C:\\myfolder\\myfile.zip'))
   do = WinForms.DataObject(WinForms.DataFormats.FileDrop, file_list)
   self.DoDragDrop(do, WinForms.DragDropEffects.All)

How to do it instead:

from System import Array
def on_mouse_down(self, sender, event):
   file_list = Array[str](['C:\\myfolder\\myfile.zip'])
   do = WinForms.DataObject(WinForms.DataFormats.FileDrop, file_list)
   self.DoDragDrop(do, WinForms.DragDropEffects.All)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM