Drag-and-Drop Data Transfers

程序有时候会用到实现拖拽响应.

如果单纯实现拖文件的响应的话,比较简单.

 

Windows用于实现文件拖拽的主要用到以下几个api

DragFinish 拖拽结束

DragQueryFile 查询拖拽的文件

DragAcceptFiles 注册窗口,该窗口接受文件拖拽

 

调用DragAcceptFiles后,接受处理WM_DROPFILES即可

 

如果想实现自定义内容的拖拽的话,则要实现对应的接口.实现起来也是比较简单,按照Window提供给你的接口一步一步的来就行了.

 

MSDN中讲的很详细了,主要用到2个接口IDropSource和IDropTarget.一个是拖拽源要实现的接口,一个是拖拽目标要实现的接口.

另外,RegisterDragDrop 用于注册接口.

 

附上msdn描述:

 

Drag-and-Drop Data Transfers

While somewhat more complex to implement, drag-and-drop data transfer has some significant advantages over the Clipboard:

  • Drag-and-drop transfers can be done with a simple mouse movement, making operation more flexible and intuitive to use than the Clipboard.
  • Drag-and-drop provides the user with a visual representation of the operation. The user can follow the icon as it moves from source to target.
  • Drag-and-drop notifies the target when the data is available.

Drag-and-drop operations also use data objects to transfer data. However, the drop source must provide functionality beyond that required for Clipboard transfers:

  • The drop source must also create an object that exposes an IDropSource interface. The system uses IDropSource to communicate with the source while the operation is in progress.
  • The drag-and-drop data object is responsible for tracking cursor movement and displaying an icon to represent the data object.

Drop targets must also provide more functionality than is needed to handle Clipboard transfers:

  • The drop target must expose an IDropTarget interface. When the cursor is over a target window, the system uses IDropTarget to provide the target with information such as the cursor position, and to notify it when the data is dropped.
  • The drop target must register itself with the system by calling RegisterDragDrop. This function provides the system with the handle to a target window and a pointer to the target application's IDropTarget interface.
Note   For drag-and-drop operations, your application must initialize COM with OleInitialize, not CoInitialize.

The following procedure outlines the essential steps that are typically used to transfer Shell data with drag-and-drop:

  1. The target calls RegisterDragDrop to give the system a pointer to its IDropTarget interface and register a window as a drop target.
  2. When the user starts a drag-and-drop operation, the source creates a data object and initiates a drag loop by calling DoDragDrop.
  3. When the cursor is over the target window, the system notifies the target by calling one of the target's IDropTarget methods. The system calls IDropTarget::DragEnter when the cursor enters the target window, and IDropTarget::DragOver as the cursor passes over the target window. Both methods provide the drop target with the current cursor position and the state of keyboard modifier keys such as CTRL or ALT. When the cursor leaves the target window, the system notifies the target by calling IDropTarget::DragLeave. When any of these methods return, the system calls the IDropSource interface to pass the return value to the source.
  4. When the user releases the mouse button to drop the data, the system calls the target's IDropTarget::Drop method. Among the method's parameters is a pointer to the data object's IDataObject interface.
  5. The target calls the data object's IDataObject::GetData method to extract the data.
  6. With some Shell data transfers, the target might also need to call the data object's IDataObject::SetData method to provide feedback to the source on the outcome of the data transfer.
  7. When the target is finished with the data object, it returns from IDropTarget::Drop. The system returns the source's DoDragDrop call to notify the source that the data transfer is complete.
  8. Depending on the particular data transfer scenario, the source might need to take additional action based on the value returned by DoDragDrop and the values that are passed to the data object by the target. For instance, when a file is moved, the source must check these values to determine whether it must delete the original file.
  9. The source releases the data object.

While the procedures outlined above provide a good general model for Shell data transfer, there are many different types of data that can be contained in a Shell data object. There are also a number of different data transfer scenarios that your application might need to handle. Each data type and scenario requires a somewhat different approach to three key steps in the procedure:

  • How a source constructs a data object to contain the Shell data.
  • How a target extracts Shell data from the data object.
  • How the source completes the data transfer operation. 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值