添加窗体的Onsize事件:
void CStatusbartestDlg::OnSize(UINT nType, int cx, int cy)
{
CDialog::OnSize(nType, cx, cy);
// TODO: Add your message handler code here
CRect rectMain;
CPaintDC dc(this);
this->GetClientRect(rectMain);
CRect rectBar;
CToolBar *pToolBar = (CToolBar *)AfxGetApp()->m_pMainWnd->GetDescendantWindow(AFX_IDW_TOOLBAR);
if(pToolBar != NULL)
{
pToolBar->GetClientRect(&rectBar);
pToolBar->MoveWindow(0,0,rectMain.Width(),rectBar.Height()+9);
CWnd *pStaticToolBar = GetDlgItem(IDC_STATIC_ToolBar);//IDC_STATIC_ToolBar为picture控件,模拟工具栏的分隔条
if(pStaticToolBar != NULL)
{
pToolBar->GetClientRect(&rectBar);
pStaticToolBar->MoveWindow(0,rectBar.Height()+10,rectMain.Width(),2);
pStaticToolBar->ShowWindow(SW_SHOW);
}
}
CStatusBarCtrl *pStatusBar = (CStatusBarCtrl *)AfxGetApp()->m_pMainWnd->GetDescendantWindow(AFX_IDW_STATUS_BAR);//取得窗口指针
if(pStatusBar != NULL)
{
pStatusBar->GetClientRect(&rectBar);
pStatusBar->MoveWindow(0,cy-rectBar.Height(),rectMain.Width(),rectBar.Height());
pStatusBar->GetClientRect(&rectBar);
int nParts[4]= {rectMain.Width()/rectBar.Width()*100, rectMain.Width()/rectBar.Width()*200, rectMain.Width()/rectBar.Width()*400,-1}; //分割尺寸
pStatusBar->SetParts(4, nParts); //分割状态栏
CWnd *pStaticBar = GetDlgItem(IDC_STATIC_BAR);//picture控件ID
if(pStaticBar != NULL)
{
pStaticBar->MoveWindow(0,rectMain.Height() - rectBar.Height()-1,rectMain.Width(),2);
pStaticBar->ShowWindow(SW_SHOW);
}
//绘制背景色
dc.FillSolidRect(rectMain,RGB(255,255,255)); //设置为白色
}
}