在 Delphi 拖放中检查复制与移动
使用“标准”VCL 拖放事件 OnDragOver 和 OnDragDrop,如何区分“复制”和“移动”操作?
我似乎没有可用的 TDragType,并且键盘 Shift 状态未传递给这些事件。
Using the 'standard' VCL drag and drop events OnDragOver and OnDragDrop, how can I distinguish between "Copy" and "Move" operations?
I don't seem to have a TDragType available, and the keyboard Shift state isn't passed to these events.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您无法从事件中确定这一点,因为事件不会对您的应用程序需求或功能做出任何假设。
将特定的拖/放解释为具有任何特定的语义是应用程序本身的责任 - 操作系统无法知道应用程序将如何对文件的放置做出反应,因此无法假设拖动操作可能对用户意味着什么。
对于许多应用程序来说,复制/移动之间没有区别,只有拖放。
复制/移动区别是 Windows 资源管理器应用于文件操作的东西。对于“普通”拖/放,它会应用基于原始驱动器卷和目标驱动器卷的规则 - 默认情况下,在卷上拖/放文件是一种移动操作。默认情况下,跨卷拖/放是一种复制。
但这些只是由应用程序(Windows 资源管理器)确定的默认规则。用户可以在拖动期间和(最重要的是)放置时使用键盘快捷键覆盖这些默认值。但这些是由特定应用程序(即 Windows 资源管理器)而不是操作系统定义和解释的。
因此,如果您的应用程序是可以从 Windows 资源管理器中拖动的文件的放置目标,并且如果您的应用程序区分复制和移动是有意义的,那么您可能需要支持Windows 资源管理器 支持相同的键盘修饰符。我不认为这些是可修改的(尽管我建议确认这一点),因此您可以简单地测试拖动事件中 Ctrl 或 Shift 键的状态
: com/en-us/library/ms646301(VS.85).aspx" rel="noreferrer">GetKeyState() 可用于在任何给定时刻直接询问特定键的状态。
如果需要不同的“默认”行为,那么您必须对源信息应用您自己的测试,以确定哪个默认最有意义(即模仿Windows资源管理器 “卷边界”默认规则),或者只需为您的应用程序选择最合适或直观的默认操作。
This is not something you can determine from the events because the events make no assumptions about your application needs or capabilities.
Interpreting a particular drag/drop as having any particular semantics is the responsibility of the application itself - the OS cannot know how the application will react to the dropping of a file so can make no assumptions about what the dragging operation may imply for the user.
For many applications there will be no distinction between copy/move, there will be just drag-and-drop.
The copy/move distinction is something that Windows Explorer applies to file operations. For "vanilla" drag/drops it applies rules based on original and destination drive volumes - dragging/dropping files around on a volume is a move operation, by default. Dragging/dropping across volumes is a copy, by default.
But these are only default rules determined by the application (Windows Explorer). The user can override these default using keyboard shortcuts during dragging and (most importantly) when dropping. But these are defined and interpreted by the particular application - i.e. Windows Explorer - not the OS.
So, if you're application is a drop target for files that may be dragged from Windows Explorer, and if it makes sense for your application to discriminate between copy and move, then you may need to support the same keyboard modifiers that Windows Explorer supports. I don't believe these are modifiable (although I'd advise this be confirmed), so you could simply test the state of the Ctrl or Shift keys in your drag events:
GetKeyState() can be used to directly interrogate the state of a specific key at any given moment in time.
If a varying "default" behaviour is desired then you would have to apply your own tests to the source information to determine which default makes most sense (i.e. to mimic the Windows Explorer "volume boundary" default rules), or simply choose the most appropriate or intuitive default action for your application.
简短的回答是——你不知道。 VCL 的内置拖放系统无法区分两者。但是,您可以派生自己的 TDragObject/Ex 类来控制实际拖动的数据类型。
The short answer is - you don't. The VCL's built-in drag-n-drop system does not distinquish between the two. You can, however, derive your own TDragObject/Ex classes to control what kind of data actually gets dragged around.
如果您想在您的应用程序和其他 Windows 应用程序之间使用 Drag'n'Drop,值得一看 Anders Melander 的 Drag'n'Drop删除 Delphi 组件套件。
最新代码位于此处。
If you want to use Drag'n'Drop between your application and other Windows applications, it's worth looking at Anders Melander's Drag and Drop Component Suite for Delphi.
The latest code being here.
具体不确定 Delphi,但在 C# 中,您检查事件参数的AllowedEffect 属性。由于它们都链接回 Win32,我无法想象有多大区别。
http://msdn.microsoft.com/en- us/library/system.windows.forms.drageventargs.aspx 有一个很好的例子。希望这有帮助!
Not sure about Delphi specifically, but in C# you check the AllowedEffect property of your event parameter. Since they both link back to Win32 I can't imagine there is much difference.
http://msdn.microsoft.com/en-us/library/system.windows.forms.drageventargs.aspx has a good example. Hope this helps!