拖放相关的接口包括
1.注册支持拖放目标的窗口
RegisterDragDrop( HWND, IDragTarget* );
将需要支持拖放的目标窗口注册为可接收数据,并传入实现的IDragTarget接口。
2.当需要启动拖放时调用
DoDragDrop(
IDataObject * pDataObject, //Pointer to the data object
IDropSource * pDropSource, //Pointer to the source
DWORD dwOKEffect, //Effects allowed by the source
DWORD * pdwEffect //Pointer to effects on the source
);
DoDragDrop将进入拖放循环,循环中将依次调用所提供的接口。
A. 在拖放初始进入目标窗口时系统调用 IDropTarget::DragEnter.
HRESULT DragEnter(
IDataObject * pDataObject,
//Pointer to the interface of the source data
// object
DWORD grfKeyState, //Current state of keyboard modifier keys
POINTL pt, //Current cursor coordinates
DWORD * pdwEffect //Pointer to the effect of the drag-and-drop
// operation
);
此函数提供一个如下的DROPEFFECT代码,
DROPEFFECT_NONE = 0,
DROPEFFECT_COPY = 1,
DROPEFFECT_MOVE = 2,
DROPEFFECT_LINK = 4,
DROPEFFECT_SCROLL = 0x80000000
反映出拖动当前状态
DoDragDrop还调用IDropSource::GiveFeedback,反映当前显示状态
HRESULT GiveFeedback(
DWORD dwEffect //Effect of a drop operation,最近设置的状态码
);
该函数返回
E_INVALIDARG, E_UNEXPECTED, and E_OUTOFMEMORY,DRAGDROP_S_USEDEFAULTCURSORS 或者S_OK
显示当前图标
B. 在同一窗口移动时,调用IDropTarget::DragOver.
HRESULT DragOver(
DWORD grfKeyState, //Current state of keyboard modifier keys
//MK_CONTROL, MK_SHIFT, MK_ALT, MK_BUTTON, MK_LBUTTON, MK_MBUTTON, and MK_RBUTTON.
POINTL pt, //Current cursor coordinates
DWORD * pdwEffect //Pointer to the effect of the drag-and-drop
// operation
);
C. 离开时调用IDropTarget::DragLeave.
HRESULT DragLeave(void);
返回S_OK 即可
如果状态发生变化,将调用IDropSource::QueryContinueDrag,以便决定接下来的动作
如果返回S_OK DoDragDrop 先调用 IDropTarget::DragOver来继续操作.接着调用 IDropSource::GiveFeedback 反映图标
如果返回 DRAGDROP_S_DROP就调用IDropTarget::Drop,该函数返回一个状态码给源处理
如果返回 DRAGDROP_S_CANCEL就调用 IDropTarget::DragLeave.