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
How to set an item in CListCtrl as selected?
提问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)
andSetSelectionMark(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 SetItemState
function where you can pass -1
as its nItem
argument 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 SetItemState
function sends the LVM_SETITEMSTATE
message, and the nItem=-1 feature is documented on the message's reference documentationas its wParam
argument.
该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