// 파일탐색기에서 드래그앤드롭한 파일의 이름을 구해서 리스트컨트롤에 추가
////////////////////////////////////////////
// FileDropListCtrl.h
#pragma once
#include <afxole.h>
////////////////////////////////////////////
// CFileDropTarget
class CFileDropTarget : public COleDropTarget
{
public:
CFileDropTarget(CWnd* pWnd){m_pParent = pWnd;}
~CFileDropTarget(){}
private:
CWnd* m_pParent;
private:
BOOL OnDrop(CWnd* pWnd, COleDataObject* pDataObject, DROPEFFECT dropEffect, CPoint point);
DROPEFFECT OnDragEnter(CWnd* pWnd, COleDataObject* pDataObject, DWORD dwKeyState, CPoint point);
void OnDragLeave(CWnd* pWnd);
DROPEFFECT OnDragOver(CWnd* pWnd, COleDataObject* pDataObject, DWORD dwKeyState, CPoint point);
DROPEFFECT OnDragScroll(CWnd* pWnd, DWORD dwKeyState, CPoint point);
public:
void SetParent(CWnd* pWnd) { m_pParent = pWnd;}
BOOL FileNamesToList( HDROP hDrop, CPoint point );
};
////////////////////////////////////////////
// CFileDropListCtrl
class CFileDropListCtrl : public CListCtrl
{
DECLARE_DYNAMIC(CFileDropListCtrl)
public:
CFileDropListCtrl();
virtual ~CFileDropListCtrl();
protected:
DECLARE_MESSAGE_MAP()
afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
private:
CFileDropTarget m_dropTarget;
public:
void FileDropRegister();
};
///////////////////////////////////////////////////
// FileDropListCtrl.cpp
//
#include "stdafx.h"
#include "FileDropListCtrl.h"
////////////////////////////////////////////////////
// CFileDropTarget
DROPEFFECT CFileDropTarget::OnDragScroll(CWnd* /*pWnd*/, DWORD /*dwKeyState*/, CPoint /*point*/)
{
// return DROPEFFECT_SCROLL | DROPEFFECT_COPY | DROPEFFECT_LINK;
return DROPEFFECT_SCROLL | DROPEFFECT_COPY;
}
DROPEFFECT CFileDropTarget::OnDragEnter(CWnd* /*pWnd*/, COleDataObject* /*pDataObject*/,
DWORD /*dwKeyState*/, CPoint /*point*/)
{
return DROPEFFECT_COPY;
}
void CFileDropTarget::OnDragLeave(CWnd* /*pWnd*/)
{
}
DROPEFFECT CFileDropTarget::OnDragOver(CWnd* /*pWnd*/, COleDataObject* /*pDataObject*/,
DWORD /*dwKeyState*/, CPoint /*point*/)
{
return DROPEFFECT_COPY;
}
BOOL CFileDropTarget::OnDrop(CWnd* /*pWnd*/, COleDataObject* pDataObject,
DROPEFFECT /*dropEffect*/, CPoint point)
{
try
{
if( pDataObject->IsDataAvailable( CF_HDROP ) )
{
STGMEDIUM StgMed;
FORMATETC fmte = {CF_HDROP, (DVTARGETDEVICE FAR *)NULL, DVASPECT_CONTENT, -1, TYMED_HGLOBAL};
if( pDataObject->GetData( CF_HDROP, &StgMed, &fmte ) )
{
if(! FileNamesToList( (HDROP)StgMed.hGlobal, point ) )
{
return FALSE;
} // if(! FileNamesToList( (HDROP)StgMed.hGlobal, point ) )
if (StgMed.pUnkForRelease)
{
StgMed.pUnkForRelease->Release();
}
else
{
::GlobalFree(StgMed.hGlobal);
} // if (StgMed.pUnkForRelease)...else
return TRUE;
} // if( pDataObject->GetData( CF_HDROP, &StgMed, &fmte ) )
} // if( pDataObject->IsDataAvailable( CF_HDROP ) )
} // try
catch(...)
{
return FALSE;
}
return TRUE;
}
BOOL CFileDropTarget::FileNamesToList( HDROP hDrop, CPoint point )
{
UINT cFiles = ::DragQueryFile(hDrop, (UINT)-1, NULL, 0);
if(cFiles <= 0)
{
return FALSE;
}
TCHAR szFile[MAX_PATH];
for( UINT count = 0; count < cFiles; count++ )
{
::DragQueryFile(hDrop, count, szFile, sizeof(szFile));
if( (m_pParent != NULL ) && ( ::IsWindow( m_pParent->GetSafeHwnd() ) != FALSE))
{
CListCtrl * pListCtrl = dynamic_cast<CListCtrl*>(m_pParent);
if( pListCtrl != NULL )
{
UINT uFlags;
int nItem = pListCtrl->HitTest(point, &uFlags);
CString strItem;
strItem.Format(_T("file=%s, nItem=%d, flag=0x%04x"),szFile, nItem, uFlags );
pListCtrl->InsertItem(0, strItem);
} // if( pListCtrl != NULL )
} // if( (m_pParent != NULL ) && ( ::IsWindow( m_pParent->GetSafeHwnd() ) != FALSE))
} // for( UINT count = 0; count < cFiles; count++ )
return TRUE;
}
////////////////////////////////////////////////////
// CFileDropListCtrl
IMPLEMENT_DYNAMIC(CFileDropListCtrl, CListCtrl)
CFileDropListCtrl::CFileDropListCtrl() : m_dropTarget(this)
{
}
CFileDropListCtrl::~CFileDropListCtrl()
{
}
BEGIN_MESSAGE_MAP(CFileDropListCtrl, CListCtrl)
ON_WM_CREATE()
END_MESSAGE_MAP()
// CFileDropListCtrl 메시지 처리기입니다.
int CFileDropListCtrl::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CListCtrl::OnCreate(lpCreateStruct) == -1)
return -1;
// TODO: 여기에 특수화된 작성 코드를 추가합니다.
FileDropRegister();
return 0;
}
void CFileDropListCtrl::FileDropRegister()
{
DragAcceptFiles();
m_dropTarget.Register(this);
}
////////////////////////////////////////////
// FileDropListCtrl.h
#pragma once
#include <afxole.h>
////////////////////////////////////////////
// CFileDropTarget
class CFileDropTarget : public COleDropTarget
{
public:
CFileDropTarget(CWnd* pWnd){m_pParent = pWnd;}
~CFileDropTarget(){}
private:
CWnd* m_pParent;
private:
BOOL OnDrop(CWnd* pWnd, COleDataObject* pDataObject, DROPEFFECT dropEffect, CPoint point);
DROPEFFECT OnDragEnter(CWnd* pWnd, COleDataObject* pDataObject, DWORD dwKeyState, CPoint point);
void OnDragLeave(CWnd* pWnd);
DROPEFFECT OnDragOver(CWnd* pWnd, COleDataObject* pDataObject, DWORD dwKeyState, CPoint point);
DROPEFFECT OnDragScroll(CWnd* pWnd, DWORD dwKeyState, CPoint point);
public:
void SetParent(CWnd* pWnd) { m_pParent = pWnd;}
BOOL FileNamesToList( HDROP hDrop, CPoint point );
};
////////////////////////////////////////////
// CFileDropListCtrl
class CFileDropListCtrl : public CListCtrl
{
DECLARE_DYNAMIC(CFileDropListCtrl)
public:
CFileDropListCtrl();
virtual ~CFileDropListCtrl();
protected:
DECLARE_MESSAGE_MAP()
afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
private:
CFileDropTarget m_dropTarget;
public:
void FileDropRegister();
};
///////////////////////////////////////////////////
// FileDropListCtrl.cpp
//
#include "stdafx.h"
#include "FileDropListCtrl.h"
////////////////////////////////////////////////////
// CFileDropTarget
DROPEFFECT CFileDropTarget::OnDragScroll(CWnd* /*pWnd*/, DWORD /*dwKeyState*/, CPoint /*point*/)
{
// return DROPEFFECT_SCROLL | DROPEFFECT_COPY | DROPEFFECT_LINK;
return DROPEFFECT_SCROLL | DROPEFFECT_COPY;
}
DROPEFFECT CFileDropTarget::OnDragEnter(CWnd* /*pWnd*/, COleDataObject* /*pDataObject*/,
DWORD /*dwKeyState*/, CPoint /*point*/)
{
return DROPEFFECT_COPY;
}
void CFileDropTarget::OnDragLeave(CWnd* /*pWnd*/)
{
}
DROPEFFECT CFileDropTarget::OnDragOver(CWnd* /*pWnd*/, COleDataObject* /*pDataObject*/,
DWORD /*dwKeyState*/, CPoint /*point*/)
{
return DROPEFFECT_COPY;
}
BOOL CFileDropTarget::OnDrop(CWnd* /*pWnd*/, COleDataObject* pDataObject,
DROPEFFECT /*dropEffect*/, CPoint point)
{
try
{
if( pDataObject->IsDataAvailable( CF_HDROP ) )
{
STGMEDIUM StgMed;
FORMATETC fmte = {CF_HDROP, (DVTARGETDEVICE FAR *)NULL, DVASPECT_CONTENT, -1, TYMED_HGLOBAL};
if( pDataObject->GetData( CF_HDROP, &StgMed, &fmte ) )
{
if(! FileNamesToList( (HDROP)StgMed.hGlobal, point ) )
{
return FALSE;
} // if(! FileNamesToList( (HDROP)StgMed.hGlobal, point ) )
if (StgMed.pUnkForRelease)
{
StgMed.pUnkForRelease->Release();
}
else
{
::GlobalFree(StgMed.hGlobal);
} // if (StgMed.pUnkForRelease)...else
return TRUE;
} // if( pDataObject->GetData( CF_HDROP, &StgMed, &fmte ) )
} // if( pDataObject->IsDataAvailable( CF_HDROP ) )
} // try
catch(...)
{
return FALSE;
}
return TRUE;
}
BOOL CFileDropTarget::FileNamesToList( HDROP hDrop, CPoint point )
{
UINT cFiles = ::DragQueryFile(hDrop, (UINT)-1, NULL, 0);
if(cFiles <= 0)
{
return FALSE;
}
TCHAR szFile[MAX_PATH];
for( UINT count = 0; count < cFiles; count++ )
{
::DragQueryFile(hDrop, count, szFile, sizeof(szFile));
if( (m_pParent != NULL ) && ( ::IsWindow( m_pParent->GetSafeHwnd() ) != FALSE))
{
CListCtrl * pListCtrl = dynamic_cast<CListCtrl*>(m_pParent);
if( pListCtrl != NULL )
{
UINT uFlags;
int nItem = pListCtrl->HitTest(point, &uFlags);
CString strItem;
strItem.Format(_T("file=%s, nItem=%d, flag=0x%04x"),szFile, nItem, uFlags );
pListCtrl->InsertItem(0, strItem);
} // if( pListCtrl != NULL )
} // if( (m_pParent != NULL ) && ( ::IsWindow( m_pParent->GetSafeHwnd() ) != FALSE))
} // for( UINT count = 0; count < cFiles; count++ )
return TRUE;
}
////////////////////////////////////////////////////
// CFileDropListCtrl
IMPLEMENT_DYNAMIC(CFileDropListCtrl, CListCtrl)
CFileDropListCtrl::CFileDropListCtrl() : m_dropTarget(this)
{
}
CFileDropListCtrl::~CFileDropListCtrl()
{
}
BEGIN_MESSAGE_MAP(CFileDropListCtrl, CListCtrl)
ON_WM_CREATE()
END_MESSAGE_MAP()
// CFileDropListCtrl 메시지 처리기입니다.
int CFileDropListCtrl::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CListCtrl::OnCreate(lpCreateStruct) == -1)
return -1;
// TODO: 여기에 특수화된 작성 코드를 추가합니다.
FileDropRegister();
return 0;
}
void CFileDropListCtrl::FileDropRegister()
{
DragAcceptFiles();
m_dropTarget.Register(this);
}