這篇文章主要介紹了MFC對話框中添加狀態欄的方法,實例分析了MFC對話框添加狀態欄所涉及的相關成員變量與事件實現技巧,需要的朋友可以參考下
本文實例講述了MFC對話框中添加狀態欄的方法。分享給大家供大家參考。具體如下:
1.在對話框的dlg實現類里添加成員變量:
- CXTPStatusBar m_wndStatusBar;
- //狀態欄(或者是CStatusBar)
- //在OnInitDialog方法中初始化:
- static UINT indicators[] =
- {
- ID_SEPARATOR, // status line indicator
- ID_INDICATOR_CAPS,
- ID_INDICATOR_NUM,
- ID_INDICATOR_SCRL,
- };
- //添加狀態欄
- if (!m_wndStatusBar.Create(this) ||
- !m_wndStatusBar.SetIndicators(indicators,
- sizeof(indicators)/sizeof(UINT)))
- {
- TRACE0("Failed to create status bar/n");
- return -1; // fail to create
- }
2.添加OnKickIdle事件(在對話框的dlg的頭文件加上) :
- afx_msg LRESULT OnKickIdle(WPARAM, LPARAM);
- afx_msg void OnUpdateKeyIndicator(CCmdUI* pCmdUI);
- DECLARE_MESSAGE_MAP()
3.在實現類中添加對應的兩個方法:
- LRESULT CDialogPanesDlg::OnKickIdle(WPARAM, LPARAM)
- {
- m_wndStatusBar.SendMessage(WM_IDLEUPDATECMDUI, TRUE);
- return 0;
- }
- void CDialogPanesDlg::OnUpdateKeyIndicator(CCmdUI* pCmdUI)
- {
- UINT nVK;
- UINT flag = 0×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.運行發現看不見狀態欄,添加對話框的WM_SIZE事件:
- 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);
- }
希望本文所述對大家的MFC程序設計有所幫助。
新聞熱點
疑難解答