带有状态文本的 WTL CListViewCtrl

发布于 2024-07-22 12:04:09 字数 199 浏览 5 评论 0原文

我有一个处于报告模式的 Windows 模板库 CListViewCtrl(因此有一个包含 2 列的标题)和所有者数据集。 该控件显示搜索结果。 如果没有返回结果,我想在列表框区域中显示一条消息,指示没有结果。 是否有捷径可寻? 您知道任何现有的控件/示例代码吗(我找不到任何东西)。

否则,如果我对控件进行子类化以提供此功能,什么是一个好的方法?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

两人的回忆 2024-07-29 12:04:09

我最终对控件进行了子类化并像这样处理 OnPaint:

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 );
            }
        }
    }
};

搜索运行后,如果没有结果,我调用 SetStatusMessage 并将消息显示在标题下方的中心。 这就是我想要的。 我是子类化控件的新手,所以我不确定这是否是最好的解决方案。

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.

暮色兮凉城 2024-07-29 12:04:09

如果您使用的是 Vista 或更高版本,请处理 LVN_GETEMPTYMARKUP 通知。 对于 Vista 之前的版本,您需要自己绘制消息。

If you're on Vista or later, handle the LVN_GETEMPTYMARKUP notification. For pre-Vista, you'll need to paint the message yourself.

落叶缤纷 2024-07-29 12:04:09

另一个想法是拥有另一个控件,其大小和位置与列表控件相同,但隐藏。 可以是编辑控件、静态文本、浏览器控件或其他控件。

然后,当您没有任何搜索结果时,您可以将消息放入此控件中,并取消隐藏它。 当用户执行另一次搜索并返回结果时,您可以隐藏此控件并正常在列表视图中显示结果。

Another idea is to have another control, with the same size and position as the list control, but hidden. Could be an edit control, static text, browser control, or what have you.

Then when you don't have any search results, you put the message in this control, and un-hide it. When the user does another search that returns results, you hide this control and show the results in the list view normally.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文