1

I thought it be nice to add a Status Bar with percentage and other information to a CDialogEx that is used for viewing an image. But it doesn't seem that you can simply use a CMFCStatusBar or a CStatusBar and have it just work.

I found various samples, but none of them have the statusbar outside the client area and moves as resized? The different methods simply create a statusbar and it ends up hidden under a horizontal scrollbar and if you resize the window, the statusbar is sitting there in the middle of the dialog.

Is there an easy way or full example of having a statusbar on a CDialogEx that can be resized like a normal window?

Andrew Truckle
  • 17,769
  • 16
  • 66
  • 164
df234987
  • 513
  • 2
  • 13
  • A [status bar](https://learn.microsoft.com/en-us/windows/win32/controls/status-bar-reference) *is* a normal window. – IInspectable Jun 20 '20 at 07:19

1 Answers1

3

Is there an easy way or full example of having a statusbar on a CDialogEx that can be resized like a normal window?

Yes! Once you have created the status bar you can add it to the dynamic layout for resizing:

//This is where we actually draw it on the screen
RepositionBars(AFX_IDW_CONTROLBAR_FIRST, AFX_IDW_CONTROLBAR_LAST,
    ID_INDICATOR_MEETING_TYPE);
GetDynamicLayout()->AddItem(m_StatusBar.GetSafeHwnd(),
    CMFCDynamicLayout::MoveVertical(100), CMFCDynamicLayout::SizeHorizontal(100));

I have a status bar (not CMFCStatusBar as it will not work, but CStatusBar is OK) on two dialogs in my application.


When Dynamic Layout is not automatically enabled

Here is an updated example for when Dynamic Layout is not automatically enabled for you (CDialogEx with no controls):

BOOL CMyDlg::OnInitDialog()
{
  CDialogEx::OnInitDialog();

  if (!m_StatusBar.Create(this)) {
    TRACE0("Failed to create status bar\n");
    return -1;
  }

  m_StatusBar.SetIndicators(indicators, _countof(indicators));

  RepositionBars(AFX_IDW_CONTROLBAR_FIRST, AFX_IDW_CONTROLBAR_LAST, 0);

  EnableDynamicLayout();

  auto pdlmanager=GetDynamicLayout();
  if (pdlmanager) {
    if (pdlmanager->Create(this)) {
      pdlmanager->AddItem(m_StatusBar.GetSafeHwnd(), CMFCDynamicLayout::MoveVertical(100), CMFCDynamicLayout::SizeHorizontal(100));
    }
  }
  // return TRUE unless you set the focus to a control
  // EXCEPTION: OCX Property Pages should return FALSE

  return TRUE;  
}

Catering for horizontal scroll bars

NIf you have a horizontal scrollbar the StatusBar will end up above it; therefore you may have to create separate CWnd and add it to the dynamic layout (it would also be the nIDLeftOver of the RepositionBars()).

Here's how you can add the a "view" window for the contents so scrollbars can be contained within the view area:

BOOL CMyDlg::OnInitDialog()
{
  CDialogEx::OnInitDialog();

  if (!m_StatusBar.Create(this)) {
    TRACE0("Failed to create status bar\n");
    return -1;
  }

  m_StatusBar.SetIndicators(indicators, _countof(indicators));

  CRect rc;
  GetClientRect(&rc);

  CString clsname=AfxRegisterWndClass(0);
  m_ImageView.Create(clsname, _T(""), WS_CHILD | WS_VISIBLE, rc, this, IDC_MY_VIEW);

  RepositionBars(AFX_IDW_CONTROLBAR_FIRST, AFX_IDW_CONTROLBAR_LAST, IDC_MY_VIEW);

  EnableDynamicLayout();

  auto pdlmanager=GetDynamicLayout();
  if (pdlmanager) {
    if (pdlmanager->Create(this)) {
      pdlmanager->AddItem(m_StatusBar.GetSafeHwnd(), CMFCDynamicLayout::MoveVertical(100), CMFCDynamicLayout::SizeHorizontal(100));
      pdlmanager->AddItem(m_ImageView.GetSafeHwnd(), CMFCDynamicLayout::MoveNone(), CMFCDynamicLayout::SizeHorizontalAndVertical(100, 100));
    }
  }

  // return TRUE unless you set the focus to a control
  // EXCEPTION: OCX Property Pages should return FALSE

  return TRUE;  
}
Andrew Truckle
  • 17,769
  • 16
  • 66
  • 164
  • 1
    I added additional information to answer that I needed to use to get Dynamic Layout enabled. – df234987 Jun 20 '20 at 18:46
  • 1
    @df234987 Please try to keep my answer relative to your question. You are adding additional code outside the question of the status bar resizing. The first edit was ok about enabling the layout manager but the second edit is going in another direction IMHO. – Andrew Truckle Jun 20 '20 at 23:44
  • 1
    Well, it gives the results people may want if using scrollbars. You add the CStatusBar but doesn't quite give you want you really want and you need to add another item to encapsulate what used to be on the dialog - this way they get all the answers in one place. – df234987 Jun 21 '20 at 00:29