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
{
public:
protected:
public:
protected:
afx_msg void OnPaint();
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();
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);
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);
pDC->RestoreDC(nSavedDC);
ReleaseDC(pDC);
}
}