Method to add a status bar to the MFC dialog box

  • 2020-04-02 03:09:18
  • OfStack

This article illustrates how to add a status bar to an MFC dialog box. Share with you for your reference. The details are as follows:

1. Add a member variable to the DLG implementation class in the dialog box:


CXTPStatusBar m_wndStatusBar; 
//Status bar (or CStatusBar)
//Initializes in the OnInitDialog method:
static UINT indicators[] =
{
ID_SEPARATOR, // status line indicator
ID_INDICATOR_CAPS,
ID_INDICATOR_NUM,
ID_INDICATOR_SCRL,
};
//Add status bar
if (!m_wndStatusBar.Create(this) ||
!m_wndStatusBar.SetIndicators(indicators,
sizeof(indicators)/sizeof(UINT)))
{
TRACE0("Failed to create status barn");
return -1; // fail to create
}

2. Add the OnKickIdle event (add it to the DLG header in the dialog box) :


afx_msg LRESULT OnKickIdle(WPARAM, LPARAM);
afx_msg void OnUpdateKeyIndicator(CCmdUI* pCmdUI);
DECLARE_MESSAGE_MAP()

3. Add the corresponding two methods in the implementation class:


LRESULT CDialogPanesDlg::OnKickIdle(WPARAM, LPARAM)
{
m_wndStatusBar.SendMessage(WM_IDLEUPDATECMDUI, TRUE);
return 0;
}
void CDialogPanesDlg::OnUpdateKeyIndicator(CCmdUI* pCmdUI)
{
UINT nVK;
UINT flag = 0 x 0001;
switch (pCmdUI->m_nID)
{
case ID_INDICATOR_CAPS:
nVK = VK_CAPITAL;
break;
case ID_INDICATOR_NUM:
nVK = VK_NUMLOCK;
break;
case ID_INDICATOR_SCRL:
nVK = VK_SCROLL;
break;
default:
TRACE1("Warning: OnUpdateKeyIndicator  �  unknown indicator 0x%04X.n",
pCmdUI->m_nID);
pCmdUI->ContinueRouting();
return; // not for us
}
pCmdUI->Enable(::GetKeyState(nVK) & flag);
// enable static text based on toggled key state
ASSERT(pCmdUI->m_bEnableChanged);
}

4. Run and find the status bar invisible, add WM_SIZE event of the dialog box:


void CDialogPanesDlg::OnSize(UINT nType, int cx, int cy)
{
CDialog::OnSize(nType, cx, cy);
// TODO: Add your message handler code here
CRect rcClient(0, 0, cx, cy);
RepositionBars(0, 0xffff, AFX_IDW_PANE_FIRST, 0, 0, &rcClient);
RepositionBars(0, 0xffff, AFX_IDW_PANE_FIRST, reposQuery, &rcClient, &rcClient);
}

Hope that this article is helpful to everyone's MFC programming.


Related articles: