WINDOWS的拖拽实现

拖放相关的接口包括

 

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.

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值