今天实现了一个关于CListBox取消高亮选中状态的功能:
点击任意内容行,内容行高亮显示,点击内容行外位置,取消当前高亮,
CRect rect;
CPoint point;
int nSelectIndex = 0;
GetCursorPos(&point);//获取当前鼠标位置,相对屏幕坐标
::MapWindowPoints(NULL,m_ListBox.m_hWnd,&point,1);//将屏幕坐标转换为控件中坐标
//单行选中模式
/*nSelectIndex = m_ListBox.GetCurSel();//获取当前选中行
if (nSelectIndex != LB_ERR)
{
m_ListBox.GetItemRect(nSelectIndex,&rect);//获取当前选中行矩形
if (!rect.PtInRect(point))//判断是否在选中行坐标中
{
m_ListBox.SetCurSel(-1);//如果不在,设置当前选中行为-1
}
}*/
// 多行选中模式
int nCount = m_ListBox.GetSelCount();
CArray<int,int> aryListBoxSel;
if (nCount == LB_ERR || nCount < 1)
{
return ;
}
aryListBoxSel.SetSize(nCount);
m_ListBox.GetSelItems(nCount, aryListBoxSel.GetData());
for (int i = 0;i< nCount; i++)
{
m_ListBox.GetItemRect(aryListBoxSel[i],&rect);
if (rect.PtInRect(point))
{
return ;
}
}
for (int i = 0;i< nCount; i++)
{
m_ListBox.SetSel(aryListBoxSel[i],FALSE);
}
上面代码很清楚了,就不多写了,资源中有对应的资源,可以下载查看运行效果!
资源连接:http://download.csdn.net/detail/daiafei/4457339