Click here to Skip to main content
Licence CPOL
First Posted 11 Jan 2000
Views 132,376
Bookmarked 47 times

Indicating an empty ListView

By | 11 Jan 2000 | Article
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)

About the Author

GHINI Mauro



Italy Italy

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
Generaluseful idea PinmemberCIDev4:56 24 Sep '10  
GeneralA non MFC version Pinmemberineox2:37 24 Mar '10  
GeneralError PinmemberAdi Narayana21:30 22 May '06  
GeneralBugs PinmemberTim L9:24 18 Nov '05  
GeneralUsing the default listview font and colors Pinmemberjavrillon6:38 23 Jul '04  
QuestionHow to use in a DLL PinmemberLordLiverpool3:37 22 May '03  
Generallistview adding mutiple colored row selection PinmemberAnonymous9:16 2 Apr '02  
GeneralPerhaps a better way... PinmemberDavid Fleming11:19 7 Mar '02  
GeneralRe: Perhaps a better way... PinmemberAtlantys6:46 19 Sep '02  
GeneralRe: Perhaps a better way... PinmemberStlan3:27 7 Oct '03  
GeneralRe: Perhaps a better way... PinmemberDavid Fleming6:18 7 Oct '03  
GeneralRe: Perhaps a better way... PinmemberStlan19:20 7 Oct '03  
GeneralRe: Perhaps a better way... PinsussRon Olson5:31 16 Jun '04  
GeneralRe: Perhaps a better way... PinmemberDavid Fleming6:13 16 Jun '04  
GeneralRe: Perhaps a better way... PinmemberRon Olson16:02 16 Jun '04  
Generaltoo complicated PinmemberAnonymous2:22 2 Mar '02  
GeneralRe: too complicated PinmemberMartijn2:28 18 Sep '03  
GeneralI can't get this code to work PinmemberDanYELL14:48 24 Aug '01  
Generalgot it working for a ClistView PinsussX15:21 23 Oct '00  
QuestionHow does control get painted when the row count > 0??? PinsussCaleb15:58 15 Aug '00  
AnswerFound better example at Codeguru PinsussCaleb17:41 15 Aug '00  
GeneralSuggestion for Call Back List Control Type PinsussMurugan N.V.10:47 7 Jun '00  
GeneralDisabled ListCtrl PinsussLeo Davidson7:15 13 Jan '00  

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.

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