CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Jul 2007
    Posts
    52

    Question [Solved]Adding a CWnd child window

    Hi all

    I am a newbie with CWnd classes and I would like to know how I can implement a little child window inside another one using the CWnd class (I don't want the resource template)

    I created a normal mfc dialog based project and added a CWnd class to my project, then added a member variable to the main dialog class and initialized the CWnd's constructor with a pointer to main dialog (parent) 'this'

    The constructor of the CWnd class is:

    CProp::CProp(CWnd* pParent)
    {
    //int x = CWnd::CreateEx(NULL, AfxRegisterWndClass(NULL), NULL, WS_CHILD|WS_VISIBLE|WS_DLGFRAME,0, 0, 120, 120, pParent, (HMENU)NULL);

    UINT nID = 1234;
    CWnd* pWnd = new CWnd;
    LPCTSTR ClassName = (LPCTSTR)AfxRegisterWndClass(CS_VREDRAW | CS_HREDRAW,NULL,(HBRUSH)COLOR_BACKGROUND + 1,NULL);


    pWnd->CreateEx(0, ClassName, "Win", WS_CHILD | WS_CAPTION|WS_VISIBLE|WS_DLGFRAME,
    0, 0, 580, 100, pParent->m_hWnd, (HMENU)NULL);

    pWnd->ShowWindow(SW_SHOWNORMAL);
    pWnd->CenterWindow();
    }


    I am always getting Warning: Window creation failed: GetLastError returns 0x0000057E that Lookup says: "Cannot create a top-level child window."


    If you'd be so kind to solve this problem and maybe linking me a guide or something to learn (after shown the child window) how to use pDX data exchanges and handlers with a CWnd window like above, I will be extremely grateful

    Any help appreciated, thanks again
    Last edited by NasterMain; April 9th, 2008 at 05:51 AM. Reason: Problem Solved

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Adding a CWnd child window

    Since the error is telling you that you can't create a top level child window, have you tried to set its parent hwnd to a non-null value?

    Code:
    pWnd->CreateEx(this, ClassName, "Win", WS_CHILD | WS_CAPTION|WS_VISIBLE|WS_DLGFRAME,
    0, 0, 580, 100, pParent->m_hWnd, (HMENU)NULL);

  3. #3
    Join Date
    Jul 2007
    Posts
    52

    Re: Adding a CWnd child window

    uh?

    First member is extended style, and pParent->m_hWnd is a valid handle to parent

  4. #4
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Adding a CWnd child window

    Ok, my mistake. Did you check the value of pParent->m_hWnd in the debugger?

  5. #5
    Join Date
    Nov 2003
    Location
    Belgium
    Posts
    8,150

    Re: Adding a CWnd child window

    Maybe there is a problem with your styles.
    According to MSDN:
    Quote Originally Posted by MSDN
    WS_CAPTION Creates a window that has a title bar (implies the WS_BORDER style). Cannot be used with the WS_DLGFRAME style
    .
    So try to remove the WS_DLGFRAME flag.
    Marc Gregoire - NuonSoft (http://www.nuonsoft.com)
    My Blog
    Wallpaper Cycler 3.5.0.97

    Author of Professional C++, 4th Edition by Wiley/Wrox (includes C++17 features)
    ISBN: 978-1-119-42130-6
    [ http://www.facebook.com/professionalcpp ]

  6. #6
    Join Date
    Jul 2007
    Posts
    52

    Re: Adding a CWnd child window

    You're both right, the CWnd value of pParent was right, but pParent->m_hWnd was null (i was calling the child creation before the parent was created) and there was the WS_DLGFRAME style

    Thanks very much!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured