Tab Control是一个标签控件,只有标签,标签下面的窗体要你自己创建的子窗体、并通过回调、或者消息的方式来实现隐藏、显示,从而实现标签+显示选中的子窗体的效果。
MFC使用CTabCtrl进行封装,大致的流程是
1创建TabCtrl
2通过InsertItem方法添加页签
3创建子窗体,指定这些窗体的父窗体为 上面创建的TabCtrl
4在TabCtrl的父窗体类 或者 TabCtrl的继承类中 添加ON_NOTIFY响应消息, OnSize消息
5实现消息响应函数
参考代码:
int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
int nCtrlID = 1, nTabIndex = 0;
if ( !m_wndTabCreate( TCS_FOCUSNEVER|WS_VISIBLE|TCS_RIGHTJUSTIFY, CRect( 0, 0, 0, 0 ), &m_wndView, nCtrlID++ ) )
{
TRACE0("Failed to create m_wndTab \n");
return -1;
}
CFont tabFont;
tabFontCreateFont(12,0,0,0,FW_NORMAL,0,0,0,DEFAULT_CHARSET,OUT_DEFAULT_PRECIS,
CLIP_DEFAULT_PRECIS,DEFAULT_QUALITY,DEFAULT_PITCH|FF_DONTCARE,
"宋体");
m_wndTabSetFont(&tabFont,true);
tabFontDetach();
m_wndTabInsertItem( nTabIndex++, "错误" );
if ( !m_wndErrEventCreate( WS_VISIBLE|WS_BORDER|LVS_REPORT|LVS_NOSORTHEADER, CRect( 0, 0, 0, 0 ), &m_wndTab, nCtrlID++ ) )
{
TRACE0("Failed to create m_wndErrEvent \n");
return -1;
}
m_wndErrEventSetExtendedStyle( LVS_EX_FULLROWSELECT );
m_wndErrEventInsertColumn( 0, "时间" );
m_wndErrEventInsertColumn( 1, "错误事件" );
m_wndErrEventShowWindow( SW_SHOW );
m_wndTabInsertItem( nTabIndex++, "调试" );
if ( !m_wndDebugEventCreate( WS_VISIBLE|WS_BORDER|LVS_REPORT|LVS_NOSORTHEADER, CRect( 0, 0, 0, 0 ), &m_wndTab, nCtrlID++ ) )
{
TRACE0("Failed to create m_wndDebugEvent \n");
return -1;
}
m_wndDebugEventSetExtendedStyle( LVS_EX_FULLROWSELECT );
m_wndDebugEventInsertColumn( 0, "时间" );
m_wndDebugEventInsertColumn( 1, "调试事件" );
m_wndErrEventShowWindow( SW_HIDE );
m_wndDebugEventShowWindow( SW_SHOW );
m_wndTabSetCurSel( m_wndTabGetItemCount() - 1 );
}
ChildViewh:
//{{AFX_MSG(CChildView)
afx_msg void OnPaint();
afx_msg void OnSize(UINT nType, int cx, int cy);
//}}AFX_MSG
afx_msg void OnSelChangeTab(NMHDR pNotify,LRESULT pResult);
DECLARE_MESSAGE_MAP()
ChildViewcpp:
BEGIN_MESSAGE_MAP(CChildView,CWnd )
//{{AFX_MSG_MAP(CChildView)
ON_WM_PAINT()
ON_WM_SIZE()
//}}AFX_MSG_MAP
ON_NOTIFY(TCN_SELCHANGE,1,OnSelChangeTab)
END_MESSAGE_MAP()
void CChildView::OnSize(UINT nType, int cx, int cy)
{
CWnd ::OnSize(nType, cx, cy);
if (IsWindow(((CMainFrame)theAppm_pMainWnd)->m_wndTabm_hWnd))
{
CRect viewRect;
this->GetClientRect(viewRect);
((CMainFrame)theAppm_pMainWnd)->m_wndTabMoveWindow(viewRect);
CRect tabRect;
((CMainFrame)theAppm_pMainWnd)->m_wndTabGetClientRect(tabRect);
((CMainFrame)theAppm_pMainWnd)->m_wndTabAdjustRect(FALSE,tabRect);
if (IsWindow(((CMainFrame)theAppm_pMainWnd)->m_wndDebugEventm_hWnd))
{
((CMainFrame)theAppm_pMainWnd)->m_wndDebugEventMoveWindow(tabRect);
}
if (IsWindow(((CMainFrame)theAppm_pMainWnd)->m_wndErrEventm_hWnd))
{
((CMainFrame)theAppm_pMainWnd)->m_wndErrEventMoveWindow(tabRect);
}
for ( int i = 0; i < 2; i++ )
{
((CMainFrame)theAppm_pMainWnd)->m_wndDebugEventSetColumnWidth( i, LVSCW_AUTOSIZE_USEHEADER );
((CMainFrame)theAppm_pMainWnd)->m_wndErrEventSetColumnWidth( i, LVSCW_AUTOSIZE_USEHEADER );
}
}
}
void CChildView::OnSelChangeTab(NMHDR pNotify,LRESULT pResult)
{
int cursel=((CMainFrame)theAppm_pMainWnd)->m_wndTabGetCurSel();
if ( cursel == ((CMainFrame)theAppm_pMainWnd)->m_wndTabGetItemCount() - 1 )
{
((CMainFrame)theAppm_pMainWnd)->m_wndDebugEventShowWindow(SW_SHOW);
((CMainFrame)theAppm_pMainWnd)->m_wndErrEventShowWindow(SW_HIDE);
}
else if ( cursel == 0 )
{
((CMainFrame)theAppm_pMainWnd)->m_wndDebugEventShowWindow(SW_HIDE);
((CMainFrame)theAppm_pMainWnd)->m_wndErrEventShowWindow(SW_SHOW);
}
}
以上就是关于VC6.0中基于对话框的MFC EXE中的Tab Control控件如何使用全部的内容,包括:VC6.0中基于对话框的MFC EXE中的Tab Control控件如何使用、、等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)