KnowledgeBase Archive

An Archive of Early Microsoft KnowledgeBase Articles

View on GitHub

Q135299: Using MFC OLE Drag and Drop to Drag Text Between Windows

Article: Q135299
Product(s): Microsoft C Compiler
Version(s): 1.50,1.51,1.52,1.52b,2.0,2.1,2.2,4.0,4.1,4.2,5.0,6.0
Operating System(s): 
Keyword(s): kbfile kbProgramming kbSample kbCOMt kbDragDrop kbMFC kbVC150 kbVC200 kbVC400 kbVC500 k
Last Modified: 02-JUL-2002

-------------------------------------------------------------------------------
The information in this article applies to:

- The Microsoft Foundation Classes (MFC), used with:
   - Microsoft Visual C++ for Windows, 16-bit edition, versions 1.50, 1.51, 1.52, 1.52b 
   - Microsoft Visual C++, 32-bit Editions, versions 2.0, 2.1, 2.2, 4.0, 4.1 
   - Microsoft Visual C++, 32-bit Enterprise Edition, versions 4.2, 5.0, 6.0 
   - Microsoft Visual C++, 32-bit Professional Edition, versions 4.2, 5.0, 6.0 
   - Microsoft Visual C++, 32-bit Learning Edition, version 6.0 
-------------------------------------------------------------------------------

SUMMARY
=======

In a Microsoft Foundation Classes (MFC) application, you may want to enable the
user to drag text between various windows, including those in CWnd-derived
objects as well as standard control windows in CListBox and CEdit-derived
objects.

A sample (LSTDRG) is available that demonstrates how to add OLE drag and drop
functionality to a pre-existing MFC application. This sample demonstrates using
OLE drag and drop functionality to drag text between two list boxes, an edit
control, and a CWnd-derived object's window. You can also use any other
application with OLE text drag and drop functionality enabled as a drag source
or a drop target (Word for Windows 6.x is one such application).

The following files are available for download from the Microsoft Download
Center:

Visual C++ 6.0:

  DownloadDownload Listdrg.exe now
  (http://download.microsoft.com/download/vc60pro/samp39/1/WIN98/EN-US/Lstdrg.exe)

For additional information about how to download Microsoft Support files, click
the following article number to view the article in the Microsoft Knowledge
Base:

  Q119591 How to Obtain Microsoft Support Files from Online Services

Microsoft scanned this file for viruses. Microsoft used the most current
virus-detection software that was available on the date that the file was
posted. The file is stored on secure servers that prevent any unauthorized
changes to the file.

Visual C++ .NET:

  DownloadDownload Listdrgvcnet.exe now
  (http://download.microsoft.com/download//VisualStudioNET/SAMPLE/1.16/WIN98MeXP/EN-US/Listdrgvcnet.exe)

Release Date: June 26, 2002

For additional information about how to download Microsoft Support files, click
the following article number to view the article in the Microsoft Knowledge
Base:

  Q119591 How to Obtain Microsoft Support Files from Online Services

Microsoft scanned this file for viruses. Microsoft used the most current
virus-detection software that was available on the date that the file was
posted. The file is stored on secure servers that prevent any unauthorized
changes to the file.


MORE INFORMATION
================

To enable OLE for a pre-existing MFC application, you must initialize the OLE
DLLs. This is done in the InitInstance() of your application object by calling
AfxOleInit().

The LSTDRG sample was first created with no OLE support using AppWizard. The code
that calls AfxOleInit() was then copied from InitInstance() of the OCLIENT MFC
sample application that is supplied with the above mentioned products.

OLE drag and drop functionality was enabled for each window by following the
procedure outlined in the MFC version 2.5 OLE 2 Classes documentation. Sections
of interest are:

- "Drag and Drop: Implementing a Drop Source"

- "Drag and Drop: Implementing a Drop Target"

- "Data Objects and Data Sources: Creation and Destruction"

- "Data Objects and Data Sources: Manipulation"

You will also want to review the MFC Classes COleDataSource and COleDropTarget.

To turn a CWnd-derived object into a drop source, you must instantiate a
COleDataSource object in your CWnd-derived object. You can then call
COleDataSource::CacheGlobalData() to cache the data (text) that you are going to
drag. Then call COleDataSource::DoDragDrop() to actually initiate the drag drop.
The return value from DoDragDrop() gives you the result of the drag/drop
operation. The DROPEFFECT return value from DoDragDrop() is a bit field, so you
have to test specific bits of the return value to determine the drag/drop
results.

Turning a CWnd-derived object into a drop target is a bit more complicated. You
must derive your own class from COleDropTarget, and instantiate a member of your
COleDropTarget-derived object in the CWnd-derived class. In your COleDropTarget
object you have to, at a minimum, override these four member functions:

  COleDropTarget::OnDragEnter()
  COleDropTarget::OnDragLeave()
  COleDropTarget::OnDragOver()
  COleDropTarget::OnDrop()

You must also initialize the COleDropTarget derived object as a drop target with
the OLE DLLs by calling COleDropTarget::Register(). This would normally be done
in the OnCreate member function of a CWnd-derived object (see the
CDDWnd::OnCreate method in LSTDRG). A problem in dialog template created classes
(CListBox, CEdit, and so on created by AppStudio) is that OnCreate is not
called. This is because control windows associated with the dialog template are
created during the dialog creation process before their actual CWnd-derived C++
objects are created. These windows are then attached by using Attached() to the
C++ CWnd-derived objects once they are created.

To work around this, an Initialize() method was created for the CListBox and
CEdit-derived classes where COleDropTarget::Register() is called. This
Initialize() method is then called for each template-created control in the
CFormView-derived classes OnInitialUpdate() method (see
CListdragView::OnInitialUpdate() and CDDListBox::Initialize()).

Another interesting aspect of the LSTDRG application is the use of a burning
trashcan animation when text is dropped on a window. This animation uses a
single bitmap composed of multiple 'cells' or images. The use of a single bitmap
as opposed to multiple bitmaps conserves GDI resources and is more efficient to
paint. It also turns off the mouse pointer (cursor) by loading an empty pointer
during painting to eliminate pointer flicker on Windows version 3.x computers.

REFERENCES
==========

"MFC 2.5 OLE 2 Classes" reference book, available in the Books Online of Visual
C++ version 1.5. See sections on drag and drop beginning with the section titled
"Drag and Drop." Also review the COleDataSource class and the COleDataTarget
class in the same reference book.

For good background information on how OLE handles drag and drop, please see
"Inside OLE 2" by Kraig Brockschmidt, published by Microsoft Press.

Additional query words: kbvc150 kbvc151 kbvc152 kbvc200 kbvc210 kbvc220 kbvc400 kbvc410 kbvc420 kbvc500 kbvc600 Listdrg Listdrgvcnet

======================================================================
Keywords          : kbfile kbProgramming kbSample kbCOMt kbDragDrop kbMFC kbVC150 kbVC200 kbVC400 kbVC500 kbVC600 kbGrpDSMFCATL 
Technology        : kbAudDeveloper kbMFC
Version           : :1.50,1.51,1.52,1.52b,2.0,2.1,2.2,4.0,4.1,4.2,5.0,6.0

=============================================================================

THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Copyright Microsoft Corporation 1986-2002.