本文介绍了OLE的方式做拖放wxPython的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个在MS Windows上运行的wxPython应用程序,我希望它支持拖放它的实例(所以用户打开我的应用程序3次,并将数据从一个实例拖到另一个实例)。



wxPython中的简单拖放可以这样工作:


  1. 用户启动拖动:源窗口在wx.DataObject()中打包必需的数据,创建新的wx.DropSource,设置其数据并调用dropSource.DoDragDrop()

  2. 数据到目标窗口:下拉目标调用库函数GetData(),将实际数据传输到其wx.DataObject实例,最后 - dataObject.GetData()解包实际数据。

我想要一些更复杂的拖放功能,允许用户选择在之后拖动的数据

我的梦想的场景:


  1. 用户启动拖动:仅一些指向源窗口的指针被打包(某些功能或对象)。

  2. 用户将数据放入目标窗口:显示尼斯对话框,询问用户哪个拖放他选择的模式(比如拖曳歌曲名称,歌曲名称以及被拖放的艺术家的艺术家名称或整个专辑)。

  3. 用户选择拖放模式:拖放目标在拖动的数据对象上调用一些函数,然后从拖动源检索数据并将其传输到放置目标。

我的梦想的场景似乎在MS Windows中可行,但wxWidgets和wxPython的文档相当复杂和模糊。并不是所有的wx.DataObject类都可以在wxPython中使用(只有wx.PySimpleDataObject),所以我想让某人用这种方法分享他的经验。可以在wxPython中实现这种行为,而无需直接在winAPI中编码吗?



编辑:
ToniRuža给出了一个工作拖放示例的答案,但是这不完全是我的梦想的场景。他的代码在删除数据时操作数据( HandleDrop()显示弹出菜单),但是在拖动启动时(在 On_ElementDrag())中的数据是准备的)。在我的应用程序中,应该有三种不同的拖放模式,其中一些需要耗时的数据准备。这就是为什么我想推迟数据检索到用户删除数据并选择(潜在的代价)d& d模式的时候。



对于内存保护问题 - 我想使用用于进程间通信的OLE机制,如MS Office。您可以复制Excel图表,并将其粘贴到MS-Word中,其行为就像一个图像(也就是一种)。因为它的作品我相信可以在winAPI中完成。我只是不知道我是否可以在wxPython中编码它。

解决方案

好的,似乎不能完成我想要的方式。



可能的解决方案是:


  1. 传递一些参数在d&d之后,在用户在目标进程窗口中删除数据后,自行执行一些进程间通信。

  2. 使用 DataObjectComposite 以支持多种拖放格式和键盘修改器来选择当前格式。情况:


    1. 用户启动拖动。选中CTRL,ALT和SHIFT的状态,并根据此选择d& d格式。 DataObjectComposite已创建,并以已选择的格式设置数据。

    2. 用户在目标窗口中删除数据。删除目标要求丢弃DataObject支持的格式,并检索数据知道是什么格式。


我正在选择解决方案 2。,因为它不需要在进程之间手工制作通信,并且允许我在用户想要时避免不必要的数据检索只拖拽最简单的数据。



无论如何 - Toni,谢谢你的回答!玩了一点,它让我想起了d& d,并改变了我对这个问题的方法。


I have wxPython app which is running on MS Windows and I'd like it to support drag&drop between its instances (so the user opens my app 3 times and drags data from one instance to another).

The simple drag&drop in wxPython works that way:

  1. User initiates drag: The source window packs necessary data in wx.DataObject(), creates new wx.DropSource, sets its data and calls dropSource.DoDragDrop()
  2. User drops data onto target window: The drop target calls library function GetData() which transfers actual data to its wx.DataObject instance and finally - dataObject.GetData() unpacks the actual data.

I'd like to have some more sophisticated drag&drop which would allow user to choose what data is dragged after he drops.
Scenario of my dreams:

  1. User initiates drag: Only some pointer to the source window is packed (some function or object).
  2. User drops data onto target window: Nice dialog is displayed which asks user which drag&drop mode he chooses (like - dragging only song title, or song title and the artists name or whole album of the dragged artist).
  3. Users chooses drag&drop mode: Drop target calls some function on the dragged data object, which then retrieves data from the drag source and transfers it to the drop target.

The scenario of my dreams seems doable in MS Windows, but the docs for wxWidgets and wxPython are pretty complex and ambigious. Not all wx.DataObject classes are available in wxPython (only wx.PySimpleDataObject), so I'd like someone to share his experience with such approach. Can such behaviour be implemented in wxPython without having to code it directly in winAPI?

EDIT:Toni Ruža gave an answer with working drag&drop example, but that's not exactly the scenario of my dreams. His code manipulates data when it's dropped (the HandleDrop() shows popup menu), but data is prepared when drag is initiated (in On_ElementDrag()). In my application there should be three different drag&drop modes, and some of them require time-consuming data preparation. That's why I want to postpone data retrieval to the moment user drops data and chooses (potentially costly) d&d mode.

And for memory protection issue - I want to use OLE mechanisms for inter-process communication, like MS Office does. You can copy Excel diagram and paste it into MS-Word where it will behave like an image (well, sort of). Since it works I believe it can be done in winAPI. I just don't know if I can code it in wxPython.

解决方案

Ok, it seems that it can't be done the way I wanted it.

Possible solutions are:

  1. Pass some parameters in d&d and do some inter-process communication on your own, after user drops data in target processes window.
  2. Use DataObjectComposite to support multiple drag&drop formats and keyboard modifiers to choose current format. Scenario:

    1. User initiates drag. State of CTRL, ALT and SHIFT is checked, and depending on it the d&d format is selected. DataObjectComposite is created, and has set data in chosen format.
    2. User drops data in target window. Drop target asks dropped DataObject for supported format and retrieves data, knowing what format it is in.

I'm choosing the solution 2., because it doesn't require hand crafting communication between processes and it allows me to avoid unnecessary data retrieval when user wants to drag only the simplest data.

Anyway - Toni, thanks for your answer! Played with it a little and it made me think of d&d and of changing my approach to the problem.

这篇关于OLE的方式做拖放wxPython的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 20:12