Click here to Skip to main content
12,897,945 members (87,262 online)
Click here to Skip to main content


107 bookmarked
Posted 16 Jun 2006

WTL for MFC Programmers, Part X - Implementing a Drag and Drop Source

, 16 Jun 2006
A tutorial on using drag and drop in your WTL application.
// stdafx.h : include file for standard system include files,
//  or project specific include files that are used frequently, but
//      are changed infrequently

#if !defined(AFX_STDAFX_H__1AB5E883_E5CD_4C40_82A5_E272A27176C9__INCLUDED_)
#define AFX_STDAFX_H__1AB5E883_E5CD_4C40_82A5_E272A27176C9__INCLUDED_

// Change these values to use different versions
#define WINVER          0x0500
#define _WIN32_WINNT    0x0501
#define _WIN32_IE       0x0600

#define _ATL_NO_MSIMG
#define _ATL_NO_OPENGL

#include <atlbase.h>
#include <atlapp.h>
extern CAppModule _Module;
#include <atlcom.h>
#include <atlwin.h>
#include <atlframe.h>
#include <atlcrack.h>
#include <atlmisc.h>
#include <atlctrls.h>
#include <atlctrlx.h>
#include <atldlgs.h>

// STL
#include <vector>

// CRT
#include <io.h>
#include <fcntl.h>
#include <sys/stat.h>

// Win32
#include <shellapi.h>
#include <commoncontrols.h>     // IImageList definitions

// Other libraries

// If you get an error here, you need to have the CAB SDK installed in the same
// dir as the source, in a dir called "cabsdk".
#include "cabsdk/include/fci.h"
#include "cabsdk/include/fdi.h"

// Global data
extern bool g_bXPOrLater;
extern std::vector<CString> g_vecsTempFiles;
const LPCTSTR APP_SETTINGS_KEY = _T("software\\Mike's Classy Software\\WTLCabView");

// Types
struct CCompressedFileInfo
    CString  sFilename;     // name of the file as stored in the CAB
    CString  sFileType;     // descriptio of the file type
    DWORD    dwFileSize;    // uncompressed size of the file
    FILETIME ftDateTime;    // modified date/time of the file
    UINT     uAttribs;      // file attributes
    enum { from_prev_cab, in_this_cab, to_next_cab } location;
    CString  sOtherCabName; // name of the prev/next CAB
    bool     bExtractable;  // true => we can extract this file

    CCompressedFileInfo() :
        dwFileSize(0), uAttribs(0), location(in_this_cab), bExtractable(true)
    { }

struct CDraggedFileInfo
    // Data set at the beginning of a drag/drop:
    CString sFilename;      // name of the file as stored in the CAB
    CString sTempFilePath;  // path to the file we extract from the CAB
    int nListIdx;           // index of this item in the list ctrl

    // Data set while extracting files:
    bool bPartialFile;      // true if this file is continued in another cab
    CString sCabName;       // name of the CAB file
    bool bCabMissing;       // true if the file is partially in this cab and
                            // the CAB it's continued in isn't found, meaning
                            // the file can't be extracted

    CDraggedFileInfo ( const CString& s, int n ) :
        sFilename(s), nListIdx(n), bPartialFile(false), bCabMissing(false)
    { }

// Version of CComObjectStack that doesn't freak out and assert when IUnknown
// methods are called.
template <class Base>
class CComObjectStack2 : public CComObjectStack<Base>
    CComObjectStack2() : CComObjectStack<Base>() { }

    STDMETHOD_(ULONG, AddRef)() { return 1; }
    STDMETHOD_(ULONG, Release)() { return 1; }

    STDMETHOD(QueryInterface)(REFIID iid, void ** ppvObject)
        { return _InternalQueryInterface(iid, ppvObject); }

// Convenience macros
#define countof(x) (sizeof(x)/sizeof((x)[0]))
#define _S(x) (CString(LPCTSTR(x)))

#if _ATL_VER < 0x0700

// Microsoft Visual C++ will insert additional declarations immediately before the previous line.

#endif // !defined(AFX_STDAFX_H__1AB5E883_E5CD_4C40_82A5_E272A27176C9__INCLUDED_)

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.


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

Michael Dunn
Software Developer (Senior) VMware
United States United States
Michael lives in sunny Mountain View, California. He started programming with an Apple //e in 4th grade, graduated from UCLA with a math degree in 1994, and immediately landed a job as a QA engineer at Symantec, working on the Norton AntiVirus team. He pretty much taught himself Windows and MFC programming, and in 1999 he designed and coded a new interface for Norton AntiVirus 2000.
Mike has been a a developer at Napster and at his own lil' startup, Zabersoft, a development company he co-founded with offices in Los Angeles and Odense, Denmark. Mike is now a senior engineer at VMware.

He also enjoys his hobbies of playing pinball, bike riding, photography, and Domion on Friday nights (current favorite combo: Village + double Pirate Ship). He would get his own snooker table too if they weren't so darn big! He is also sad that he's forgotten the languages he's studied: French, Mandarin Chinese, and Japanese.

Mike was a VC MVP from 2005 to 2009.

You may also be interested in...

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170424.1 | Last Updated 16 Jun 2006
Article Copyright 2006 by Michael Dunn
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid