結合icon圖示的CListCtrl摺疊和展開實施方法
阿新 • • 發佈:2018-12-23
類似上圖所示,通過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++;\
}
通過滑鼠雙擊對應行,達到摺疊和展開的資料重新整理方式,網上資料較多,在此不贅述。