參考:
CMFCListCtrl控件使用
方法是行里插入一個1像素寬的icon
修改標題的方法是修改控件的字體
代碼的效果是這樣的
1.新建一個MFC類CMyListCtrl,繼承自CListCtrl
MyListCtrl.h
1 #pragma once 2 3 4 // CMyListCtrl 5 6 class CMyListCtrl : public CListCtrl 7 { 8 DECLARE_DYNAMIC(CMyListCtrl) 9 10 public: 11 CMyListCtrl(); 12 virtual ~CMyListCtrl(); 13 14 int m_nRowHeight; 15 16 protected: 17 DECLARE_MESSAGE_MAP() 18 public: 19 void SetRowHeigt(int); 20 void AutoColumn(void); 21 };
MyListCtrl.cpp
1 // MyListCtrl.cpp : 實現文件 2 // 3 4 #include "stdafx.h" 5 #include "MyListCtrl.h" 6 7 8 // CMyListCtrl 9 10 IMPLEMENT_DYNAMIC(CMyListCtrl, CListCtrl) 11 12 CMyListCtrl::CMyListCtrl() 13 { 14 15 } 16 17 CMyListCtrl::~CMyListCtrl() 18 { 19 } 20 21 22 BEGIN_MESSAGE_MAP(CMyListCtrl, CListCtrl) 23 END_MESSAGE_MAP() 24 25 //取巧方法設置行高,只要這一個函數就行了 26 void CMyListCtrl::SetRowHeigt(int nHeight) 27 { 28 CImageList im; 29 im.Create(1, nHeight, ILC_COLOR4, 10, 10); 30 SetImageList(&im, LVSIL_SMALL); 31 } 32 33 //調整最后一列的寬度是剩余寬度 34 void CMyListCtrl::AutoColumn(void) 35 { 36 CRect xRect; 37 int nLastColumnWidth = 0; 38 int nColumnNum = 0; 39 40 GetClientRect(&xRect);//OleControl的 41 nLastColumnWidth = xRect.Width(); 42 nColumnNum = GetHeaderCtrl()->GetItemCount(); 43 44 for (int i = 0; i < nColumnNum - 1; i++) 45 nLastColumnWidth = nLastColumnWidth - GetColumnWidth(i); 46 SetColumnWidth(nColumnNum - 1, nLastColumnWidth); 47 }
2.將控件變量的類型改成自己的類 m_listFont要聲明和釋放 cFont m_listFont;
3.在OnInitDialog添加代碼
1 m_listFont.CreatePointFont(200, _T("宋體")); 2 m_list.SetFont(&m_listFont); 3 4 DWORD dwStyle = m_list.GetExtendedStyle(); 5 dwStyle = dwStyle | LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES | LVS_EX_CHECKBOXES; 6 //LVS_EX_HEADERDRAGDROP | LVS_EX_SUBITEMIMAGES; 7 //選擇整行 | 繪制表格 | 復選框 | 項目頭可拖拽 | 圖標列表 8 m_list.SetExtendedStyle(dwStyle); 9 m_list.SetRowHeigt(70); 10 m_list.InsertColumn(0, _T("1234"), LVCFMT_LEFT, 200); 11 m_list.InsertColumn(1, _T("ABCD"), LVCFMT_LEFT, 200); 12 m_list.AutoColumn(); 13 14 m_list.InsertItem(0, _T("111")); 15 m_list.SetItemText(0, 1, _T("QQQ")); 16 m_list.SetCheck(0, 1);//選中 17 m_list.InsertItem(1, _T("222")); 18 m_list.SetItemText(1, 1, _T("WWW"));