본문 바로가기
MFC

Select, Deselect a CListCtrl Item Programmatically

by leo21c 2018. 6. 18.
SMALL

프로그래밍으로 CLIstCtrl의 Item을 선택 또는 해제하고 Highlight 처리하는 방법.

구글에서 이것 저것 다 찾아 봤다.


SetItemState 함수를 이용해서 처리한다고 여러곳에 설명되어 있는데 내가 원하는 것처럼 되지 않았다.

내가 원하는 것은 Up/Down 버튼으로 List의 선택한 Row를 바꾸는 것이다.


예를 들어 1st Row가 Select 되어 있을 때 Down 버튼을 누르면 1st Row의 Highlight가 사라지고 2nd Row가 선택되어 Highlight가 되는 것이다.

아래 함수처럼 Down 버튼을 클릭하면 버튼에 포커스가 이동된다. 따라서 현재 선택된 Item을 해제하고 다음 Item을 선택해서 Highlight를 이동하게 한다.

그리고 ListCtrl에 포커스를 줘서 Highlight가 표시되도록 한다.


int current_item_index = -1;

CListCtrl m_ListCtrl;


void CListCtrlView::OnGridDownClick()

{

INT_PTR nRowCnt = m_ListCtrl.GetItemCount();

        current_item_index++;       /// 현재 index에서 증감.

if (current_item_index > nRowCnt )   /// 마지막 Row보다 크면 마지막 Row를 선택한 상태로 유지

{

current_item_index= nRowCnt  - 1;

m_ListCtrl.SetItemState(current_item_index, LVIS_SELECTED, LVIS_SELECTED); 

m_ListCtrl.SetFocus();

return;

}


if (current_item_index != -1)

{

/// index에 맞는 특정 함수 실행.

}

m_ListCtrl.SetItemState(current_item_index - 1, 0, LVIS_SELECTED);              ///이전 라인을 선택 해제 한다.

m_ListCtrl.SetItemState(current_item_index, LVIS_SELECTED, LVIS_SELECTED); ///다음 라인을 선택 한다.

m_ListCtrl.SetFocus();        /// 버튼을 클릭해서 포커스가 버튼으로 이동 되었기 때문에 ListCtrl에 다시 포커스를 준다.

}


LIST

'MFC' 카테고리의 다른 글

View Background Color(배경색) 변경하기  (0) 2018.06.22
Detect a CListCtrl selection change  (0) 2018.06.18
Bitmap 리소스로 Toolbar 만들기  (0) 2018.05.28
MFC Icon 만들기  (0) 2018.05.23
CListCtrl DeleteItem  (0) 2018.05.15