我按MSDN上的例子:
CButton myButton1, myButton2, myButton3, myButton4;
// Create a push button.
myButton1.Create(_T("My button"), WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON, 
   CRect(10,10,100,30), pParentWnd, 1);
// Create a radio button.
myButton2.Create(_T("My button"), WS_CHILD|WS_VISIBLE|BS_RADIOBUTTON, 
   CRect(10,40,100,70), pParentWnd, 2);
// Create an auto 3-state button.
myButton3.Create(_T("My button"), WS_CHILD|WS_VISIBLE|BS_AUTO3STATE, 
   CRect(10,70,100,100), pParentWnd, 3);
// Create an auto check box.
myButton4.Create(_T("My button"), WS_CHILD|WS_VISIBLE|BS_AUTOCHECKBOX, 
   CRect(10,100,100,130), pParentWnd, 4);自己这样编写代码:
CButton cbMyButton;
int iret = cbMyButton.Create(_T("My button"), WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON, CRect(10,10,100,30), this, 1);但在界面上无法显示,是怎么回事?请有此经验者多多赐教!

解决方案 »

  1.   

    另外,如果你的cbMyButton是局部变量的话,出了函数那个右大括号生命期就结束了
    试着将其改成类的成员变量试试
      

  2.   

    结合1,2楼,将CButton类变量定义成类成员,如果是在框架类中的函数创建的话就定义成cmainframe类的成员变量,或者这样 CButton *cbMyButton=new CButton;cbMyButton->create();
    创建了后还要显示出来,cbMyButton->ShowWindow(SW_SHOWNORMAL);
      

  3.   

    如果你一定要定义为局部变量,你可以这么写:#define IDC_BUTTON 112 CButton m_btn;
    m_btn.Create(_T("My button"), WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,  
    CRect(10,10,100,33), this, IDC_BUTTON);
    CFont* pFont = GetFont();
    m_btn.SetFont(pFont);
    m_btn.UnsubclassWindow(); CString strText(_T(""));
    GetDlgItemText(IDC_BUTTON, strText);
    AfxMessageBox(strText);
      

  4.   

    如果你一定要定义为局部变量,你可以这么写:#define IDC_BUTTON 112 CButton m_btn;
    m_btn.Create(_T("My button"), WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,  
    CRect(10,10,100,33), this, IDC_BUTTON);
    CFont* pFont = GetFont();
    m_btn.SetFont(pFont);
    m_btn.UnsubclassWindow(); CString strText(_T(""));
    GetDlgItemText(IDC_BUTTON, strText);
    AfxMessageBox(strText);
      

  5.   

    如果你一定要定义为局部变量,你可以这么写:#define IDC_BUTTON 112 CButton m_btn;
    m_btn.Create(_T("My button"), WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,  
    CRect(10,10,100,33), this, IDC_BUTTON);
    CFont* pFont = GetFont();
    m_btn.SetFont(pFont);
    m_btn.UnsubclassWindow(); CString strText(_T(""));
    GetDlgItemText(IDC_BUTTON, strText);
    AfxMessageBox(strText);