본문 바로가기

API

MFC List Control 복사 붙여넣기

BOOL  CopyToClipboard( CListCtrl* pListCtrl, LPCTSTR lpszSeparator = _T( "\t" ) );


BOOL Class::CopyToClipboard( CListCtrl* pListCtrl, LPCTSTR lpszSeparator /*= _T( "\t" ) */ )

{

ASSERT(pListCtrl && ::IsWindow(pListCtrl->GetSafeHwnd()));


CString sResult;

POSITION pos = pListCtrl->GetFirstSelectedItemPosition();

if (!pos)

return TRUE;


CWaitCursor wait;

int nItem, nCount = 0;

int nColumn = 1;


if ((pListCtrl->GetStyle() & LVS_TYPEMASK) == LVS_REPORT &&

pListCtrl->GetExtendedStyle() & LVS_EX_FULLROWSELECT)

{

CHeaderCtrl* pHeader = pListCtrl->GetHeaderCtrl();

nColumn = pHeader ? pHeader->GetItemCount() : 1;

}


while (pos)

{

nItem = pListCtrl->GetNextSelectedItem(pos);

if (0 != nCount)

sResult += _T("\r\n");


for (int i = 0; i < nColumn; ++i)

{

sResult += pListCtrl->GetItemText(nItem, i);

if (i != nColumn - 1)

sResult += lpszSeparator;

}

++nCount;

}


if (pListCtrl->OpenClipboard())

{

EmptyClipboard();


int nLen = sResult.GetLength() * sizeof(WCHAR) + 1;

HGLOBAL hGlobal = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, nLen);

LPBYTE pGlobalData = (LPBYTE)GlobalLock(hGlobal);


USES_CONVERSION_EX;

CopyMemory(pGlobalData, T2CW_EX(sResult, _ATL_SAFE_ALLOCA_DEF_THRESHOLD), nLen);

SetClipboardData(CF_UNICODETEXT, hGlobal);


GlobalUnlock(hGlobal);

GlobalFree(hGlobal);


CloseClipboard();

return TRUE;

}

return FALSE;

}


출처 : http://blog.naver.com/PostView.nhn?blogId=yo9lee&logNo=100198894855