for example: void CSortListCtrl::CopyItem() { int iSel, iCurr, i, j; m_Copynum = 0; m_PasteFlag = 1; // copy flag iSel = CListCtrl::GetSelectedCount(); m_Copynum = iSel; if (iSel > MAX_COPYNUM) { MessageBox("The num of you copy is so large!","Phonebook Copy"); return; }
POSITION pos = CListCtrl::GetFirstSelectedItemPosition(); for (i=0; i<iSel; i++) { iCurr = CListCtrl::GetNextSelectedItem(pos); m_CopyInfo[i] = _T(""); for (j=0; j<m_iNumColumns; j++) { m_CopyInfo[i] += CListCtrl::GetItemText(iCurr, j) + "\t"; } } }void CSortListCtrl::PasteItem() { LV_ITEM lvItem; int i, iItem, nWord; UINT nPos, oldPos; CIVStringSet m_setstrings; CString strText[MAX_ITEMNUM], buf; char * text;
m_setstrings.Add("\t"); if (m_Copynum == 0) { return; } else { for (i=0; i<m_Copynum; i++) { iItem = 0; nPos = m_setstrings.FindFirstIn(m_CopyInfo[i], nWord); if (nPos != 0xFFFFFFFF) { do { if (iItem == 0) { strText[iItem] = m_CopyInfo[i].Left(nPos); } else { buf = m_CopyInfo[i].Left(nPos); strText[iItem] = buf.Right(nPos-oldPos-1); } iItem++; oldPos = nPos; nPos = m_setstrings.FindNext(nWord); } while (nPos != 0xFFFFFFFF); // additem text = new char[strText[0].GetLength()+1]; memset(text, 0, strText[0].GetLength()+1); sprintf(text, "%s", strText[0]); lvItem.mask = LVIF_IMAGE | LVIF_TEXT; lvItem.iImage=-1; lvItem.iItem =GetItemCount(); lvItem.iSubItem =0; lvItem.pszText = (LPTSTR) text; const int iIndex = CListCtrl::InsertItem(&lvItem); LPTSTR* arrpsz = new LPTSTR[ iItem ]; arrpsz[ 0 ] = new TCHAR[ lstrlen( text ) + 1 ]; (void)lstrcpy( arrpsz[ 0 ], text ); delete text;
for( int iColumn = 1; iColumn < iItem; iColumn++ ) { text = new char[strText[iColumn].GetLength()+1]; memset(text, 0, strText[iColumn].GetLength()+1); sprintf(text, "%s", strText[iColumn]); ASSERT_VALID_STRING( text ); VERIFY( CListCtrl::SetItem( iIndex, iColumn, LVIF_TEXT , text, 0, 0, 0, 0 ) ); arrpsz[ iColumn ] = new TCHAR[ lstrlen( text ) + 1 ]; (void)lstrcpy( arrpsz[ iColumn ], text ); delete text; } VERIFY( SetTextArray( iIndex, arrpsz ) ); } } } if (m_PasteFlag == 2) { m_Copynum = 0; } }
void CSortListCtrl::CopyItem()
{
int iSel, iCurr, i, j; m_Copynum = 0;
m_PasteFlag = 1; // copy flag
iSel = CListCtrl::GetSelectedCount();
m_Copynum = iSel;
if (iSel > MAX_COPYNUM)
{
MessageBox("The num of you copy is so large!","Phonebook Copy");
return;
}
POSITION pos = CListCtrl::GetFirstSelectedItemPosition();
for (i=0; i<iSel; i++)
{
iCurr = CListCtrl::GetNextSelectedItem(pos);
m_CopyInfo[i] = _T("");
for (j=0; j<m_iNumColumns; j++)
{
m_CopyInfo[i] += CListCtrl::GetItemText(iCurr, j) + "\t";
}
}
}void CSortListCtrl::PasteItem()
{
LV_ITEM lvItem;
int i, iItem, nWord;
UINT nPos, oldPos;
CIVStringSet m_setstrings;
CString strText[MAX_ITEMNUM], buf;
char * text;
m_setstrings.Add("\t");
if (m_Copynum == 0)
{
return;
}
else
{
for (i=0; i<m_Copynum; i++)
{
iItem = 0;
nPos = m_setstrings.FindFirstIn(m_CopyInfo[i], nWord);
if (nPos != 0xFFFFFFFF)
{
do
{
if (iItem == 0)
{
strText[iItem] = m_CopyInfo[i].Left(nPos);
}
else
{
buf = m_CopyInfo[i].Left(nPos);
strText[iItem] = buf.Right(nPos-oldPos-1);
}
iItem++;
oldPos = nPos;
nPos = m_setstrings.FindNext(nWord);
}
while (nPos != 0xFFFFFFFF);
// additem
text = new char[strText[0].GetLength()+1];
memset(text, 0, strText[0].GetLength()+1);
sprintf(text, "%s", strText[0]);
lvItem.mask = LVIF_IMAGE | LVIF_TEXT;
lvItem.iImage=-1;
lvItem.iItem =GetItemCount();
lvItem.iSubItem =0;
lvItem.pszText = (LPTSTR) text;
const int iIndex = CListCtrl::InsertItem(&lvItem); LPTSTR* arrpsz = new LPTSTR[ iItem ];
arrpsz[ 0 ] = new TCHAR[ lstrlen( text ) + 1 ];
(void)lstrcpy( arrpsz[ 0 ], text );
delete text;
for( int iColumn = 1; iColumn < iItem; iColumn++ )
{
text = new char[strText[iColumn].GetLength()+1];
memset(text, 0, strText[iColumn].GetLength()+1);
sprintf(text, "%s", strText[iColumn]);
ASSERT_VALID_STRING( text );
VERIFY( CListCtrl::SetItem( iIndex, iColumn, LVIF_TEXT , text, 0, 0, 0, 0 ) );
arrpsz[ iColumn ] = new TCHAR[ lstrlen( text ) + 1 ];
(void)lstrcpy( arrpsz[ iColumn ], text );
delete text;
} VERIFY( SetTextArray( iIndex, arrpsz ) );
}
}
}
if (m_PasteFlag == 2)
{
m_Copynum = 0;
}
}
GetColumn()
GetItemText();然后设置
SetItemText();
SetColumn();