其他分享
首页 > 其他分享> > MFC状态栏显示消息

MFC状态栏显示消息

作者:互联网

状态栏的消息显示,MSDN里面有三种方式

There are three ways to update the text in a status-bar pane:

  1. CallCWnd::SetWindowText to update the text in pane 0 only.
  2. CallCCmdUI::SetTextin the status bar’s ON_UPDATE_COMMAND_UI handler.
  3. Call SetPaneText to update the text for any pane.
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)]

属性-事件

BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
	ON_WM_CREATE()
	ON_WM_SETFOCUS()
	ON_UPDATE_COMMAND_UI(ID_INDICATOR_STR, &CMainFrame::OnUpdateViewStatusBar)//vs2022 自动添加代码
	//ON_UPDATE_COMMAND_UI(ID_INDICATOR_TIME, &CMainFrame::OnUpdateTimeStatusBar)
	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标识
	ID_INDICATOR_STR		//用来显示 行和列 的ID标识
};

​ 补充代码如下

void CMainFrame::OnUpdateViewStatusBar(CCmdUI* pCmdUI)
{
	// TODO: 在此添加命令更新用户界面处理程序代码
	CString string;
    //获取CEdit里面文本的行和列,仿照记事本
	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)

在这里插入图片描述

标签:INDICATOR,MFC,状态栏,CMainFrame,void,COMMAND,消息,OnUpdateViewStatusBar,ID
来源: https://blog.csdn.net/xiongguang1111111/article/details/122724808