用代码实现clistctrl某行的选中
一个Report风格的CListCtrl怎么样通过程序而不是通过鼠标键盘来使某一项处于选中状态?
---------------------------------------------------------------
SetItemState(n, LVIS_SELECTED, LVIS_SELECTED);
---------------------------------------------------------------
//获得选中
int nItem = listctrl.GetNextItem (-1, LVNI_SELECTED);
//取消选中
listctrl.SetItemState(nItem,NULL,NULL);
//设置选中
listctrl.SetItemState(nItem+1,LVNI_SELECTED,LVNI_SELECTED);
---------------------------------------------------------------
SetItemState(n, LVIS_SELECTED, LVIS_SELECTED);
---------------------------------------------------------------
m_wndList.SetFocus();
m_wndList.SetItemState(2, LVIS_SELECTED, LVIS_SELECTED ¦ LVIS_FOCUSED);
m_wndList.EnsureVisible(2, FALSE);
::SendMessage(m_wndList.m_hWnd, LVM_SETEXTENDEDLISTVIEWSTYLE,
LVS_EX_FULLROWSELECT, LVS_EX_FULLROWSELECT);
---------------------------------------------------------------
以下的代码时再增加CListCtrl的行时,总是让最后一行是选中状态,看看对你有没有帮助
int nCount = m_list.GetItemCount();
if (nCount > 0)
{
m_list.EnsureVisible(nCount-1, FALSE);
m_list.SetItemState(nCount-1, LVIS_SELECTED, LVIS_SELECTED);
if ( nCount-1 > 0 )
{
m_list.SetItemState(nCount-1-1, 0, LVIS_SELECTED);
}
}
m_list.SetFocus();
---------------------------------------------------------------
SetItemState(n, LVIS_SELECTED, LVIS_SELECTED);
******************************************************
用代码实现不用不用鼠标选项也可以让某一些高亮选中;
void CMapHeadFileView::SeleteItem( int nindex )
{
m_listItem.SetFocus();
int nItem = m_listItem.GetNextItem (-1, LVNI_SELECTED); //表示获取上一次被设置点中的某项;
if( nItem >= 0 )
m_listItem.SetItemState( nItem , 0 ,-1 );//0表示不被选中,-1表示不高亮;
if ( nindex >= 0)
{
m_listItem.SetItemState( nindex , LVIS_SELECTED|LVIS_FOCUSED , LVIS_SELECTED|LVIS_FOCUSED );
m_listItem.SetFocus();
}
}