Click here to Skip to main content
15,889,034 members
Articles / Desktop Programming / MFC
Tip/Trick

Missing CMFCListView Class

Rate me:
Please Sign up or sign in to vote.
5.00/5 (4 votes)
29 Jan 2024CPOL 22.1K   38   9   2
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)


Written By
Software Developer NXP Semiconductors
Romania Romania
My professional background includes knowledge of analyst programmer for Microsoft Visual C++, Microsoft Visual C#, Microsoft Visual Basic, Sun Java, assembly for Intel 80x86 microprocessors, assembly for PIC microcontrollers (produced by Microchip Inc.), relational databases (MySQL, Oracle, SQL Server), concurrent version systems, bug tracking systems, web design (HTML5, CSS3, XML, PHP/MySQL, JavaScript).

Comments and Discussions

 
GeneralWhy don't you like CListView? It is inherited from CCtrlView... Pin
Nick Kulikovsky10-Nov-11 15:14
Nick Kulikovsky10-Nov-11 15:14 
GeneralRe: CListView is using CListCtrl, and I wanted to use the functi... Pin
Ștefan-Mihai MOGA12-Nov-11 20:56
professionalȘtefan-Mihai MOGA12-Nov-11 20:56 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.