拖放相關的介面包括
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.