我正在运行以下程序:
我正在尝试编写一个可以接收从Outlook拖动的附件的应用程序。这件事似乎真的没有得到充分的记录,但经过大量的研究和痛苦,我得到的是:
import struct
import wx
class MainFrame(wx.Frame):
def __init__(self, *args, **kwargs):
wx.Frame.__init__(self, *args, **kwargs)
self.drop_target = MyDropTarget()
self.SetSize((800, 600))
self.SetDropTarget(self.drop_target)
class MyDropTarget(wx.DropTarget):
def __init__(self):
wx.DropTarget.__init__(self)
self.fileContentsDataFormat = wx.DataFormat("FileContents")
self.fileGroupDataFormat = wx.DataFormat("FileGroupDescriptor")
self.fileGroupWDataFormat = wx.DataFormat("FileGroupDescriptorW")
self.composite = wx.DataObjectComposite()
self.fileContentsDropData = wx.CustomDataObject(format=self.fileContentsDataFormat)
self.fileGroupDropData = wx.CustomDataObject(format=self.fileGroupDataFormat)
self.fileGroupWDropData = wx.CustomDataObject(format=self.fileGroupWDataFormat)
self.composite.Add(self.fileContentsDropData, preferred=True)
self.composite.Add(self.fileGroupDropData)
self.composite.Add(self.fileGroupWDropData)
self.SetDataObject(self.composite)
def OnDrop(self, x, y):
return True
def OnData(self, x, y, result):
self.GetData()
format = self.composite.GetReceivedFormat()
data_object = self.composite.GetObject(format, wx.DataObject.Get)
if format in [self.fileGroupDataFormat, self.fileGroupWDataFormat]:
# See:
# https://docs.microsoft.com/en-us/windows/win32/api/shlobj_core/ns-shlobj_core-filedescriptora
filenames = []
data = data_object.GetData()
count = struct.unpack("i", data[:4])
fmt = "i16s8s8si8s8s8sii260s"
for unpacked in struct.iter_unpack(fmt, data[4:]):
filename = ""
for b in unpacked[10]:
if b:
filename += chr(b)
else:
break
filenames.append(filename)
print(filenames)
return result
app = wx.App(redirect=False)
frame = MainFrame(None)
frame.Show()
app.MainLoop()
因此,现在我的应用程序接受拖拽的Outlook附件,我可以解析它们的名称,但如何获取实际的文件内容呢?我似乎从未收到任何使用“文件内容”格式的数据对象
在我的旅行中,我发现:
这让我发疯了,每次我觉得我正在接近一个解决方案,它都在逃避我
不,使用普通wxPython不可能实现这一点。问题在于wx:s数据对象的概念不同于WIN32:s。在WX中,DataObject有一个它支持的所有格式的列表。假设每种格式对应于一条数据。在WIN32中,DataObject在请求数据时采用结构,该结构除了采用格式外,还采用索引。从Outlook拖放文件要求您提供索引以迭代拖动的文件及其内容,而无法向WX提供此索引
因此,我必须编写自己的拖放功能。此实现是特定于Windows的。另外,由于每个窗口只能调用一次RegisterDragDrop,这意味着此代码与WX:s拖放不兼容:
相关问题 更多 >
编程相关推荐