Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / desktop / MFC

Missing CMFCListView Class

5.00/5 (4 votes)
29 Jan 2024CPOL 23.5K   47  
How to implement the missing CMFCListView class
The code provided here presents a solution for implementing a custom list view functionality, CMFCListView, in MFC Framework, utilizing CMFCListCtrl to extend CListCtrl functionalities with advanced header control features.

Since Microsoft forgot to implement the CMFCListView class, I found a solution. The CMFCListCtrl class extends the functionality of the CListCtrl class by supporting the advanced header control functionality of the CMFCHeaderCtrl class.

MFCListView.h : header file
C++
class CMFCListView : public CFormView
{
    DECLARE_DYNCREATE(CMFCListView)
 
protected:
    CMFCListView();           // protected constructor used by dynamic creation
    virtual ~CMFCListView();
 
public:
    enum { IDD = IDD_MFCLISTVIEW };
#ifdef _DEBUG
    virtual void AssertValid() const;
#ifndef _WIN32_WCE
    virtual void Dump(CDumpContext& dc) const;
#endif
#endif
 
protected:
    virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
public:
    afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
    afx_msg void OnDestroy();
    afx_msg void OnSize(UINT nType, int cx, int cy);
    afx_msg BOOL OnEraseBkgnd(CDC* pDC);
    virtual void OnInitialUpdate();
    virtual BOOL OnCmdMsg(UINT nID, int nCode, 
                 void* pExtra, AFX_CMDHANDLERINFO* pHandlerInfo);
    CMFCListCtrl& GetListCtrl() { return m_mfcListCtrl; }
 
protected:
    CMFCListCtrl m_mfcListCtrl;
 
    DECLARE_MESSAGE_MAP()
};
MFCListView.cpp : implementation file
C++
#define ID_MFCLISTCTRL (WM_USER+0x1234)
 
IMPLEMENT_DYNCREATE(CMFCListView, CFormView)
 
CMFCListView::CMFCListView()
    : CFormView(CMFCListView::IDD)
{
}
 
CMFCListView::~CMFCListView()
{
}
 
void CMFCListView::DoDataExchange(CDataExchange* pDX)
{
    CFormView::DoDataExchange(pDX);
}
 
BEGIN_MESSAGE_MAP(CMFCListView, CFormView)
    ON_WM_CREATE()
    ON_WM_DESTROY()
    ON_WM_SIZE()
    ON_WM_ERASEBKGND()
END_MESSAGE_MAP()
 
// CMFCListView diagnostics

#ifdef _DEBUG
void CMFCListView::AssertValid() const
{
    CFormView::AssertValid();
}
 
#ifndef _WIN32_WCE
void CMFCListView::Dump(CDumpContext& dc) const
{
    CFormView::Dump(dc);
}
#endif
#endif //_DEBUG

// CMFCListView message handlers

int CMFCListView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
    if (CFormView::OnCreate(lpCreateStruct) == -1)
        return -1;
 
    if (!m_mfcListCtrl.Create(WS_CHILD | WS_VISIBLE | LVS_REPORT | 
            LVS_SHOWSELALWAYS, CRect(0, 0, 0, 0), this, ID_MFCLISTCTRL))
        return -1;
 
    return 0;
}
 
void CMFCListView::OnDestroy()
{
    CFormView::OnDestroy();
 
    VERIFY(m_mfcListCtrl.DestroyWindow());
}
 
void CMFCListView::OnSize(UINT nType, int cx, int cy)
{
    CFormView::OnSize(nType, cx, cy);
 
    if (m_mfcListCtrl.GetSafeHwnd() != NULL)
        m_mfcListCtrl.MoveWindow(0, 0, cx, cy);
}
 
BOOL CMFCListView::OnEraseBkgnd(CDC* pDC)
{
    // return CFormView::OnEraseBkgnd(pDC);

    return TRUE;
}
 
void CMFCListView::OnInitialUpdate()
{
    CFormView::OnInitialUpdate();
 
    SetScrollSizes(MM_TEXT, CSize(0, 0));
}
 
BOOL CMFCListView::OnCmdMsg(UINT nID, int nCode, 
     void* pExtra, AFX_CMDHANDLERINFO* pHandlerInfo)
{
    if (m_mfcListCtrl.OnCmdMsg(nID, nCode, pExtra, pHandlerInfo))
        return TRUE;
 
    return CFormView::OnCmdMsg(nID, nCode, pExtra, pHandlerInfo);
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)