1. 样式修改
2. 关键代码
BOOL CMFCApplication3Dlg :: OnInitDialog ( )
{
CDialogEx :: OnInitDialog ( ) ;
SetIcon ( m_hIcon, TRUE) ;
SetIcon ( m_hIcon, FALSE) ;
m_btnMoveDown. EnableWindow ( FALSE) ;
m_btnMoveUp. EnableWindow ( FALSE) ;
m_MyListCtrl. InsertColumn ( 0 , _T ( "测试" ) , 0 , 100 ) ;
return TRUE;
}
void CMFCApplication3Dlg :: OnBnClickedAddSomeItems ( )
{
static std:: default_random_engine dre ( unsigned int ( time ( 0 ) ) ) ;
std:: uniform_int_distribution< int > uid ( 100 , 999 ) ;
CString strValue;
strValue. Format ( _T ( "测试文本--%d" ) , uid ( dre) ) ;
int nSelMark = m_MyListCtrl. GetSelectionMark ( ) ;
int nInsertIndex = m_MyListCtrl. GetSelectionMark ( ) ;
if ( nInsertIndex < 0 )
{
nInsertIndex = m_MyListCtrl. GetItemCount ( ) - 1 ;
if ( nInsertIndex < 0 ) nInsertIndex = 0 ;
}
nInsertIndex += 1 ;
int nNewIndex = m_MyListCtrl. InsertItem ( nInsertIndex, strValue) ;
m_MyListCtrl. SetSelectionMark ( nNewIndex) ;
m_MyListCtrl. SetItemState ( nNewIndex,
LVIS_FOCUSED | LVIS_SELECTED, LVIS_FOCUSED | LVIS_SELECTED) ;
m_MyListCtrl. SetFocus ( ) ;
}
void CMFCApplication3Dlg :: OnBnClickedMoveDown ( )
{
int nSelIndex = m_MyListCtrl. GetSelectionMark ( ) ;
if ( nSelIndex < 0 || nSelIndex >= m_MyListCtrl. GetItemCount ( ) - 1 )
{
return ;
}
int nNextIndex = nSelIndex + 1 ;
CString strNextText = m_MyListCtrl. GetItemText ( nNextIndex, 0 ) ;
m_MyListCtrl. SetItemText ( nNextIndex, 0 , m_MyListCtrl. GetItemText ( nSelIndex, 0 ) ) ;
m_MyListCtrl. SetItemText ( nSelIndex, 0 , strNextText) ;
DWORD_PTR dwNextItemData = m_MyListCtrl. GetItemData ( nNextIndex) ;
m_MyListCtrl. SetItemData ( nNextIndex, m_MyListCtrl. GetItemData ( nSelIndex) ) ;
m_MyListCtrl. SetItemData ( nSelIndex, dwNextItemData) ;
m_MyListCtrl. SetSelectionMark ( nNextIndex) ;
m_MyListCtrl. SetItemState ( nNextIndex,
LVIS_FOCUSED | LVIS_SELECTED, LVIS_FOCUSED | LVIS_SELECTED) ;
m_MyListCtrl. SetFocus ( ) ;
}
void CMFCApplication3Dlg :: OnBnClickedMoveUp ( )
{
int nSelIndex = m_MyListCtrl. GetSelectionMark ( ) ;
if ( nSelIndex <= 0 )
{
return ;
}
int nPrevIndex = nSelIndex - 1 ;
CString strPrevText = m_MyListCtrl. GetItemText ( nPrevIndex, 0 ) ;
m_MyListCtrl. SetItemText ( nPrevIndex, 0 , m_MyListCtrl. GetItemText ( nSelIndex, 0 ) ) ;
m_MyListCtrl. SetItemText ( nSelIndex, 0 , strPrevText) ;
DWORD_PTR dwPrevItemData = m_MyListCtrl. GetItemData ( nPrevIndex) ;
m_MyListCtrl. SetItemData ( nPrevIndex, m_MyListCtrl. GetItemData ( nSelIndex) ) ;
m_MyListCtrl. SetItemData ( nSelIndex, dwPrevItemData) ;
m_MyListCtrl. SetSelectionMark ( nPrevIndex) ;
m_MyListCtrl. SetItemState ( nPrevIndex,
LVIS_FOCUSED | LVIS_SELECTED, LVIS_FOCUSED | LVIS_SELECTED) ;
m_MyListCtrl. SetFocus ( ) ;
}
void CMFCApplication3Dlg :: OnItemchangedList1 ( NMHDR * pNMHDR, LRESULT * pResult)
{
LPNMLISTVIEW pNMLV = reinterpret_cast < LPNMLISTVIEW> ( pNMHDR) ;
TRACE3 ( "nItem:%d, nNewState:%u, nOldState:%u\n" ,
pNMLV-> iItem, pNMLV-> uNewState, pNMLV-> uOldState) ;
if ( pNMLV-> uNewState & LVIS_SELECTED)
{
if ( pNMLV-> iItem == 0 )
{
m_btnMoveDown. EnableWindow ( TRUE) ;
m_btnMoveUp. EnableWindow ( FALSE) ;
}
else if ( pNMLV-> iItem == m_MyListCtrl. GetItemCount ( ) - 1 )
{
m_btnMoveDown. EnableWindow ( FALSE) ;
m_btnMoveUp. EnableWindow ( TRUE) ;
}
}
* pResult = 0 ;
}
3. 运行截图