简体   繁体   English

CListCtrl选择

[英]CListCtrl selection

I'm trying to do something that I think is simple but I can't seem to make it work! 我正在尝试做一些我认为很简单的事情,但我似乎无法让它发挥作用!

I have a CListCtrl and I want to select the last element in the list if the user clicks in the view empty space. 我有一个CListCtrl ,如果用户在视图空白区域中单击,我想选择列表中的最后一个元素。 I can do that just by calling Select(lastElementInList) , but the element that was previously selected and that is now unselected still has a "bounding rectangle" around it. 我只能通过调用Select(lastElementInList)来做到这一点,但是之前选择的元素以及现在未被选中的元素周围仍然有一个“边界矩形”。

The code that Implements this is as follows: 实现此的代码如下:

    int nSel = GetNextItem(-1, LVNI_SELECTED);
    if (nSel != -1)
        SetItemState(nSel, 0, LVIS_SELECTED);

    Select(lastElementInList);

Any hints? 任何提示? What am I missing? 我错过了什么?

The "bounding rectangle" you see indicates that the element currently is "focused" , ie. 您看到的“边界矩形”表示该元素当前是“聚焦”的 ,即。 in a state where a user interaction, such as pressing the down and up arrows, would start off from this point. 在这种状态下,用户交互(例如按下向上和向上箭头)将从这一点开始。


Change focused element 改变集中的元素

To move focus to your newly selected element you'll have to use SetItemState together with LVIS_FOCUSED , as in the below example: 要将焦点移动到新选择的元素,您必须与LVIS_FOCUSED一起使用SetItemState ,如下例所示:

if (nSel != -1)
    SetItemState (nSel, ~LVIS_FOCUSED, LVIS_FOCUSED);          // (1)

SetItemState (lastElementInList, LVIS_FOCUSED, LVIS_FOCUSED);  // (2)

// (1) -> Remove focus from `nSel`
// (2) -> Add focus to `lastElementInList`

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM