SAMPLE: Using MFC OLE Drag & Drop to Drag Text Between WindowsID: Q135299
|
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 file is available for download from the Microsoft Software
Library:
~ Lstdrg.exeFor more information about downloading files from the Microsoft Software Library, please see the following article in the Microsoft Knowledge Base:
Q119591 How to Obtain Microsoft Support Files from Online Services
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:
COleDropTarget::OnDragEnter()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.
COleDropTarget::OnDragLeave()
COleDropTarget::OnDragOver()
COleDropTarget::OnDrop()
"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 kbvc152 kbvc200 kbvc210 kbvc220 kbvc400 kbvc410 kbvc420 kbvc500 kbvc600
Keywords : kbole kbprg kbsample kbMFC kbVC
Version : 2.0 2.1 2.2 4.0 4.1 4.2 5.0 6.0
Platform : NT WINDOWS
Issue type :
Last Reviewed: July 15, 1999