0

我试图CWnd在运行时创建派生类但CWnd::Create失败了。我不知道为什么。这是显示问题的最少代码:

MFCTestApplicationDlg.h

#pragma once

class c_CustomButton : public CButton
{
protected:
    DECLARE_MESSAGE_MAP()

    virtual void PreSubclassWindow();

public:
    c_CustomButton();
    virtual ~c_CustomButton();
};


class TestWindow : public CWnd
{
public:
    TestWindow();
    virtual ~TestWindow();

protected:
    DECLARE_MESSAGE_MAP()
    DECLARE_DYNCREATE(TestWindow)  
};

// CMFCTestApplicationDlg dialog
class CMFCTestApplicationDlg : public CDialogEx
{
...
}

MFCTestApplicationDlg.cpp

//

#include "stdafx.h"
#include "MFCTestApplication.h"
#include "MFCTestApplicationDlg.h"
#include "afxdialogex.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

/*==========================================================================*/
c_CustomButton::c_CustomButton()
{
}

/*==========================================================================*/
c_CustomButton::~c_CustomButton()
{
}

BEGIN_MESSAGE_MAP(c_CustomButton, CButton)
END_MESSAGE_MAP()

/*==========================================================================*/
void c_CustomButton::PreSubclassWindow()
{
    CButton::PreSubclassWindow();

    ModifyStyle(0, BS_OWNERDRAW);
}

IMPLEMENT_DYNAMIC(TestWindow, CWnd)

TestWindow::TestWindow()
{

}

TestWindow::~TestWindow()
{
}


BEGIN_MESSAGE_MAP(TestWindow, CWnd)
END_MESSAGE_MAP()

void CMFCTestApplicationDlg::DoDataExchange(CDataExchange* pDX)
{
    CDialogEx::DoDataExchange(pDX);

    c_CustomButton* pColoredButton = new c_CustomButton;
    pColoredButton->Create((LPCTSTR)"", 0, CRect(), this, 0);// successeded
    pColoredButton->SetWindowTextW((LPCTSTR)"test");

    TestWindow* pTestWindow = new TestWindow;
    pTestWindow->Create((LPCTSTR)"TestWindow", (LPCTSTR)"TestWindowName", 0, CRect(), this, 0);// failed
    pTestWindow->SetWindowText((LPCTSTR)"test");
}

void CMFCTestApplicationDlg::DoDataExchange(CDataExchange* pDX)我尝试创建CButton派生类对象和CWnd派生类对象。第一个创建成功,但CWnd派生类对象创建失败。这段代码有什么问题?

4

0 回答 0