CListCtrl高亮显示某一项

来源:互联网 发布:数据库创建学生信息表 编辑:程序博客网 时间:2024/06/03 00:25

CListCtrl高亮显示某一项

  今天遇到一个小小的问题,就是高亮显示ListCtrl某一项,通过查阅MSDN,找到了相应函数,即:SetItemState,下面是MSDN中对该函数的相关说明.

--------------------------------------------------------------------------------------------------------------------

BOOL SetItemState(
   int nItem,
   LVITEM* pItem
);
BOOL SetItemState(
   int nItem,
   UINT nState,
   UINT nMask
);

Parameters

nItem
Index of the item whose state is to be set.
pItem
Address of an LVITEM structure, as described in the Platform SDK. The structure's stateMask member specifies which state bits to change, and the structure's state member contains the new values for those bits. The other members are ignored.
nState
New values for the state bits.
nMask
Mask specifying which state bits to change.

Return Value

Nonzero if successful; otherwise zero.

Remarks

An item's "state" is a value that specifies the item's availability, indicates user actions, or otherwise reflects the item's status. A list view control changes some state bits, such as when the user selects an item. An application might change other state bits to disable or hide the item, or to specify an overlay image or state image.

--------------------------------------------------------------------------------------------------------------------

函数参数很简单,在这里就不作介绍了,一般情况下用下面的代码会正常高亮显示你所想要高亮显示的项:

  m_listCtrl.SetItemState(0,   LVIS_SELECTED,   LVIS_SELECTED   |   LVIS_FOCUSED);  

但是有的时候你会发现这段代码并不好用!上网搜....网上也大都是这么说的,没错呀,再加上一行代码试试...

listCtrl.SetItemState(index,   LVIS_SELECTED,   LVIS_SELECTED   |   LVIS_FOCUSED);   
  listCtrl.EnsureVisible(index,   FALSE);  

还是不行!这是为什么呢?!其实原因很简单:ListCtrl没有获得焦点!!!简单吧,哈哈..."千里之堤溃于蚁穴"(用这句话形容好像不太合适,大概是这个意思吧,文笔不好,大家多多见谅(*^__^*) 嘻嘻……),问题虽小,但可以说明一些问题,有的时候这个小小的问题也可能耗费你很长时间(个人深有体会!),如果你也遇到了类似问题,看到了这篇文章后问题解决了,我会感到很高兴,我今晚熬夜也"熬而无憾了"!

  下面是我遇到该问题的情形:

  (详见图片)

..........

Over,May it can help  you!

原创粉丝点击