C++ 如何将 CListCtrl 中的项目设置为选中状态?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1729260/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 21:02:06  来源:igfitidea点击:

How to set an item in CListCtrl as selected?

c++winapiselectionclistctrl

提问by DarshanG

ClistCtrl is set to single selection & single column in report view with no header.

ClistCtrl 在没有标题的报告视图中设置为单选和单列。

I have tried SetItemState(0,LVIS_SELECTED,LVIF_STATE)and
SetSelectionMark(int index)but these don't work.

我试过了SetItemState(0,LVIS_SELECTED,LVIF_STATE)
SetSelectionMark(int index)但这些都不起作用。

回答by EdM

you also need to call SetSelectionMark after updating the item states.

您还需要在更新项目状态后调用 SetSelectionMark。

SetItemState(prev_item, ~LVIS_SELECTED, LVIS_SELECTED);
SetItemState(new_selected_item, LVIS_SELECTED, LVIS_SELECTED);
SetSelectionMark(new_selected_item);

回答by Nuri Crayton

Use SetItemState(0, LVIS_SELECTED, LVIS_SELECTED) to highlight, and SetItemState(0, ~LVIS_SELECTED, LVIS_SELECTED) to unhighlight.

使用 SetItemState(0, LVIS_SELECTED, LVIS_SELECTED) 突出显示,使用 SetItemState(0, ~LVIS_SELECTED, LVIS_SELECTED) 取消突出显示。

回答by timyau

Just Set Properties->Appearance->Always Show Selectionto TRUE

只需设置Properties->Appearance->Always Show Selectionto TRUE

回答by sergiol

I think there is an undocumented feature of the SetItemStatefunction where you can pass -1as its nItemargument to make the function act on allitems.

我认为该SetItemState函数有一个未记录的功能,您可以将-1其作为nItem参数传递,以使该函数作用于所有项目。

So, if you only want to select only one, I can suggest:

所以,如果你只想选择一个,我可以建议:

    SetItemState(-1, 0, LVIS_SELECTED);
    SetItemState(index, LVIS_SELECTED, LVIS_SELECTED);


UPDATE:

更新:

(after seeing http://www.verycomputer.com/417_11fcb075491b88c9_1.htm#p3)

(看到http://www.verycomputer.com/417_11fcb075491b88c9_1.htm#p3 后

The SetItemStatefunction sends the LVM_SETITEMSTATEmessage, and the nItem=-1 feature is documented on the message's reference documentationas its wParamargument.

SetItemState函数发送LVM_SETITEMSTATE消息,并且 nItem=-1 特性作为其参数记录在消息的参考文档wParam

回答by user9322513

If you wont set selected item in OnInitialDialog you mast use this code:

如果您不会在 OnInitialDialog 中设置所选项目,则必须使用以下代码:

m_pSPSMapList->EnsureVisible(nItem, FALSE);
m_pSPSMapList->SetFocus();
m_pSPSMapList->SetItemState(nItem, LVIS_FOCUSED | LVIS_SELECTED, 
                                   LVIS_FOCUSED | LVIS_SELECTED);

return FALSE; // OnInitialDialog necessarily must return false