bool AddLog(LPCSTR msg, const int max) { // Append the latest message const bool result = m_ListBox.AddString(msg) != CB_ERR; // Ensure its under the max while (m_ListBox.GetCount() > max) { Delete(0); } // Scro9ll to the last entry m_ListBox.SetTopIndex(m_ListBox.GetCount() - 1); return result; } void ExampleUse() { const int MAX_LOG = 100; AddLog("Something happened", MAX_LOG); }
Programming Tips - MFC: Use a CListBox as a visual log
Date: 2015dec9
Update: 2025sep16
OS: Windows
Framework: MFC
Keywords: CListCtrl
Q. MFC: Use a CListBox as a visual log
A. Here is a function that adds strings, makes sure the control
doesn't get too big and scrolls to the bottom.