虽然我对CListCtrl不熟,但这几天的编程,我也学到了一点,就在这里总结一下。
一、给CListCtrl添加表头
如下代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
void
CMainFrameDlg::AddListForPC()
{
m_PC_information.SetExtendedStyle(LVS_EX_GRIDLINES);
m_PC_information.DeleteAllItems();
while
(m_PC_information.DeleteColumn(
0
));
m_PC_information.ModifyStyle(
0
,LVS_REPORT);
m_PC_information.InsertColumn(
0
,
"IP"
,LVCFMT_CENTER,
90
);
m_PC_information.InsertColumn(
1
,
"下载流量"
,LVCFMT_CENTER,
90
);
m_PC_information.InsertColumn(
2
,
"上传流量"
,LVCFMT_CENTER,
90
);
m_PC_information.InsertColumn(
3
,
"下载总流量"
,LVCFMT_CENTER,
100
);
m_PC_information.InsertColumn(
4
,
"网速(K/S)"
,LVCFMT_CENTER,
111
);
m_PC_information.InsertColumn(
5
,
"主机名"
,LVCFMT_CENTER,
94
);
}
|
如果需要有表格可以添加这行代码:
m_PC_information.SetExtendedStyle(LVS_EX_GRIDLINES);
二、添加数据:
代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
void
CMainFrameDlg::AddInfoToList()
{
CString temp;
//添加数据
m_PC_information.SetRedraw(FALSE);
int
nRow=m_PC_information.InsertItem(
0
,flowInfo->IP);
m_PC_information.SetItemText(nRow,
0
,flowInfo->IP);
temp.Format(
"%lu"
,flowInfo->iReciveData);
m_PC_information.SetItemText(nRow,
1
,temp);
temp.Format(
"%lu"
,flowInfo->iSendData);
m_PC_information.SetItemText(nRow,
2
,temp);
temp.Format(
"%ld"
,flowInfo->iAllData);
m_PC_information.SetItemText(nRow,
3
,temp);
temp.Format(
"%lf"
,flowInfo->NetSpeed);
m_PC_information.SetItemText(nRow,
4
,temp);
m_PC_information.SetItemText(nRow,
5
,flowInfo->hostName);
m_PC_information.SetRedraw(TRUE);
}
|
记得SetItemText函数里的第三个参数的类型转换。
m_PC_information。SetRedraw()那两句话是防止更新数据时,屏幕闪烁。