zoukankan      html  css  js  c++  java
  • 在MFC对话框中添加状态栏

    如果我们想实现在MFC对话框中添加状态栏显示,如何例如分状态栏为两列,第一列显示鼠标的当前位置,第二列显示当前的时间,(如上图)。

    1. 首先,打开在资源视图的String Table并添加两个ID:ID_INDICATOR_NISH 和ID_INDICATOR_TIME,如下图

    2. 在该对话框的头文件中添加一个CStatusBar类对象

     

    CStatusBar m_bar;

     

     

    3. 打开该对话框的cpp文件,并在最顶端添加以下代码:

     

    static UINT BASED_CODE indicators[]=
    {
        ID_INDICATOR_NISH,
        ID_INDICATOR_TIME
    };
    
    

    4. 接下来创建状态栏,在OnInitDialog()函数如下代码:

     

     

    m_bar.Create(this);//创建状态栏
    
    m_bar.SerIndicators(indicators, sizeof(indicators)/sizeof(UINT)); //设置状态栏数目
    
    CRect rect;
    GetClientRect(&rect);
    //设置各栏长度
    m_bar.SetPaneInfo(0, ID_INDICATOR_NISH, SBPS_NORMAL, rect.Width()-100);
    m_bar.SetPaneInfo(1, ID_INDICATOR_TIME, SBPS_STRETCH, 0);
    //在ping屏幕上绘制状态栏
    
    
    RepositionBars(AFX_IDW_CONTROLBAR_FIRST, AFX_IDW_CONTROLBAR_LAST, ID_INDICATOR_TIME);

    
    
    
    
    
    5. 至此,对话框上的状态栏创建已经成功了。若是用户喜欢,可以单独设置状态栏背景颜色,

    可以在OnInitDialog()函数中添加代码:

    m_bar.GetStatusBarCtrl().SetBKColor(RGB(180,180,180));

     当设置背景颜色无效是,请检查编码方式选择的是什么,应该是和编程环境有关。比如Unicode。或者选择使用多字节字符集

    6. 添加时钟显示。首先在OnInitDialog()中添加

    SetTimer(100,1000,NULL);

    之后添加WM_TIMER的相应函数:

    void CDlgStatusBarDlg::OnTimer(UINT nIDEvent)
    {
        if(nIDEvent==100)
        {
            CTime t1;
            t1=CTime::GetCurrentTime();
            m_bar.SetPaneText(1,t1.Format("%H:%M:%S"));
        }
        CDialog::OnTimer(nIDEvent);
    }
    7. 添加XY坐标显示。重写函数OnMouseMove():
    void CDlgStatusBarDlg::OnMouseMove(UINT nFlags, CPoint point) 
    {
        CString s;
        s.Format("X=%d Y=%d",point.x,point.y);
        m_bar.SetPaneText(0,s);
        CDialog::OnMouseMove(nFlags, point);
    }

    至此,编译运行程序,就可以看到预期的效果了。

    方法二:
      UINT array[4];
     
    for(int i=0;i<4;i++)
     
    {
       array[i] = 1001 + i;
      }
     
    m_StatusBar.Create(this); //创建状态栏窗口
     
    m_StatusBar.SetIndicators(array,sizeof(array)/sizeof(UINT)); //添加面板
     
    for(int n=0;n<4;n++)
      {
       m_StatusBar.SetPaneInfo(n,array[n],0,100); //设置面板宽度
      }
     
    CTime time = CTime::GetCurrentTime();
     
    m_StatusBar.SetPaneText(0,_T("当前用户:"));//设置面板文本
     
    m_StatusBar.SetPaneText(1,_T("TM"));
     
    m_StatusBar.SetPaneText(2,_T("当前日期:"));
     
    m_StatusBar.SetPaneText(3,time.Format("%Y-%m-%d"));
     
    RepositionBars(AFX_IDW_CONTROLBAR_FIRST,AFX_IDW_CONTROLBAR_LAST,0);
  • 相关阅读:
    C++ Primer 学习笔记_104_特殊工具与技术 --嵌套类
    [AngularJS + Webpack] Requiring CSS & Preprocessors
    [AngularJS + Webpack] Requiring Templates
    [AngularJS + Webpack] ES6 with BabelJS
    [Flux] 3. Actions
    [RxJS] Aggregating Streams With Reduce And Scan using RxJS
    [RxJS] map vs flatMap
    [RxJS] Stream Processing With RxJS vs Array Higher-Order Functions
    [MODx] Solve cannot upload large file
    [React + webpack] hjs-webpack
  • 原文地址:https://www.cnblogs.com/jinjiangongzuoshi/p/3281695.html
Copyright © 2011-2022 走看看