Click here to Skip to main content
11,805,604 members (54,533 online)
Click here to Skip to main content

DragSourceHelper MFC

, 19 Jan 2003 72.3K 1.1K 22
Rate this:
Please Sign up or sign in to vote.
Making use of the IDragSourceHelper interface with MFC
<!-- Article image -->

Sample Image - DropSourceHelperMFC.jpg

<!-- Add the rest of your HTML here -->


Beginning with Window2000 the shell exposes helper COM-objects that allow an application to specify an image that is displayed during a drag&drop operation. This image will automatically be alpha-blended to fit the current OS's look&feel. That's great news for a developer. The bad news is that it does not work with MFC's default implementation of the OLE data source without modification.


The shell (version 5 or higher) exposes the DragSourceHelper object

(CLSID_DragDropHelper) with the interface IDragSourceHelper.

HRESULT InitializeFromBitmap(      
  IDataObject *pDataObject
HRESULT InitializeFromWindow(      
  HWND hwnd,
  POINT *ppt,
  IDataObject *pDataObject

The purpose of the object is to give visual feedback during a drag&drop operation. Before issuing the ::DoDragDrop call, you need to instantiate the object and transfer the bitmap you would like to see while the object is dragged around. That's all. In MFC that procedure reads like this:

The Data Source

Instantiate and initialize the data source:

COleDataSource source;
   // add some content to the source

The Helper Object

Instantiate the helper object using standard COM techniques. Obtain a bitmap handle and initialize the SHDRAGIMAGE structure. Now initialize the helper with the bitmap and the data object. Please note that this code also works under systems that do not expose the helper object (Windows 95,98,ME and NT), it simply does not display a drag image.

CComPtr<IDragSourceHelper> pHelper;
HRESULT hr = CoCreateInstance(CLSID_DragDropHelper,NULL,
if SUCCEEDED(hr) {
  // load a testbitmap
  CBitmap bmp;
  // prepare the SHDRAGIMAGE structure
  BITMAP bmpInfo;
  // fill the drag&drop structure  
  SHDRAGIMAGE info; = bmpInfo.bmWidth; = 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
    // a pointer to the data object, the helper will store 
    // a reference to itself into this object
  // in case of an error we need to destroy the image, 
  // else the helper object takes ownership
  if FAILED(hr)

Starting the Drag&Drop operation

Perform standard drag&drop.

The Problem

Unfortunately this piece of code does not work because of a limitation of COleDataSource's implementation of the IDataObject interface. The reason is that in the call to InitializeFromBitmap the helper calls IDataObject::SetData with a custom clipboard format to keep a pointer to itself within the data object. The data object must satisfy this call. The default implementation of COleDataSource however does not accept unknown clipboard formats, so we need to override the IDataObject::SetData method.

The Solution

If we override the IDataObject::SetData method and allow an arbitrary format to be stored in the data object, everything works fine.
Class COleDataSourceEx : public COleDataSource
  // helper methods to fix IDropSourceHelper
  // handle name events
    INIT_INTERFACE_PART(COleDataSourceEx, DataObject)
    // IDataObject
STDMETHODIMP COleDataSourceEx::XDataObj::SetData(LPFORMATETC pFormatetc, 
    LPSTGMEDIUM pmedium, BOOL fRelease)
  METHOD_PROLOGUE(COleDataSourceEx, DataObj)
  // normal processing
  HRESULT hr = pThis->m_xDataObject.SetData(pFormatetc,pmedium,fRelease);
  if (hr==DATA_E_FORMATETC) {
    // cache the data explicitly
    return S_OK;
  // normal error
  return hr;


The Windows2000 DragSourceHelper object provides a cool way to do alpha blended drag&drop with only very few lines of code.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Carsten Leue
Web Developer
Germany Germany
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralDrag Image Temporarily Disappears Over Some Windows Pin
Raptorikus20-Jul-07 12:25
memberRaptorikus20-Jul-07 12:25 
QuestionDrop Destinations Infos? Pin
vertex_x1-Dec-06 3:13
membervertex_x1-Dec-06 3:13 
GeneralImage question Pin
Tamás Károly6-Sep-05 22:26
memberTamás Károly6-Sep-05 22:26 
GeneralRe: Image question Pin
serhio1-Dec-05 8:04
memberserhio1-Dec-05 8:04 
GeneralGood! Pin
dhatqd30-Mar-05 15:01
memberdhatqd30-Mar-05 15:01 
GeneralA couple of points Pin
Handyman11-Jun-03 21:35
memberHandyman11-Jun-03 21:35 
This article and the code really helped me get this going. But I have two points, and a question:

Firstly, In Win2003 Server (not sure about other operating systems) I needed to enable the "show window contents while dragging" effect in display properties to get it working in explorer windows.

Secondly, using the IDropTargetHelper is pretty straightforward (explained in an MSDN article) and allows you to show the bitmap within your own application.

Finally, I started to look for ways to do the same thing as in this article, but without all that BEGIN_INTERFACE_PART, etc. , by simply overiding the OnSetData() function of COleDataSource. I found that it is possible to get it working by making two calls to DelaySetData() with hardcoded values that seem to represent the clipboard format ids used by the helper, prior to calling InitializeFromBitmap(). However, I think these two numbers are system specific, becuase it doesn't work on other machines.

So what I would like to know is how to retrive these two values programmatically. I have tried to enumerate the clipboard formats, but nothing there seems to match. (though I've noticed that the enumeration doesn't seem to list all formats anyway - I tried enumerating it after regestering the HTML format and it doesn't return it...).

Any thoughts?

GeneralRe: A couple of points Pin
Carsten Leue14-Jun-03 3:39
memberCarsten Leue14-Jun-03 3:39 
Generalerror C2065: 'IDragSourceHelper' : undeclared identifier Pin
Anthony_Yio24-Feb-03 23:29
memberAnthony_Yio24-Feb-03 23:29 
GeneralRe: error C2065: 'IDragSourceHelper' : undeclared identifier Pin
Carsten Leue28-Feb-03 4:46
memberCarsten Leue28-Feb-03 4:46 
GeneralRe: error C2065: 'IDragSourceHelper' : undeclared identifier Pin
vinsonhsieh9-Jan-05 21:48
membervinsonhsieh9-Jan-05 21:48 
GeneralGood Job, but help me.... Pin
jedyking22-Jan-03 6:49
memberjedyking22-Jan-03 6:49 
GeneralRe: Good Job, but help me.... Pin
Carsten Leue22-Jan-03 11:04
memberCarsten Leue22-Jan-03 11:04 
GeneralHaven't you thougnt in make a one for the Win9X Pin
CosmoS2k20-Jan-03 18:57
memberCosmoS2k20-Jan-03 18:57 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.151002.1 | Last Updated 20 Jan 2003
Article Copyright 2003 by Carsten Leue
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid