在vc中如何实现下面功能:
比如一个计算器程序,其上有一二十来个button ,假设id 为button1,button2,......
这里它的值并不一定是连续的,现在我希望通过"button"和一个int整数组合成一个button的id ,可以通过什么方法实现呢?我记得原来见过用宏写的,但找不到了。

解决方案 »

  1.   

    自己定义一个宏,里面有(Button, id),实现里用##把他们连起来,如Button##id
      

  2.   

    CButton m_button[10];
    m_button[0].GetsubclassCtrlID( this, IDC_BUTTON0 );是不是这个意思?
      

  3.   

    正确写法,上面的错了,可以按照数组访问。
    m_edit[ 0 ].SubclassDlgItem( IDC_EDIT1, this );
    m_edit[ 1 ].SubclassDlgItem( IDC_EDIT2, this );
    m_edit[ 2 ].SubclassDlgItem( IDC_EDIT3, this );
    m_lbl[ 0 ].SubclassDlgItem( IDC_STATIC1, this );
    m_lbl[ 1 ].SubclassDlgItem( IDC_STATIC2, this );
    m_lbl[ 2 ].SubclassDlgItem( IDC_STATIC3, this );
      

  4.   

    比如象下面的功能:
    for(int i=0;i+=3;i<19)
    {
       ... get the IDC_BUTTONi  //就是要简化这里的做法,根据i取得IDC_BUTTONi
       pButton->SetWindowText(strInfo);
    }
      

  5.   

    看一下《Windows程序设计》第5版里的计算器程序,对你会有启发。
      

  6.   

    #define COMBINE(Button, ID) Button##ID;
    SetWindowText(GetDlgItem(hWnd, COMBINE(IDC_BUTTON, i)), strInfo);
      

  7.   

    CString str1,str2;
    str1 = "BUTTON";
    str2.Format(_T("%d"),id);
    str1 += str2;
    str1中就是你索要的
      

  8.   


    learn and attention and up
      

  9.   

    jspring,我尝试的过程中有这样的问题:由于宏是预编译的,而里面的i是一个变量,它只有到具体运行的时候才有值呀!所以如果写
    SetWindowText(GetDlgItem(hWnd, COMBINE(IDC_BUTTON, 3)), strInfo);
    SetWindowText(GetDlgItem(hWnd, COMBINE(IDC_BUTTON, 4)), strInfo);
    这样的都完全正确,但i不是为常量是编译就有问题了,它会认为IDC_BUTTONi没有定义?你认为呢?