Code to View/Hide Status Bar in SDI and MDI Applications

Ever wanted to change the "View/Hide Status Bar" menue option so that the application (main) window changes it's height accordingly preserving the size of the visible client area?

Here's a little handler for the OnViewStatusBar message handler to perform this task. Versions for MFC or Non-MFC available.

//
// OnViewStatusBar handler
//
// increase or decrease app window height to adapt to
// visible or invisible status bar
//

void CMainFrame::OnViewStatusBar() // MFC version
// by Volker Bartheld 

{
 CControlBar* pBar = GetControlBar(AFX_IDW_STATUS_BAR);

 if (pBar != NULL)
 {
  RECT AppWinRect, BarRect;

  GetWindowRect(&AppWinRect);
  pBar->GetWindowRect(&BarRect);
  int iBarHeight = BarRect.bottom - BarRect.top;

  BOOL bShow = (pBar->GetStyle() & WS_VISIBLE) == 0;
  ShowControlBar(pBar, bShow, FALSE);
  if (bShow)
  {
   //    ... increase frame
   SetWindowPos(&CWnd::wndNoTopMost,
   AppWinRect.top, AppWinRect.left,
   AppWinRect.right-AppWinRect.left, AppWinRect.bottom-AppWinRect.top+iBarHeight,
   SWP_NOMOVE);
  }
  else
  {
   //    ... decrease frame
   SetWindowPos(&CWnd::wndNoTopMost,
   AppWinRect.top, AppWinRect.left,
   AppWinRect.right-AppWinRect.left, AppWinRect.bottom-AppWinRect.top-iBarHeight,
   SWP_NOMOVE);
  }
 }
}

void CMainFrame::OnViewStatusBar()  // non-MFC-version
// by Alexander Sailer 

{
 // MFC StatusbarHandler aufrufen
 OnBarCheck(ID_VIEW_STATUS_BAR);

 CControlBar* pBar = NULL;
 BOOL bVisible(false);
 RECT AppWinRect, BarRect;
 int iBarHeight(0);

 // get ptr to status bar
 pBar = GetControlBar(ID_VIEW_STATUS_BAR);
 ASSERT(pBar);

 // Abme_ungen holen
 GetWindowRect(&AppWinRect);
 pBar->GetWindowRect(&BarRect);
 iBarHeight = BarRect.bottom - BarRect.top;

 // Abfrage ob Bar sichtbar ist
 bVisible = pBar->IsVisible();

 if (bVisible)//    ... increase frame
 {    
  SetWindowPos(&CWnd::wndNoTopMost,
  AppWinRect.top, AppWinRect.left,
  AppWinRect.right-AppWinRect.left,
  AppWinRect.bottom-AppWinRect.top+iBarHeight,
  SWP_NOMOVE);
 }
 else //    ... decrease frame
 {    
  SetWindowPos(&CWnd::wndNoTopMost,
  AppWinRect.top, AppWinRect.left,
  AppWinRect.right-AppWinRect.left,
  AppWinRect.bottom-AppWinRect.top-iBarHeight,
  SWP_NOMOVE);
 }
}

Downloads

Download source - 1 Kb


Comments

  • Why not just use CFrameWnd::ShowControBar???

    Posted by Legacy on 03/26/2002 12:00am

    Originally posted by: CodeGuy

    The framework will automatically adjust the size of the client area for you:

    CControlBar* pStatusBar = GetControlBar(AFX_IDW_STATUS_BAR);
    ASSERT_VALID(pStatusBar);

    ShowControlBar(pStatusBar, FALSE, FALSE);

    Could save yourself several lines of code ;).

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: November 20, 2014 @ 2:00 p.m. ET / 11:00 a.m. PT Are you wanting to target two or more platforms such as iOS, Android, and/or Windows? You are not alone. 90% of enterprises today are targeting two or more platforms. Attend this eSeminar to discover how mobile app developers can rely on one IDE to create applications across platforms and approaches (web, native, and/or hybrid), saving time, money, and effort and introducing apps to market faster. You'll learn the trade-offs for gaining long …

  • IBM Worklight is a mobile application development platform that lets you extend your business to mobile devices. It is designed to provide an open, comprehensive platform to build, run and manage HTML5, hybrid and native mobile apps.

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds