CListBox取消高亮选中状态的功能:

来源:互联网 发布:德拉蒙德格林数据虎扑 编辑:程序博客网 时间:2024/05/24 05:12

今天实现了一个关于CListBox取消高亮选中状态的功能:

点击任意内容行,内容行高亮显示,点击内容行外位置,取消当前高亮,

[cpp] view plaincopy
  1. <p> CRect rect;  
  2.  CPoint point;  
  3.  int nSelectIndex = 0;</p><p>  
  4.  GetCursorPos(&point);//获取当前鼠标位置,相对屏幕坐标  
  5.  ::MapWindowPoints(NULL,m_ListBox.m_hWnd,&point,1);//将屏幕坐标转换为控件中坐标  
  6.    
  7.  //单行选中模式  
  8.  /*nSelectIndex = m_ListBox.GetCurSel();//获取当前选中行 
  9.  if (nSelectIndex != LB_ERR) 
  10.  { 
  11.   m_ListBox.GetItemRect(nSelectIndex,&rect);//获取当前选中行矩形 
  12.   if (!rect.PtInRect(point))//判断是否在选中行坐标中 
  13.   { 
  14.    m_ListBox.SetCurSel(-1);//如果不在,设置当前选中行为-1 
  15.   }  
  16.  }*/</p><p> // 多行选中模式  
  17.  int nCount = m_ListBox.GetSelCount();  
  18.  CArray<int,int> aryListBoxSel;</p><p> if (nCount == LB_ERR || nCount < 1)  
  19.  {  
  20.   return ;  
  21.  }  
  22.    
  23.  aryListBoxSel.SetSize(nCount);  
  24.  m_ListBox.GetSelItems(nCount, aryListBoxSel.GetData());   
  25.  for (int i = 0;i< nCount; i++)  
  26.  {  
  27.   m_ListBox.GetItemRect(aryListBoxSel[i],&rect);  
  28.   if (rect.PtInRect(point))  
  29.   {  
  30.    return ;  
  31.   }  
  32.  }</p><p> for (int i = 0;i< nCount; i++)  
  33.  {  
  34.   m_ListBox.SetSel(aryListBoxSel[i],FALSE);  
  35.  }</p>  
0 0
原创粉丝点击