Click here to Skip to main content
13,352,879 members (81,365 online)
Click here to Skip to main content
Add your own
alternative version


54 bookmarked
Posted 11 Jan 2000

Indicating an empty ListView

, 11 Jan 2000
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

You may also be interested in...

Comments and Discussions

Generaluseful idea Pin
CIDev24-Sep-10 5:56
memberCIDev24-Sep-10 5:56 
GeneralA non MFC version Pin
ineox24-Mar-10 3:37
memberineox24-Mar-10 3:37 
GeneralError Pin
Adi Narayana22-May-06 22:30
memberAdi Narayana22-May-06 22:30 
GeneralBugs Pin
Tim L18-Nov-05 10:24
memberTim L18-Nov-05 10:24 
GeneralUsing the default listview font and colors Pin
javrillon23-Jul-04 7:38
memberjavrillon23-Jul-04 7:38 
QuestionHow to use in a DLL Pin
LordLiverpool22-May-03 4:37
memberLordLiverpool22-May-03 4:37 
Generallistview adding mutiple colored row selection Pin
Anonymous2-Apr-02 10:16
memberAnonymous2-Apr-02 10:16 
GeneralPerhaps a better way... Pin
David Fleming7-Mar-02 12:19
memberDavid Fleming7-Mar-02 12:19 
After looking over the comments from others and giving it some thought (as well as some fiddling with my own code), I think I have found a better way.

I think it is better to perform this sort of thing in response to a WM_ERASEBKGND message (rather than WM_PAINT).

Also, if you are using Callbacks, you have to account for that as one of the messages mentions.

Considering those things, here's my take on it:

Add a function to handle the WM_ERASEBKGND message as follows:

BOOL CCallbackListCtrl::OnEraseBkgnd(CDC* pDC)
	if(GetItemCount() <= 0)
		int nSavedDC = pDC->SaveDC(); //save the current DC state

		//Set up variables
		COLORREF clrText = ::GetSysColor(COLOR_WINDOWTEXT);	//system text color
		COLORREF clrBack = ::GetSysColor(COLOR_WINDOW);		//system background color
		CRect rc;
		GetClientRect(&rc);	//get client area of the ListCtrl

		//If there is a header, we need to account for the space it occupies
		CHeaderCtrl* pHC = GetHeaderCtrl();
		if(pHC != NULL)
			CRect rcH;
			pHC->GetItemRect(0, &rcH); += rcH.bottom;

        //Here is the string we want to display (or you can use a StringTable entry
        CString strText = "There are no items to display.";

        //Now we actually display the text
        pDC->SetTextColor(clrText);	//set the text color
        pDC->SetBkColor(clrBack);	//set the background color
        pDC->FillRect(rc, &CBrush(clrBack));	//fill the client area rect
        pDC->SelectStockObject(ANSI_VAR_FONT);	//select a font
        pDC->DrawText(strText, -1, rc, 
                      DT_CENTER | DT_WORDBREAK | DT_NOPREFIX | DT_NOCLIP); //and draw the text

        // Restore dc
	//If there are items in the ListCtrl, we need to call the base class function
    return true;

That should do it. It works well for me (even when using Callbacks).
GeneralRe: Perhaps a better way... Pin
Atlantys19-Sep-02 7:46
memberAtlantys19-Sep-02 7:46 
GeneralRe: Perhaps a better way... Pin
Stlan7-Oct-03 4:27
memberStlan7-Oct-03 4:27 
GeneralRe: Perhaps a better way... Pin
David Fleming7-Oct-03 7:18
memberDavid Fleming7-Oct-03 7:18 
GeneralRe: Perhaps a better way... Pin
Stlan7-Oct-03 20:20
memberStlan7-Oct-03 20:20 
GeneralRe: Perhaps a better way... Pin
Ron Olson16-Jun-04 6:31
sussRon Olson16-Jun-04 6:31 
GeneralRe: Perhaps a better way... Pin
David Fleming16-Jun-04 7:13
memberDavid Fleming16-Jun-04 7:13 
GeneralRe: Perhaps a better way... Pin
Ron Olson16-Jun-04 17:02
memberRon Olson16-Jun-04 17:02 
Generaltoo complicated Pin
Anonymous2-Mar-02 3:22
memberAnonymous2-Mar-02 3:22 
GeneralRe: too complicated Pin
Martijn18-Sep-03 3:28
memberMartijn18-Sep-03 3:28 
GeneralI can't get this code to work Pin
DanYELL24-Aug-01 15:48
memberDanYELL24-Aug-01 15:48 
Generalgot it working for a ClistView Pin
X23-Oct-00 16:21
sussX23-Oct-00 16:21 
QuestionHow does control get painted when the row count > 0??? Pin
Caleb15-Aug-00 16:58
sussCaleb15-Aug-00 16:58 
AnswerFound better example at Codeguru Pin
Caleb15-Aug-00 18:41
sussCaleb15-Aug-00 18:41 
GeneralSuggestion for Call Back List Control Type Pin
Murugan N.V.7-Jun-00 11:47
sussMurugan N.V.7-Jun-00 11:47 
GeneralDisabled ListCtrl Pin
Leo Davidson13-Jan-00 8:15
sussLeo Davidson13-Jan-00 8:15 

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.

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