結合icon圖標的CListCtrl摺疊和展開實施方法

類似上圖所示,通過CListCtrl控件實現一個局部列表摺疊和展開。

步驟一:設計兩個小圖標,其中“+”標識摺疊狀態,“-”標識展開狀態;

步驟二:CListCtrl初始化爲report方式,設置如下幾個屬性

LONG lStyle;
lStyle = GetWindowLong(m_list.m_hWnd, GWL_STYLE);//獲取當前窗口style
lStyle &= ~LVS_TYPEMASK; //清除顯示方式位
lStyle |= LVS_REPORT; //設置style
SetWindowLong(m_list.m_hWnd, GWL_STYLE, lStyle);//設置style
 
DWORD dwStyle = m_list.GetExtendedStyle();
dwStyle |= LVS_EX_FULLROWSELECT;//選中某行使整行高亮(只適用與report風格的listctrl)
dwStyle |= LVS_EX_GRIDLINES;//網格線(只適用與report風格的listctrl)
dwStyle |= LVS_EX_SUBITEMIMAGES;//item前生成checkbox控件
m_list.SetExtendedStyle(dwStyle); //設置擴展風格

步驟三:初始化圖標

CImageList* pImgList = &m_ImageList;
HICON hIcon[2];

pImgList->Create(12, 16, ILC_COLOR24 | ILC_MASK, 0, 2);
hIcon[0] = AfxGetApp()->LoadIcon(IDI_ICON_PLUS_16);
hIcon[1] = AfxGetApp()->LoadIcon(IDI_ICON_MINUS_16);
for (int i = 0; i < 2; i++)
	pImgList->Add(hIcon[i]);
m_ctrlListTagInfo.SetImageList(pImgList, LVSIL_SMALL);

步驟四:增加隱藏列,規避首列默認圖標

#define MAKE_LIST_HEADER(lst) \
  lst.InsertColumn(0, _T(""), LVCFMT_LEFT, 0, 0);/*add col-0: always hidden column */\
  lst.InsertColumn(1, _T("Field"), LVCFMT_LEFT, rect.Width()*8/16, 1);\
  lst.InsertColumn(2, _T("Value (Desc)"), LVCFMT_LEFT, rect.Width()*8/16, 2);

步驟五:數據插入方式及更新

#define LIST_SET_ICON_SUBITEM(n, nSubItem, strKey, nImageIndex)\
{\
	LVITEM it;\
	it.mask     = LVIF_IMAGE | LVIF_TEXT;\
	it.iImage   = (nImageIndex);\
	it.iItem    = n;\
	it.iSubItem = nSubItem;\
	it.pszText  = strKey;\
	lst.SetItem(&it);\
}

#define HYS_LIST_INSERT_INT_KVD_ICON(strKey, nVal, strDesc, nImageIndex) \
{\
	LIST_INSERT_NEW_ROW(nRow);\
	LIST_SET_ICON_SUBITEM(nRow, 1, strKey, nImageIndex);\
	strHex.Format(_T("%d (%s)"), (nVal), (strDesc));\
	lst.SetItemText(nRow, 2, strHex);\
	nRow++;\
}

通過鼠標雙擊對應行,達到摺疊和展開的數據刷新方式,網上資料較多,在此不贅述。

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章