Search code examples
c++wtl

WTL CListViewCtrl with status text


I have a Windows Template Library CListViewCtrl in report mode (so there is a header with 2 columns) with owner data set. This control displays search results. If no results are returned I want to display a message in the listbox area that indicates that there were no results. Is there an easy way to do this? Do you know of any existing controls/sample code (I couldn't find anything).

Otherwise, if I subclass the control to provide this functionality what would be a good approach?


Solution

  • I ended up subclassing the control and handling OnPaint like this:

    class MsgListViewCtrl : public CWindowImpl< MsgListViewCtrl, WTL::CListViewCtrl >
    {
        std::wstring m_message;
    public:
        MsgListViewCtrl(void) {}
    
        BEGIN_MSG_MAP(MsgListViewCtrl)
            MSG_WM_PAINT( OnPaint )
        END_MSG_MAP()
    
        void Attach( HWND hwnd )
        {
            SubclassWindow( hwnd );
        }
    
        void SetStatusMessage( const std::wstring& msg )
        {
            m_message = msg;
        }
    
        void OnPaint( HDC hDc )
        {
            SetMsgHandled( FALSE );
            if( GetItemCount() == 0 )
            {
                if( !m_message.empty() )
                {
                    CRect cRect, hdrRect;
                    GetClientRect( &cRect );
                    this->GetHeader().GetClientRect( &hdrRect );
                    cRect.top += hdrRect.Height() + 5;
    
                    PAINTSTRUCT ps;
                    SIZE size;
                    WTL::CDCHandle handle = this->BeginPaint( &ps );
                    handle.SelectFont( this->GetFont() );
                    handle.GetTextExtent( m_message.c_str(), (int)m_message.length(), &size );
                    cRect.bottom = cRect.top + size.cy;
                    handle.DrawText( m_message.c_str(), -1, &cRect, DT_CENTER | DT_SINGLELINE | DT_VCENTER );
                    this->EndPaint( &ps );
                    SetMsgHandled( TRUE );
                }
            }
        }
    };
    

    After the search runs, if there are no results, I call SetStatusMessage and the message is displayed centered under the header. That's what I wanted. I'm kind of a newbie at subclassing controls so I'm not sure if this is the best solution.