0
votes

I want to add simple Cedit to my derived GUI class from CWnd. This class is simple container and treat the same as Panel in MFC. in constructor of class I add simple CEdit instance in the class. but the when I instantiated the panel in client dialog, the panel shows but the button does not show.why it doesn't show. The panel code

Panel header file

#pragma once
#include "afxwin.h"
class CPanel :
public CWnd
{
public:
CPanel(void);
~CPanel(void);

virtual void PreSubclassWindow();
virtual void DoDataExchange(CDataExchange* pDX);
CEdit *txt;
DECLARE_MESSAGE_MAP()

};

panel source file

#include "stdafx.h"
#include "Panel.h"


CPanel::CPanel(void)
{
WNDCLASS wndcls;
HINSTANCE hins=AfxGetInstanceHandle();
if(!(::GetClassInfo(hins,_T("CPanelCtrl"),&wndcls))){
    wndcls.style=CS_DBLCLKS|CS_HREDRAW|CS_VREDRAW;
    wndcls.lpfnWndProc=::DefWindowProc;
    wndcls.cbClsExtra=wndcls.cbWndExtra=0;
    wndcls.hInstance=hins;
    wndcls.hIcon=NULL;
    wndcls.hCursor=AfxGetApp()->LoadStandardCursor(IDC_CROSS   );
    wndcls.hbrBackground=(HBRUSH)(COLOR_3DFACE+13);
    wndcls.lpszMenuName=NULL;
    wndcls.lpszClassName=_T("CPanelCtrl");

    txt=new CEdit();
    txt->Create(ES_PASSWORD,CRect(10,10,25,35),this,1);

    if (!AfxRegisterClass(&wndcls))
    {
        AfxThrowResourceException();
        return;
    }
    else{

        return;
    }

}
}


CPanel::~CPanel(void)
{
}


void CPanel::PreSubclassWindow()
{
// TODO: Add your specialized code here and/or call the base class

CWnd::PreSubclassWindow();
}


void CPanel::DoDataExchange(CDataExchange* pDX)
{
// TODO: Add your specialized code here and/or call the base class

CWnd::DoDataExchange(pDX);
}
BEGIN_MESSAGE_MAP(CPanel, CWnd)
END_MESSAGE_MAP()

In dialog box in OninitDialog method I do like this

panel=new CPanel();
panel->Create(L"CPanelCtrl",L"Hello ", WS_VISIBLE , CRect(70, 70, 400, 200), this, 1);
2

2 Answers

0
votes

Two hints:

first: you assign to your CEdit the same ID as to the CPanel: 1.

Second: you create CEdit inside CPanel constructor, I would try creating it inside CPanel WM_CREATE handler, because inside CPanel constructor HWND of CPanel is not yet assigned.

0
votes
  1. Your code to create the edit controls only runs once, because after the window class is registered the edit control is never be created.
  2. You can't create a child window, when the parent window is created.
  3. Create child windows on in the WM_CREATE handler of the parent.
  4. If you always have an edit control, why do you use a pointer to it. Just create a simple member.
  5. You should use WS_CHILD if the edit control is located inside the panel.