之前遇到过,搜了半天,没记录,今天又遇到了,又搜了半天。
当你希望打开对话框时,CListCtrl默认选择一项时,这个很有用。
不知道MFC为什么这么简单的功能要搞这么复杂。
可能认识不够吧,总结点是点。
三个要点:
一、 Always Show Selection:False
当你希望打开对话框时,CListCtrl默认选择一项时,这个很有用。
不知道MFC为什么这么简单的功能要搞这么复杂。
可能认识不够吧,总结点是点。
三个要点:
一、 Always Show Selection:False
总是显示选定内容,即使控件没有焦点。
三、响应
NM_CUSTOMDRAW
---------------------------------------
OnInitDialog();
NM_CUSTOMDRAW
url:
http://greatverve.cnblogs.com/archive/2013/02/25/clistctrl-highlight.html
View:Report
二、一定要设置焦点
//
设置选中第二行
m_lstTest.SetFocus();
m_lstTest.SetItemState( 1,LVIS_SELECTED|LVIS_FOCUSED, LVIS_SELECTED|LVIS_FOCUSED);
m_lstTest.SetFocus();
m_lstTest.SetItemState( 1,LVIS_SELECTED|LVIS_FOCUSED, LVIS_SELECTED|LVIS_FOCUSED);
---------------------------------------
OnInitDialog();
m_lstTest.InsertColumn(
0,
"
column0
");
m_lstTest.InsertColumn( 1, " column1 ");
m_lstTest.SetColumnWidth( 0, 80);
m_lstTest.SetColumnWidth( 1, 80);
m_lstTest.InsertItem( 0, " row0 ");
m_lstTest.InsertItem( 1, " row1 ");
m_lstTest.SetItemText( 0, 1, " row01 ");
m_lstTest.SetItemText( 1, 1, " row11 ");
// 设置选中第二行
m_lstTest.SetFocus();
m_lstTest.SetItemState( 1,LVIS_SELECTED|LVIS_FOCUSED, LVIS_SELECTED|LVIS_FOCUSED);
m_lstTest.InsertColumn( 1, " column1 ");
m_lstTest.SetColumnWidth( 0, 80);
m_lstTest.SetColumnWidth( 1, 80);
m_lstTest.InsertItem( 0, " row0 ");
m_lstTest.InsertItem( 1, " row1 ");
m_lstTest.SetItemText( 0, 1, " row01 ");
m_lstTest.SetItemText( 1, 1, " row11 ");
// 设置选中第二行
m_lstTest.SetFocus();
m_lstTest.SetItemState( 1,LVIS_SELECTED|LVIS_FOCUSED, LVIS_SELECTED|LVIS_FOCUSED);
void CmfcDlgDlg::OnNMCustomdrawList(NMHDR *pNMHDR, LRESULT *pResult)
{
NMLVCUSTOMDRAW* pLVCD = reinterpret_cast<NMLVCUSTOMDRAW*>( pNMHDR );
*pResult = CDRF_DODEFAULT;
if ( CDDS_PREPAINT == pLVCD->nmcd.dwDrawStage )
{
*pResult = CDRF_NOTIFYITEMDRAW;
}
else if ( CDDS_ITEMPREPAINT == pLVCD->nmcd.dwDrawStage )
{
*pResult = CDRF_NOTIFYSUBITEMDRAW;
}
else if ( (CDDS_ITEMPREPAINT | CDDS_SUBITEM) == pLVCD->nmcd.dwDrawStage )
{
COLORREF clrNewTextColor, clrNewBkColor;
int nItem = static_cast< int>( pLVCD->nmcd.dwItemSpec );
POSITION pos = m_lstTest.GetFirstSelectedItemPosition();
int index = m_lstTest.GetNextSelectedItem(pos);
if (index == nItem) // 如果要刷新的项为当前选择的项,则将文字设为白色,背景色设为蓝色
{
clrNewTextColor = RGB( 255, 255, 255); // Set the text to white
clrNewBkColor = RGB( 49, 106, 197); // Set the background color to blue
}
else
{
clrNewTextColor = RGB( 0, 0, 0); // set the text black
clrNewBkColor = RGB( 255, 255, 255); // leave the background color white
}
pLVCD->clrText = clrNewTextColor;
pLVCD->clrTextBk = clrNewBkColor;
*pResult = CDRF_DODEFAULT;
}
}
{
NMLVCUSTOMDRAW* pLVCD = reinterpret_cast<NMLVCUSTOMDRAW*>( pNMHDR );
*pResult = CDRF_DODEFAULT;
if ( CDDS_PREPAINT == pLVCD->nmcd.dwDrawStage )
{
*pResult = CDRF_NOTIFYITEMDRAW;
}
else if ( CDDS_ITEMPREPAINT == pLVCD->nmcd.dwDrawStage )
{
*pResult = CDRF_NOTIFYSUBITEMDRAW;
}
else if ( (CDDS_ITEMPREPAINT | CDDS_SUBITEM) == pLVCD->nmcd.dwDrawStage )
{
COLORREF clrNewTextColor, clrNewBkColor;
int nItem = static_cast< int>( pLVCD->nmcd.dwItemSpec );
POSITION pos = m_lstTest.GetFirstSelectedItemPosition();
int index = m_lstTest.GetNextSelectedItem(pos);
if (index == nItem) // 如果要刷新的项为当前选择的项,则将文字设为白色,背景色设为蓝色
{
clrNewTextColor = RGB( 255, 255, 255); // Set the text to white
clrNewBkColor = RGB( 49, 106, 197); // Set the background color to blue
}
else
{
clrNewTextColor = RGB( 0, 0, 0); // set the text black
clrNewBkColor = RGB( 255, 255, 255); // leave the background color white
}
pLVCD->clrText = clrNewTextColor;
pLVCD->clrTextBk = clrNewBkColor;
*pResult = CDRF_DODEFAULT;
}
}
本文转自wenglabs博客园博客,原文链接:http://www.cnblogs.com/greatverve/archive/2013/02/25/clistctrl-highlight.html
,如需转载请自行联系原作者