Click here to Skip to main content
Click here to Skip to main content

Indicating an empty ListView

, 11 Jan 2000 CPOL
Rate this:
Please Sign up or sign in to vote.
Instead of presenting a blank screen if your list control is empty, why not try this?

Sample Image - EmptyLV.gif

When using a ListView it may be useful to inform the user that the control is empty, like Microsoft Outlook does. Doing this is pretty straightforward. Just derive your own ListView control from CListCtrl and add a WM_PAINT event handler to your derived class.

class CEmptyListCtrl : public CListCtrl
// Construction

// Overrides
	// ClassWizard generated virtual function overrides

// Implementation

// Generated message map functions
	afx_msg void OnPaint();

Then, in OnPaint member function, implement the code necessary to handle the empty condition.

void CEmptyListCtrl::OnPaint() 
    if (GetItemCount() <= 0)
        COLORREF clrText = ::GetSysColor(COLOR_WINDOWTEXT);
        COLORREF clrTextBk = ::GetSysColor(COLOR_WINDOW);

        CDC* pDC = GetDC();
        // Save dc state
        int nSavedDC = pDC->SaveDC();

        CRect rc;

        CHeaderCtrl* pHC;
        pHC = GetHeaderCtrl();
        if (pHC != NULL)
            CRect rcH;
            pHC->GetItemRect(0, &rcH);
   += rcH.bottom;
        } += 10;

        CString strText((LPCSTR)IDS_NOITEMS); // The message you want!

        pDC->FillRect(rc, &CBrush(clrTextBk));
        pDC->DrawText(strText, -1, rc, 
                      DT_CENTER | DT_WORDBREAK | DT_NOPREFIX | DT_NOCLIP);

        // Restore dc
    // Do not call CListCtrl::OnPaint() for painting messages


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


About the Author


Italy Italy
No Biography provided

Comments and Discussions

GeneralRe: Perhaps a better way... PinmemberAtlantys19-Sep-02 7:46 
GeneralRe: Perhaps a better way... PinmemberStlan7-Oct-03 4:27 
GeneralRe: Perhaps a better way... PinmemberDavid Fleming7-Oct-03 7:18 
GeneralRe: Perhaps a better way... PinmemberStlan7-Oct-03 20:20 
Yes, it works when the routine is written in the OnPaint function (after Default()). I guess the lines are drawn during this default msg processing call.

But as you wrote, and I agree with you, it is a better practice to use OnEraseBkGrnd. So I wonder how it would work if one removes temporarily the style-ex LVS_EX_GRIDLINES each time the control enters the 'empty' mode, then to reset it again as soon there is some new item inserted.

GeneralRe: Perhaps a better way... PinsussRon Olson16-Jun-04 6:31 
GeneralRe: Perhaps a better way... PinmemberDavid Fleming16-Jun-04 7:13 
GeneralRe: Perhaps a better way... PinmemberRon Olson16-Jun-04 17:02 
Generaltoo complicated PinmemberAnonymous2-Mar-02 3:22 
GeneralRe: too complicated PinmemberMartijn18-Sep-03 3:28 
GeneralI can't get this code to work PinmemberDanYELL24-Aug-01 15:48 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150331.1 | Last Updated 12 Jan 2000
Article Copyright 2000 by GHINI Mauro
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid