MFC状态栏创建和设计

来源:互联网 发布:知豆电动车网上怎么租 编辑:程序博客网 时间:2024/05/30 20:07

demo1 (新建一个窗格)

demo2 (窗格显示时间)

demo3 (窗格显示鼠标坐标)

demo4 (窗格显示进度条)

//摘抄自MSDN/*状态栏(CStatusBar 类的一个窗口对象)包含几个“窗格”。每个窗格都是状态栏中可用来显示信息的矩形区域。例如,很多应用程序在最右边的窗格显示CAPSLOCK、NUMLOCK和其他键的状态。应用程序还经常在最左边的窗格(窗格0)显示信息文本,此窗格有时称为“消息窗格”。例如,默认 MFC 状态栏使用消息窗格显示一个字符串,来解释当前选定的菜单项或工具栏按钮。*///新建窗格/*To create a status bar, follow these steps: 1.Construct the CStatusBar object.2.Call the Create (or CreateEx) function to create the status-bar window and attach it to the CStatusBar object.3.Call SetIndicators to associate a string ID with each indicator. *//*1.定义窗格的命令 ID。在“视图”菜单上单击“资源视图”。右击项目资源并单击“资源符号”。在“资源符号”对话框中,单击“新建”。键入一个命令 ID 名称:例如,ID_INDICATOR_PAGE。为 ID 指定值,或接受“资源符号”对话框建议的值。例如,对于 ID_INDICATOR_PAGE,接受默认值。关闭“资源符号”对话框。2.定义窗格中要显示的默认字符串。打开“资源视图”后,在为应用程序列出资源类型的窗口中双击“String Table”。打开“字符串表”编辑器后,从“插入”菜单中选择“新建字符串”。在“字符串属性”窗口中,选择窗格的命令 ID(例如:ID_INDICATOR_PAGE)并键入默认字符串值,如“Page   ”。关闭字符串编辑器。(需要一个默认字符串以避免编译器错误。)3.在文件 MAINFRM.CPP 中定位 indicators 数组。该数组按从左向右的顺序为状态栏的所有指示器列出了命令 ID。在数组中的适当位置,输入窗格的命令 ID。*/demo1 (新建一个窗格)static UINT BASED_CODE indicators[] ={    ID_SEPARATOR,           // status line indicator    ID_INDICATOR_CAPS,    ID_INDICATOR_NUM,    ID_INDICATOR_SCRL,    ID_INDICATOR_PAGE,};


demo2 (窗格显示时间)/*There are three ways to update the text in a status-bar pane: 1.Call CWnd::SetWindowText to update the text in pane 0 only.2.Call CCmdUI::SetText in the status bar’s ON_UPDATE_COMMAND_UI handler.3.Call SetPaneText to update the text for any pane. */CStatusBar::SetPaneInfovoid SetPaneInfo( int nIndex, UINT nID, UINT nStyle, int cxWidth );CStatusBar::SetPaneTextBOOL SetPaneText( int nIndex, LPCTSTR lpszNewText, BOOL bUpdate = TRUE );方法1(添加定时器的方式)//step1static UINT indicators[] ={ID_SEPARATOR, // status line indicatorIDS_TIMER,    //创建资源ID,并在字符串资源中赋值IDS_PROGRESS,ID_INDICATOR_CAPS,ID_INDICATOR_NUM,ID_INDICATOR_SCRL,};//step2int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct){        SetTimer(1,1000,NULL);//添加定时器}//step3void CMainFrame::OnTimer(UINT nIDEvent) {int index = m_wndStatusBar.CommandToIndex(IDS_TIMER);//通过资源ID获取资源编号CTime t = CTime::GetCurrentTime();//获取当前系统时间        CString str = t.Format("%H:%M:%S");CClientDC dc(this);CSize cs = dc.GetTextExtent(str);//获取字符串长度m_wndStatusBar.SetPaneInfo(nindex,IDS_TIMER,SBPS_NORMAL,cs.cx);//根据字符串长度设置窗格长度m_wndStatusBar.SetPaneText(1,str,TRUE);CFrameWnd::OnTimer(nIDEvent);}方法2(ON_UPDATE_COMMAND_UI的方式)//step1static UINT indicators[] ={ID_SEPARATOR, // status line indicatorIDS_TIMER,    //创建资源ID,并在字符串资源中赋值IDS_PROGRESS,ID_INDICATOR_CAPS,ID_INDICATOR_NUM,ID_INDICATOR_SCRL,};//step2class CMainFrame : public CFrameWnd{protected:  // control bar embedded membersCStatusBar  m_wndStatusBar;protected:        afx_msg void OnProgress();//类的声明中添加响应函数DECLARE_MESSAGE_MAP()};//step3BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)ON_UPDATE_COMMAND_UI(IDS_TIMER, OnUpdatetime)//添加消息映射END_MESSAGE_MAP()//step4void CMainFrame::OnUpdatetime(CCmdUI* pCmdUI){CTime t = CTime::GetCurrentTime();        CString str = t.Format("%H:%M:%S");pCmdUI->SetText(str);}

demo3 (窗格显示鼠标坐标)//在View类中捕获并添加WM_MOUSEMOVE的响应函数//在View类中声明框架类void CStyleView::OnMouseMove(UINT nFlags, CPoint point) {// TODO: Add your message handler code here and/or call defaultCString str;str.Format("x=%d,y=%d", point.x, point.y);//((CMainFrame *)GetParent())->SetMessageText(str);//方法1//((CMainFrame *)GetParent())->GetMessageBar()->SetWindowText(str);//方法2        GetParent()->GetDescendantWindow(AFX_IDW_STATUS_BAR)->SetWindowText(str);//方法3CView::OnMouseMove(nFlags, point);}

demo4 (窗格显示进度条)//在框架类中添加CProgressCtrl类对象//在框架类中捕获并添加WM_PAINT消息的响应函数void CMainFrame::OnPaint() {CPaintDC dc(this); // device context for painting// TODO: Add your message handler code hereCRect rect;m_wndStatusBar.GetItemRect(2,&rect);if(!m_progress.m_hWnd){m_progress.Create(WS_CHILD|WS_VISIBLE, rect, &m_wndStatusBar,123);}else{m_progress.MoveWindow(rect);}m_progress.SetPos(50);// Do not call CFrameWnd::OnPaint() for painting messages}


原创粉丝点击