0

I am trying to create CWnd derived class at runtime but CWnd::Create fails. I have no idea why. Here is minimal code that show the problem:

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");
}

In void CMFCTestApplicationDlg::DoDataExchange(CDataExchange* pDX) I tried to create a CButton derived class object and CWnd derived class object. The first one created successfully but CWnd derived class object fails to create. Whats wrong with this code?

Arun A S
  • 6,421
  • 4
  • 29
  • 43
IKM2007
  • 716
  • 1
  • 8
  • 31
  • 2
    First parameter to [CWnd::Create](https://msdn.microsoft.com/en-us/library/0yhc9kx4.aspx): *"Pointer to a null-terminated string that contains the name of a **registered** system window class"*. Did you register this class? Unrelated to this (or maybe not), but you should **really** remove the `LPCTSTR` casts. Pass properly typed arguments. Use the `_T` or `TEXT` macro if you need to support both MBCS and Unicode builds. – IInspectable Mar 02 '15 at 12:07
  • IInspectable, you're right, that was the problem. I have added AfxRegisterWndClass() call and passed return name as class name to Create function and now seems it works. Thanks! – IKM2007 Mar 02 '15 at 12:24
  • It is not supposed that you create controls on DoDataExchange. That is very ... wrong. Every time an UpdateData occurs, a new control is created. And you are doing new's without delete's, possibly doing memory leaking. You should create them on OnCreate or OnInitDialog and their variables should be class members. – sergiol Jul 11 '15 at 21:44

0 Answers0