百度了List控件插入标题的方法,基本都是这几行代码加在OnInitialUpdate()中,
CListCtrl * pList;
pList = (CListCtrl*)GetDlgItem(IDC_LIST_RESULT);
pList -> InsertColumn(0, L"NO.", LVCFMT_CENTER,120);
但是试了好久都没有用,最后才查到需要将List 属性中的view设置为report,果然设置了之后就可以了。
为了让list标题宽度与list控件宽度适应,根据控件宽度生成标题宽度,代码如下:
CWnd* pWnd;
pWnd=GetDlgItem(IDC_LIST_RESULT);
CRect rec;
pWnd->GetWindowRect(&rec); //获取控件变化前的大小
int w = rec.Width()/12;
CListCtrl * pList;
pList = (CListCtrl*)GetDlgItem(IDC_LIST_RESULT);
pList -> InsertColumn(0, L"NO.", LVCFMT_CENTER,w);
pList -> InsertColumn(1, L"平均长", LVCFMT_CENTER,1.65*w);//Avg. Length
pList -> InsertColumn(2, L"最大长", LVCFMT_CENTER,1.65*w);
pList -> InsertColumn(3, L"最小长", LVCFMT_CENTER,1.65*w);
pList -> InsertColumn(4, L"平均宽", LVCFMT_CENTER,1.65*w);
pList -> InsertColumn(5, L"最大宽", LVCFMT_CENTER,1.65*w);
pList -> InsertColumn(6, L"最小宽", LVCFMT_CENTER,1.65*w);
pList -> InsertColumn(7, L"质量", LVCFMT_CENTER,1.1*w);
为了让标题宽度随窗口尺寸变化,代码为:
CWnd* pWnd;
pWnd=GetDlgItem(IDC_LIST_RESULT);
if(pWnd!=NULL) //判断是否为空,因为在窗口创建的时候也会调用OnSize函数,但是此时各个控件还没有创建,Pwnd为空
{
CRect rec;
pWnd->GetWindowRect(&rec); //获取控件变化前的大小
int w = rec.Width()/12;
CListCtrl * pList;
pList = (CListCtrl*)GetDlgItem(IDC_LIST_RESULT);
pList ->SetColumnWidth(0,w);
pList ->SetColumnWidth(1,1.65*w);
pList ->SetColumnWidth(2,1.65*w);
pList ->SetColumnWidth(3,1.65*w);
pList ->SetColumnWidth(4,1.65*w);
pList ->SetColumnWidth(5,1.65*w);
pList ->SetColumnWidth(6,1.65*w);
pList ->SetColumnWidth(7,1.2*w);
}