状态栏的消息显示,MSDN里面有三种方式
?
There are three ways to update the text in a status-bar pane:
- Call
CWnd::SetWindowText to update the text in pane 0 only. - Call
CCmdUI::SetText in the status bar’s ON_UPDATE_COMMAND_UI handler. - Call
SetPaneText to update the text for any pane.
- 用***
UPDATE_COMMAND_UI *** 与***SetText ***在状态栏插入消息,显示时间消息
class CMainFrame : public CFrameWnd
{
public:
CMainFrame() noexcept;
protected:
DECLARE_DYNAMIC(CMainFrame)
public:
virtual BOOL PreCreateWindow(CREATESTRUCT& cs);
virtual BOOL OnCmdMsg(UINT nID, int nCode, void* pExtra, AFX_CMDHANDLERINFO* pHandlerInfo);
public:
virtual ~CMainFrame();
#ifdef _DEBUG
virtual void AssertValid() const;
virtual void Dump(CDumpContext& dc) const;
#endif
protected:
CToolBar m_wndToolBar;
CStatusBar m_wndStatusBar;
CChildView m_wndView;
···
}
因为状态栏对象m_wndStatusBar 是包含在CMainFrame 类中,所以要修改状态栏消息需要在CMainFrame 类中添加OnUpdateViewStatusBar消息
afx_msg void OnUpdateViewStatusBar(CCmdUI* pCmdUI);
?
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-dvUiCWH3-1643294965129)(https://raw.githubusercontent.com/konalo-x/pic/master/pic/202201272204100.png)]
?
- 步骤2
ID_VIEW_STATUS_BAR 下面选择UPDATE_COMMAND_UI 生成OnUpdateViewStatusBar 函数,同时vs2022会自动将之加入消息映射
BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
ON_WM_CREATE()
ON_WM_SETFOCUS()
ON_UPDATE_COMMAND_UI(ID_INDICATOR_STR, &CMainFrame::OnUpdateViewStatusBar)
ON_WM_TIMER()
ON_WM_CLOSE()
ON_COMMAND(ID_FORMAT_FONT, &CMainFrame::OnFormatFont)
END_MESSAGE_MAP()
static UINT indicators[] =
{
ID_SEPARATOR,
ID_INDICATOR_CAPS,
ID_INDICATOR_NUM,
ID_INDICATOR_SCRL,
ID_INDICATOR_TIME,
ID_INDICATOR_STR
};
? 补充代码如下
void CMainFrame::OnUpdateViewStatusBar(CCmdUI* pCmdUI)
{
CString string;
string.Format(_T("行 %d 列 %d"), m_wndView.m_wndEdit.GetLineCount(), m_wndView.m_wndEdit.LineLength());
pCmdUI->SetText(string);
}
? 前面MESSAGEMAP 里面写明了 ID_INDICATOR_STR 对应OnUpdateViewStatusBar
ON_UPDATE_COMMAND_UI(ID_INDICATOR_STR, &CMainFrame::OnUpdateViewStatusBar)
-
用SetPaneText 显示时间
int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
SetTimer(1, 1000, NULL);//在里面安装定时器,并将其时间间隔设为1000毫秒
...
}
void CMainFrame::OnTimer(UINT_PTR nIDEvent)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
CTime time;
time = CTime::GetCurrentTime();//得到当前时间
CString s_time = time.Format("%H:%M:%S");//转换时间格式
m_wndStatusBar.SetPaneText(4, s_time);// 4 代表 第四个数组元素 ID_INDICATOR_TIME
CFrameWnd::OnTimer(nIDEvent);
}
void CMainFrame::OnClose()
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
KillTimer(1);
CFrameWnd::OnClose();
}
|