【发布时间】:2021-10-21 14:33:03
【问题描述】:
我正在运行以下命令:
- Python 3.7.9 64 位
- wxpython 4.1.1 msw (phoenix) wxWidgets 3.1.5
我正在尝试编写一个可以接收从 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 附件并且我可以解析它们的名称,但我如何获得实际的文件内容?我似乎从未收到任何使用“FileContents”格式的 DataObject:s...
在我的旅行中,我发现了以下内容:
- This site describes the types of Windows Clipboard formats and their corresponding names
- This questions which discusses how to accomplish this in C++
- This MSDN site which descries the layout of the FILEGROUPDESCRIPTORA struct
这让我发疯,每次我认为我正在接近解决方案时它都会逃避我......
【问题讨论】:
-
这些格式在此处正式完整记录:docs.microsoft.com/en-us/windows/win32/shell/…。对于 unicode 版本,您应该只使用“FileGroupDescriptorW”(=>FILEGROUPDESCRIPTORW),否则,您将遇到字符名称超出 ansi/codepage 的问题。
-
谢谢。我仍然需要说服 wxPython 给我 CFSTR_FILECONTENTS...
标签: python winapi wxpython python-3.7