今天实现了一个关于CListBox取消高亮选中状态的功能:
点击任意内容行,内容行高亮显示,点击内容行外位置,取消当前高亮,
- <p> CRect rect;
- CPoint point;
- int nSelectIndex = 0;</p><p>
- 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
- }
- }*/</p><p> // 多行选中模式
- int nCount = m_ListBox.GetSelCount();
- CArray<int,int> aryListBoxSel;</p><p> 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 ;
- }
- }</p><p> for (int i = 0;i< nCount; i++)
- {
- m_ListBox.SetSel(aryListBoxSel[i],FALSE);
- }</p>