C++实现log记录

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);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值