我在CMYDlg类中写了一个绘图函数:
CButton btn;
void CMYDlg::DrawButton()
{
btn.Create(_T(""),WS_CHILD|WS_VISIBLE, CRect(10, 10, 16, 16), this, 10);
}然后在类外声明了该类的指针:
CMYDlg *mydlg = NULL;为什么在全局函数:
void  fun()
{
   mydlg->DrawButton();
}
的时候程序会崩溃?

解决方案 »

  1.   

    mydlg为NULL,调用无效的指针当然会崩溃.建议
    void fun()
    {
    CMyDlg *pMydlg = new CMyDlg;
    pMydlg->Create(NULL,IDD_MYDLG)//IDD_MYDLG为对话框类的模板
    pMydlg->DrawButton();
    }
      

  2.   

    1,mydlg不能为空,否则非法访问
    2,还必须调用mydlg->create,创建对话框
      

  3.   

    先谢谢两位了!为什么单文档类:
    CMYView* myView = NULL;void DispGrid(bool bEnable)
    {
        myView->GrawButton();
    }其中:
    class CMYView : public CViewvoid CMYView::GrawButton()
    {
    btn.Create(_T(""),WS_CHILD|WS_VISIBLE, CRect(111, 134, 123+16, 145+16), this, 10);
    }
    运行正常?