您的位置:C++老鼠窝编程 专题讨论 Drag&Drop托拽 正文
原作者:Carsten Leue 添加时间:2008-06-13 原文发表时间:2008-06-13 人气:28

本文章共2057字,分2页,当前第1页,快速翻页:
 

Sample Image - DropSourceHelperMFC.jpg

介绍

Window2000开始内核揭示了帮手COM对象(helper COM-objects),它允许应用指定图象在drag&drop操作时被显示的。这个图象将阿尔法自动地被混和适合当前OS的look&feel。 那是开发商的了不起的新闻。 坏消息是它不与MFC OLE数据来源的缺省实现一起使用。

背景

内核 (version 5 or higher) 给出了DragSourceHelper 对象

(CLSID_DragDropHelper) 具有接口 IDragSourceHelper.

HRESULT InitializeFromBitmap(      
  LPSHDRAGIMAGE pshdi,
  IDataObject *pDataObject
);
HRESULT InitializeFromWindow(      
  HWND hwnd,
  POINT *ppt,
  IDataObject *pDataObject
);

此对象的目的是在drag&drop操作时给出视觉反馈。 在发布之前::DoDragDrop调用,当对象被扯拽时您需要事例化对象和转移您希望看的位图。 那是全部。 在MFC中做法就是这样:

数据源

事例并初始化一个数据源:

COleDataSource source;
   // add some content to the source

(帮助对象)

使用标准COM技术,例示帮手对象。 得到位图柄并且初始化SHDRAGIMAGE结构。 现在初始化 位图和数据对象的帮手。请注意:这个代码也工作没有暴露(helper object)帮手对象的系统(Windows 95,98,我和NT),它不显示drag图象。

Collapse

CComPtr<IDragSourceHelper> pHelper;
HRESULT hr = CoCreateInstance(CLSID_DragDropHelper,NULL,
  CLSCTX_ALL,IID_IDragSourceHelper,(LPVOID*)&pHelper);
if SUCCEEDED(hr) {
  // load a testbitmap
  CBitmap bmp;
  GetBitmap(bmp);
  // prepare the SHDRAGIMAGE structure
  BITMAP bmpInfo;
  bmp.GetBitmap(&bmpInfo);
  // fill the drag&drop structure  
  SHDRAGIMAGE info;
  info.sizeDragImage.cx = bmpInfo.bmWidth;
  info.sizeDragImage.cy = bmpInfo.bmHeight;
  info.ptOffset.x = 0;
  info.ptOffset.y = 0;
  info.hbmpDragImage = (HBITMAP)bmp.Detach();
  info.crColorKey = GetSysColor(COLOR_WINDOW);
  // this call assures that the bitmap will be dragged around
  hr = pHelper->InitializeFromBitmap(
    // drag&drop settings
    &info,
    // a pointer to the data object, the helper will store 
    // a reference to itself into this object
    (IDataObject*)source.GetInterface(&IID_IDataObject)
  );
  // in case of an error we need to destroy the image, 
  // else the helper object takes ownership
  if FAILED(hr)
    DeleteObject(info.hbmpDragImage);
}

开始 Drag&Drop 操作

source.DoDragDrop();

问题

不幸由于IDataObject接口的COleDataSource的实现的局限,这个代码部分不工作。 原因是帮助对象在调用InitializeFromBitmap时,IDataObject ::SetData具有一个定制得剪贴板格式指针在数据对象之内。

数据对象必须满足这个调用。缺省实现COleDataSource不接受未知的剪贴板格式,因此我们需要重载IDataObject ::SetData方法。

解决方案

如果我们重载 IDataObject::SetData 允许一个任意格式被存储在数据对象中, 那么它将是好用的。

 

Class COleDataSourceEx : public COleDataSource
{
public:
  // helper methods to fix IDropSourceHelper
  DECLARE_INTERFACE_MAP() 
 
本文章更多内容1 - 2 - 下一页>>
本页地址
相关文章

怎样在你的程序和IE浏览器之间实现托拽或托
怎样在你的程序和IE浏览器之间实现托拽或托
怎样在你的程序和IE浏览器之间实现托拽或托
怎样在你的程序和IE浏览器之间实现托拽或托
怎样在你的程序和IE浏览器之间实现托拽或托
在VC/MFC中怎样实现托拽功能(4)
在VC/MFC中怎样实现托拽功能(3)
在VC/MFC中怎样实现托拽功能(2)
在VC/MFC中怎样实现托拽功能(1)
利用MFC实现对象拖放
VC MFC如何实现对话框的拖放?

相关评论

评论人:Webmaster2008-06-14
error C2065: 'IDragSourceHelper' : undeclared identifier
评论人:Webmaster2008-06-14
error C2065: 'IDragSourceHelper' : undeclared identifier
评论人:Webmaster2008-06-14
error C2065: 'IDragSourceHelper' : undeclared identifier

本文章所属分类:首页 专题讨论 Drag&Drop托拽