我正在尝试动态创建一个按钮。我已经阅读了一些其他资源,并编写了以下代码:
BEGIN_MESSAGE_MAP(Cdynamic_button_sdiView, CView)
// Standard printing commands
ON_BN_CLICKED(MYBUTTONID, OnMyBN_Click)
END_MESSAGE_MAP()
void Cdynamic_button_sdiView::OnInitialUpdate()
{
CView::OnInitialUpdate();
m_Button.Create(_T("Rearrange"), WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON, CRect(0, 0, 128, 32), this, MYBUTTONID); // here will create a button
}当我启动MFC应用程序时,我可以成功地制作一个按钮。问题是,当我尝试通过单击打开新文档时:

我收到一个错误,我的应用程序在m_Button.Create(_T("Rearrange"), WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON, CRect(0, 0, 128, 32), this, MYBUTTONID);崩溃了

发布于 2016-10-01 08:48:47
我用下面的代码解决了这个问题:
Cdynamic_button_sdiView::Cdynamic_button_sdiView()
{
// TODO: add construction code here
m_Button = NULL;
}
Cdynamic_button_sdiView::~Cdynamic_button_sdiView()
{
if (m_Button != NULL)
delete m_Button;
}
void Cdynamic_button_sdiView::OnInitialUpdate()
{
CView::OnInitialUpdate();
if (m_Button != NULL)
delete m_Button;
m_Button = new CButton;
m_Button->Create(_T("Rearrange"), WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON, CRect(0, 0, 128, 32), this, MYBUTTONID); // here will create a button
}问题可能是我不应该在OnInitialUpdate()中重新创建窗口
https://stackoverflow.com/questions/39796637
复制相似问题