메뉴 바로가기

서브메뉴 바로가기

본문 바로가기

logo

C/C++/MFC

FILE 파일 경로명 복사 - 클립 보드로 복사 - ctrl+C Ctrl+V:파일명 보여줌.

2006.05.23 13:37

WAT_운영자 조회 수:19201 추천:1

#include <afxole.h>


BOOL CMyListView::PrepareFileBuff(CDropFiles &DropFiles)
{
CListCtrl& mylist = GetListCtrl();
POSITION pos = mylist.GetFirstSelectedItemPosition();

CString strPath;
if( pos == NULL){
  return FALSE;
}

while (pos) {  
  int index = mylist.GetNextSelectedItem(pos);
  strPath=GetItemPath(index);
//  DropFiles.AddFile( mylist.GetItemText(index, 0) );
  DropFiles.AddFile( strPath );
}

DropFiles.CreateBuffer();

return TRUE;

}




void CMyListView::OnEditCopy()
//복사하기
{
CDropFiles DropFiles;

if(!PrepareFileBuff(DropFiles)){
  return;
}

if (OpenClipboard())
{
  BeginWaitCursor();
  ::EmptyClipboard();
  
  HGLOBAL hMem = ::GlobalAlloc(GMEM_ZEROINIT|GMEM_MOVEABLE|GMEM_DDESHARE, DropFiles.GetBuffSize());  
  memcpy( (char*)::GlobalLock(hMem), DropFiles.GetBuffer(), DropFiles.GetBuffSize() );
  ::SetClipboardData (CF_HDROP, hMem  );

  ::CloseClipboard();
  EndWaitCursor();
}

}





--------------------------------------------------------------------------------------------------------

클립 보드로 부터 경로명 얻어오기

COleDataObject odj;
if( odj.AttachClipboard() )
{
if( odj.IsDataAvailable( CF_HDROP ) )
{
  STGMEDIUM StgMed;
  FORMATETC fmte = { CF_HDROP, (DVTARGETDEVICE FAR *)NULL, DVASPECT_CONTENT, -1, TYMED_HGLOBAL };
  if( odj.GetData( CF_HDROP, &StgMed, &fmte ) )
  {
   HDROP hdrop = (HDROP)StgMed.hGlobal;
   UINT cFiles = ::DragQueryFile(hdrop, (UINT)-1, NULL, 0);

   CString szText;
   szText.Format( \"There are %d files/directories\\r\\n\", cFiles );

   char szFile[MAX_PATH];

   for( UINT count = 0; count < cFiles; count++ )
   {
    ::DragQueryFile(hdrop, count, szFile, sizeof(szFile));
    szText += szFile;
    szText += \"\\r\\n\";
   }
   if (StgMed.pUnkForRelease)
   {
    StgMed.pUnkForRelease->Release();
   }
   else
   {
    ::GlobalFree(StgMed.hGlobal);
   }
  }
}
}