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
public:

// Overrides
protected:
	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CEmptyListCtrl)
	//}}AFX_VIRTUAL

// Implementation
public:

// Generated message map functions
protected:
	//{{AFX_MSG(CEmptyListCtrl)
	afx_msg void OnPaint();
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

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

void CEmptyListCtrl::OnPaint() 
{
    Default();
    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;
        GetWindowRect(&rc);
        ScreenToClient(&rc);

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

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

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

        // Restore dc
        pDC->RestoreDC(nSavedDC);
        ReleaseDC(pDC);
    }
    // Do not call CListCtrl::OnPaint() for painting messages
}

License

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

Share

About the Author

GHINI Mauro

Italy Italy
No Biography provided

Comments and Discussions

 
Generaluseful idea PinmemberCIDev24-Sep-10 5:56 
GeneralA non MFC version Pinmemberineox24-Mar-10 3:37 
GeneralError PinmemberAdi Narayana22-May-06 22:30 
GeneralBugs PinmemberTim L18-Nov-05 10:24 
GeneralUsing the default listview font and colors Pinmemberjavrillon23-Jul-04 7:38 
QuestionHow to use in a DLL PinmemberLordLiverpool22-May-03 4:37 
Generallistview adding mutiple colored row selection PinmemberAnonymous2-Apr-02 10:16 
GeneralPerhaps a better way... PinmemberDavid Fleming7-Mar-02 12:19 
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 
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 
Generalgot it working for a ClistView PinsussX23-Oct-00 16:21 
Here is what I did to get this to work in a ClistView.
Changed
 
from: if (GetItemCount() <= 0)
to: if (GetListCtrl().GetItemCount() <= 0)
 
from: pHC = GetHeaderCtrl();
to: pHC = GetListCtrl().GetHeaderCtrl();
 
Added the following to my cpp implementation file
 
BEGIN_MESSAGE_MAP(CExCListCtrl, CListView)
//{{AFX_MSG_MAP(CExCListCtrl)
ON_WM_PAINT()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
 
-
QuestionHow does control get painted when the row count > 0??? PinsussCaleb15-Aug-00 16:58 
AnswerFound better example at Codeguru PinsussCaleb15-Aug-00 18:41 
GeneralSuggestion for Call Back List Control Type PinsussMurugan N.V.7-Jun-00 11:47 
GeneralDisabled ListCtrl PinsussLeo Davidson13-Jan-00 8:15 

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
Web01 | 2.8.141223.1 | Last Updated 12 Jan 2000
Article Copyright 2000 by GHINI Mauro
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid