1.需要在头文件中定义log对象以及控件ID
const int IDC_LISTBOX_LOG = 12345;
CListCtrl m_logList;
2.在初始化代码中添加CListCtrl控件的初始化代码
//获取当前坐标
CRect rcClient;
GetClientRect(&rcClient);
//设置log控件位置
CRect rcListBox;
rcListBox.left = rcClient.left ;
rcListBox.top = rcClient.top;
rcListBox.bottom = rcClient.bottom ;
CRect rcScreen;
SystemParametersInfo(SPI_GETWORKAREA, 0, &rcScreen, 0);
rcListBox.right = rcListBox.left + 600.0f * rcScreen.Width()/1920;
//在指定位置创建log控件
m_logList.Create(WS_CHILD | WS_VISIBLE | WS_BORDER | LVS_ALIGNLEFT, rcListBox, this, IDC_LISTBOX_LOG);
LONG lStyle;
m_logList.SetBkColor(RGB(200, 200, 200));
lStyle = GetWindowLong(m_logList.m_hWnd, GWL_STYLE);
lStyle &= ~LVS_TYPEMASK;
lStyle |= LVS_REPORT;
SetWindowLong(m_logList.m_hWnd, GWL_STYLE, lStyle);
m_logList.ShowWindow(SW_SHOW);
DWORD dwStyle = m_logList.GetExtendedStyle();
dwStyle |= LVS_EX_FULLROWSELECT;
dwStyle |= LVS_EX_GRIDLINES;
dwStyle |= LVS_EX_INFOTIP;
//dwStyle |= LVS_EX_TRANSPARENTBKGND;
m_logList.SetExtendedStyle(dwStyle);
m_logList.EnableWindow(TRUE);
CString strItem;
m_logList.InsertColumn(0, L"时间", LVCFMT_LEFT, 95 * rcScreen.Width() / 1920);
m_logList.InsertColumn(1, L"内容", LVCFMT_LEFT,500 * rcScreen.Width() / 1920);
m_logList.EnableWindow(TRUE);
m_logList.Invalidate();
m_logList.ShowWindow(SW_SHOW);
m_logList.BringWindowToTop();
3.接着在使用的时候,直接对于该控件插入对象
SYSTEMTIME dt;
::GetLocalTime(&dt);
CString sTimestamp;
sTimestamp.Format(_T("%02d:%02d:%02d:%03d"), dt.wHour, dt.wMinute, dt.wSecond, dt.wMilliseconds);
//log产生时间
m_logList.InsertItem(0, L"");
m_logList.SetItemText(0, 0, sTimestamp);
//log内容
CString cstr2(L"Button1");
m_logList.SetItemText(0, 1, cstr2);