WINDOWS的拖拽實現

來源:互聯網
上載者:User

拖放相關的介面包括

 

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.

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.