我有一个Windows模板库CListViewCtrl在报告模式(所以有一个2列的标题)与所有者数据集.此控件显示搜索结果.如果没有返回结果,我想在列表框区域中显示一条消息,表明没有结果.是否有捷径可寻?你知道任何现有的控件/示例代码(我找不到任何东西).
否则,如果我将控件子类化以提供此功能,那么这将是一个好方法吗?
我最终继承了控件并处理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 );
}
}
}
};
Run Code Online (Sandbox Code Playgroud)
搜索运行后,如果没有结果,我调用SetStatusMessage,消息显示在标题下居中.这就是我想要的.我是一个子类化控件的新手,所以我不确定这是否是最好的解决方案.
| 归档时间: |
|
| 查看次数: |
2440 次 |
| 最近记录: |