StickysoftDESIGNS
Skip Navigation Links : Articles : WTL : Dropfile target in WTL

How to make your application a dropfile target

By dropfile target I mean the application that can process file names dropped into it. To modify your application to become a dropfile target all you have to do is to create your window with the extended window style WS_EX_ACCEPTFILES and handle WM_DROPFILES message. Another way is to create a window and then call the following API function:
DragAcceptFiles(HWND hWnd, BOOL bAccept)
that turns the WS_EX_ACCEPTFILES style on or off based on the value of the bAccept parameter.

The obvious solution would be to create a class to encapsulate the above mentioned logic. So I wrote a class CDropFileTarget that does just that.

How to use it

1. Add CDropFileTarget to the inheritance chain.

class CMainDlg : public CDialogImpl<CMainDlg>,
            public CUpdateUI<CMainDlg>,
            public CMessageFilter, 
            public CIdleHandler,
            public CDropFileTarget<CMainDlg>
    

2. Add CHAIN_MSG_MAP statement to the message map of your class.

BEGIN_MSG_MAP(CMainDlg)
    MESSAGE_HANDLER(WM_INITDIALOG, OnInitDialog)
    COMMAND_ID_HANDLER(ID_APP_ABOUT, OnAppAbout)
    COMMAND_ID_HANDLER(IDOK, OnOK)
    COMMAND_ID_HANDLER(IDCANCEL, OnCancel)
    CHAIN_MSG_MAP(CDropFileTarget<CMainDlg>)
END_MSG_MAP()
    

3. In OnInitDialog() function register your class as a dropfile target.

LRESULT OnInitDialog(UINT, WPARAM, LPARAM, BOOL&)
{
    ...
    RegisterDropTarget();
    ...
}
    

4. Add the following public function to process the dropped file.

void ProcessFile(LPCTSTR lpszPath)
{
    // Add your logic
}
    
Download the source code