代码片段:
类里面定义:
CListCtrl m_List;
void CDevStateDlg::InitListCtrl(void)
{
LONG styles;
styles = GetWindowLong(m_List.m_hWnd,GWL_STYLE);//获取窗口风格
SetWindowLong(m_List.m_hWnd,GWL_STYLE,styles | LVS_REPORT);
m_List.SetExtendedStyle(LVS_EX_FULLROWSELECT|LVS_EX_GRIDLINES);
m_List.InsertColumn(0,"时间",LVCFMT_LEFT,200);
m_List.InsertColumn(1,"数量",LVCFMT_LEFT,200);
m_List.InsertColumn(2,"备注",LVCFMT_LEFT,200);
}
不显示列可能是没加前面的两行:
LONG styles;
styles = GetWindowLong(m_List.m_hWnd,GWL_STYLE);//获取窗口风格
SetWindowLong(m_List.m_hWnd,GWL_STYLE,styles | LVS_REPORT);
然后再接收到消息后添加行:
BOOL CDevStateDlg::PreTranslateMessage(MSG* pMsg)
{
// TODO: Add your specialized code here and/or call the base class
if(pMsg->message==WM_NUM)
{
CString str;
CTime ctime = CTime::GetCurrentTime();
CString strTime = ctime.Format("%Y-%m-%d:%H:%M:%S");
str.Format("%d", pMsg->wParam);
int iIndex = m_List.GetItemCount();
m_List.InsertItem(iIndex,strTime);
m_List.SetItemText(iIndex,1,str);
if (pMsg->wParam == 0)
{
m_List.SetItemText(iIndex,2,"失败");
}
else
{
m_List.SetItemText(iIndex,2,"成功");
}
}
return CDialogEx::PreTranslateMessage(pMsg);
}
接收到WM_NUM消息后,处理界面:
CTime ctime = CTime::GetCurrentTime();
CString strTime = ctime.Format("%Y-%m-%d:%H:%M:%S");//获取系统时间
str.Format("%d", pMsg->wParam);//整型转CString
int iIndex = m_List.GetItemCount();
m_List.InsertItem(iIndex,strTime);
m_List.SetItemText(iIndex,1,str);//追加到下一行