Drag and Drop Between Any CWnd-Derived Window

Environment: Visual C++ 5

Why this code?
Suppose you have a number or controls on various windows that you want to drag and drop data between. Now suppose that the data shown in your controls come from your own data objects, so that dragging and dropping is not only required for text by also your own objects. Enter this code.

This code demonstrates how to enable OLE 2 Drag and Drop in any CWnd derived control.

Before we get into it, I want to acknowledge contributions made by Microsoft's knowledge base article Q135299.

If you hate reading and want to implement something and learn it later, then skip to the implementation section. If you want a warmer and fuzzier feeling read the theory section.

THEORY

How OLE drag and drop works
Drag and Drop (D&D) works similar to the old Win3.1 clipboard cut and paste. In essence when someone initiates a drag operation (usually the WM_LBUTTONDOWN operation), data is copied to global memory in a specified format.

When the mouse is over a window that has been registered as a drop target accepting the format delivered to the global memory, the user can release the mouse and the framework will receive the drop operation.

MFC has provided COleDataSource and COleDataTarget to handle this functionality.

Enabling a window to initiate a Drag and Drop Operation
Now let's look at the specifics for enabling D&D using MFC. In the D&D initiating event for a particular control we need to do the following:

  • 1. Instantiate a COleDataSource object
    Most likely you will need to derive a class from the control you want to D&D enable. Your class needs to have a COleDataSource object. With something like:
    COleDataSource m_Source;
    
  • 2. Create a global memory space to hold our data.
    If the data we are sending is simple text then use
    HGLOBAL hgData=GlobalAlloc(GPTR,10 ); //The 10 is the data length
    

    If you want to send something else use

    //The 10 is the data length
    HGLOBAL hgData=GlobalAlloc(GMEM_MOVEABLE | GMEM_ZEROINIT ,10 ); 
    
  • 3. Lock the memory so something is won't move or be deleted:
    LPCSTR  lpData=(LPCSTR)GlobalLock(hgData);
    
  • 4. Put the data into the global memory
    After wresting with these formats at infinitum, I had to cheat when moving objects other than text. Since I don't want to create a copy of my object, I simply need to convey the address of the memory where my object is located.

    So, if I have an instance of CMyObject1 (for example somewhere previously I created it with something like Data = new CMyObject1();), then I can store the address of the object with:

    ltoa( Data, (char*)lpData, 10 );
    

    Of course if I am dragging just text, then all I need to do is fill lpData with the text I want to drag.

  • 5. Put the global data into the COleDropTarget bus. The format of the data that you choose to put into the OLE object will determine what windows can accept it. Text is the most commonly used format, so putting your data in the OLE object as Text will allow it to be dropped onto a wide variety of D&D drop enabled windows. (These windows can be any D&D enabled window in any application)

    For simple text use:

    m_Source.CacheGlobalData(CF_TEXT, hgData);
    

    In my case, however, I have put the memory address of the object and therefore don't want other applications to accept a drop of some number. So I will use a kind of owner-drawn format. You can also register your own format with RegisterClipBoardFormat, but I could never get this to work. So for our object example use:

    m_Source.CacheGlobalData(CF_OWNERDISPLAY, hgData);   
    
  • 6. Start the drag and drop
    The drag and drop function will only start with DoDragDrop and when the mouse moves out of the coordinates specified in the last parameter. So we start the D&D operation with:
    DROPEFFECT dropEffect = 
     m_Source.DoDragDrop(DROPEFFECT_COPY | DROPEFFECT_MOVE,
                         (LPCRECT)rClient);
    

  • 7. Do something with our source control
    If this is a move operation, we need to do something with the data in the source control (like delete the string from a listbox or clear out an edit box. After the mouse is released, program control will return to the calling function and therefore the return of DoDragDrop can be tested with something like:
    //this is your function to do something
    if((dropEffect&DROPEFFECT_MOVE)==DROPEFFECT_MOVE)
    CompleteMove();

  • 8. Clean up
    Lastly we need to clear out the data that was put into the COleDataSource with:
    m_Source.Empty();
    

Enabling a window to accept a Drag and Drop Operation
Not all windows have to be both drag and drop enabled. Some can be just drag and others be drop.

  • 1. Instantiate a COleDataTarget
    Like the source, we need to provide D&D interface objects. For the drop target this is the COleDataTarget object. Add a COleDataTarget to your derived drop enabled widow with something like:
    COleDataTarget m_Target;
    

  • 2. Register the window as one that is D&D enabled
    If you have a window instantiated with some FrameWnd then you can register the COleDataTarget member in the OnCreate method. But this won't work for Dialog boxes so you'll need to do this in the OnInitialUpdate. In either case this is done with:
    m_Target.Register(wnd);
    

  • 3. Respond to the OnDrop message
    At a minimum you will need to add the OnDrop method to your derived control object. You can also override the OnDragEnter(), OnDragLeave() and the OnDragOver() methods if you like. Within your OnDrop method you will need to do the following:

  • 4. Get the global memory
    To get the data that you put into global memory use something like:
    HGLOBAL hGlobal=pDataObject->GetGlobalData(CF_TEXT);
    

    If you will recall, because I am transferring the address of my object, I formatted the data into global memory with CF_OWNERDISPLAY. Therefore I will need to get it with the same format with:

    HGLOBAL hGlobal=pDataObject->GetGlobalData(CF_OWNERDISPLAY);
    

  • 5. Get the data in the global memory
    Get the data from global memory with:
    LPCSTR pData=(LPCSTR)GlobalLock(hGlobal);
    

    For simple text D&D operations you can do whatever you want with this text. However, for my needs I need to translate this text into an object by typecasting the numeric version of the text into my object with like:

    CMyObject1* MyObj = (CMyObject1*)atol( pData );
    

    Now I can do whatever I want with this object.

  • 6. Clean up
    Lastly the global memory can be clear with:
GlobalUnlock(hGlobal);

IMPLEMENTATION

I have created two wrapper classes for the OLE stuff. The following is what you need to do to implement these classes:

  • 1. In the InititInstance method you're your app, add:
    AfxOleInit();
    

  • 2. Add CDropSource and CDropTarget to your project.

  • 3. Derive a class for each kind of class you want to D&D enable. Add CDropSource to your class inheritance or create a CDropSource member if you want the control to initiate D&D operations. Add CDropTarget to your class inheritance or create a CDropTarget member if you want the control to accept D&D operations. (If you choose the multiple inheritance route, you need to explicitly create the new and delete operators.)

  • 4. After creating your form or dialog in the resource editor and assigning the controls to variables (of type control), replace the control type in the class controlling your form with the derived control classes you just created.

  • 5. In the OnInitialUpdate of the class controlling your form register the CDropTarget with m_List.Initialize( &m_List );

  • 6. If you want to handle move operations and you inherited from CDropSource, then override the CompleteMove() method of CDropSource. If you created a variable use the if statement in the StartDragging method of CDropSource to test for a move operation and do what you need.

  • 7. To initiate a drag operation, add the LButtonDown method of your derived controls. In that method call the StartDragging method of CDropSource.

  • 8. To accept a drop, add the OnDrop method to your derived control and finish the drop operation. See the example in the OnDrop method of CdragList for an object D&D or the OnDrop method of CDropTarget for a simpler text example.

A WORD ON THE SAMPLE PROJECT

The sample project contains a Drag and Drop enabled Edit and Listbox control and a drag only enabled tree control. All the entries in the listbox have CMyObject1 object behind them. Therefore double, clicking an entry in the list will show the data contained in the object. Obviously you can whatever you want.

When the user drags text from the edit box to the list, the app will create a CMyObject1 and put that into the D&D operation.

Note too that the D&D operation will only work for my windows. This is because I put the data into global memory in my format and not something more ubiquitous like CF_TEXT.

Downloads

Download demo project - 41 Kb


Comments

  • COleDropTarget::Register in a CComboBox derived class??

    Posted by Legacy on 10/21/2002 12:00am

    Originally posted by: Clodagh

    Hi all, 
    
    

    I'm implementing drag/drop to a CComboBox.

    Does anyone know where I should call the register function in a CComboBOx derived class?
    At the moment I've got it in the constructor and it's returning unsuccessfully:

    if ( !m_dropTarget.Register(this) )
    AfxMessageBox("Error Registering Drop Target");

    Any ideas of where I'm going wrong would be greatly appreciated!

    Reply
  • Why can't I drop?

    Posted by Legacy on 10/24/2001 12:00am

    Originally posted by: Cheng Du

    Microsoft has an html browser sample named MFCIE. If set it to design mode, it becomes a html editor.

    This editor can accept the text dragged from Visual C++ to be dropped in it. I guess it only accept the drop object with the format CF_TEXT.

    But it can NOT accept the object with other format.

    Why?

    Reply
  • Missing sample file?

    Posted by Legacy on 02/13/2000 12:00am

    Originally posted by: Juraj Lutisan

    Hi,

    I can not download file D&D.zip.

    Juraj

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Today's agile organizations pose operations teams with a tremendous challenge: to deploy new releases to production immediately after development and testing is completed. To ensure that applications are deployed successfully, an automatic and transparent process is required. We refer to this process as Zero Touch Deployment™. This white paper reviews two approaches to Zero Touch Deployment--a script-based solution and a release automation platform. The article discusses how each can solve the key …

  • On-demand Event Event Date: December 18, 2014 The Internet of Things (IoT) incorporates physical devices into business processes using predictive analytics. While it relies heavily on existing Internet technologies, it differs by including physical devices, specialized protocols, physical analytics, and a unique partner network. To capture the real business value of IoT, the industry must move beyond customized projects to general patterns and platforms. Check out this webcast and join industry experts as …

Most Popular Programming Stories

More for Developers

RSS Feeds