My child window is active yet still appears greyed out

Hi all,

I have made the simplest window program possible. It simply creates a parent window. I then try to create a child window, which works fine. I can move it around it behaves ok. I gave it a caption in a titlebar. But whenever I launch my program, the title bar for the child is always greyed out, even if I am dragging it around the screen. Also when the program starts, it seems the curved area around the top corners of the child window haven't been painted and there is a spot of black there. When I move the child they get painted correctly.

I can't figure out what style I have correctly entered to make it always appear greyed out, and why the corners are not initially the bg color of the parent window. Here I create the child window in my parent's CREATE message:

        case WM_CREATE:

        hwndChild= CreateWindow( szChildClass,
                                                  "MyChildWindow",
                                                  WS_VISIBLE | WS_THICKFRAME | WS_MINIMIZEBOX |WS_MAXIMIZEBOX | WS_CAPTION | WS_CHILDWINDOW | WS_CLIPSIBLINGS,
                                                  100, 100, 300, 200,
                                                  hwndParent,
                                                  (HMENU) 0,
                                                  (HINSTANCE) GetWindowLong( hwnd, GWL_HINSTANCE ),
                                                  NULL );

        ShowWindow(hwndChild, SW_SHOW);
        UpdateWindow( hwndChild );

        break;

My child's WndProc is nothing fancy and has all the basic parts to it. The only other thing I could see having an effect is the way I register my child window. I reigster it in WinMain() right after the parent has been registered:

               // parent has just been registered.

                wndchildclass .style            = CS_HREDRAW | CS_VREDRAW;
                wndchildclass .lpfnWndProc      = ChildWndProc;
                wndchildclass .cbWndExtra      = sizeof( long );
                wndchildclass .hIcon            = NULL;
                wndchildclass .lpszClassName      = szChildClass;
                wndchildclass .hbrBackground      = (HBRUSH) GetStockObject( WHITE_BRUSH );
                wndclass.hInstance            = hInstance;
                wndclass.hCursor            = LoadCursor( NULL, IDC_ARROW );

                RegisterClass( &wndchildclass );

What am I missing that makes the child window not be greyed out when you're moving it etc?

Thanks!
Already_GoogledAsked:
Who is Participating?
 
drichardsConnect With a Mentor Commented:
Interesting that if you take away WS_CHILD from the style, the title bar works as expected.  What does this style really do?
0
 
Jaime OlivaresSoftware ArchitectCommented:
I see some omissions in your class declaration

          wndchildclass.cbClsExtra    = 0 ;
          wndchildclass.cbWndExtra    = sizeof (WORD);
          wndchildclass.lpszMenuName  = NULL ;
          wndchildclass.lpszClassName = szChildClass;    // don't see a class name

Also your problem could be related to your ChildWndProc() function.

 
0
 
Already_GoogledAuthor Commented:
My apologies, here is my full wndchildclass:

wndchildclass:.style = CS_HREDRAW | CS_VREDRAW;
wndchildclass:.lpfnWndProc = ChildWndProc;
wndchildclass:.cbClsExtra = 0;
wndchildclass:.cbWndExtra = sizeof(WORD);
wndchildclass:.hInstance = hInstance;
wndchildclass:.hIcon = NULL;
wndchildclass:.hCursor = LoadCursor( NULL, IDC_ARROW );
wndchildclass:.hbrBackground = (HBRUSH) GetStockObject( LTGRAY_BRUSH );
wndchildclass:.lpszMenuName = NULL;
wndchildclass:.lpszClassName = szChildClass;


with the class name globally defined as:

TCHAR szChildClass[] = TEXT( "Child" );


And here is my child window proc:

LRESULT CALLBACK ChildWndProc( HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam )
{
    HDC hdc;
    PAINTSTRUCT ps;
   
    switch( message )
    {
        case WM_CREATE:
            return 0;

        case WM_PAINT:
            hdc = BeginPaint( hwnd, &ps );
            EndPaint( hwnd, &ps );
            return 0;
    }
    return DefWindowProc( hwnd, message, wParam, lParam );
}
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

 
Jaime OlivaresSoftware ArchitectCommented:
Hmm... I think could be lacking of overlapped attribute. Try with:

hwndChild= CreateWindow(szChildClass, "MyChildWindow",WS_OVERLAPPEDWINDOW|WS_CHILD, 100, 100, 300, 200,
    hwndParent, (HMENU) 0, (HINSTANCE) GetWindowLong( hwnd, GWL_HINSTANCE ), NULL );


0
 
Already_GoogledAuthor Commented:
Shoot still no good, even with styles only including:

WS_OVERLAPPEDWINDOW | WS_CHILD

This is so odd as the child is completely functional yet always remains greyed out!

0
 
Jaime OlivaresSoftware ArchitectCommented:
Really it is not easy to help you with pure WinAPI call. That's why MFC is useful, it does the dirty tasks for you. I have reviewed some old projects and I have used WS_POPUP style for this kind of windows also I have used NULL for classname, but make separated tests.
0
 
Already_GoogledAuthor Commented:
I posted the complete skeleton code here below, I've made sure it compiles as is. If you drop it into a blank project you should see the same characteristics? If you can't spot what I'm doing wrong perhaps I can turn to MFC, thank you!


#include <windows.h>

LRESULT CALLBACK WndProc( HWND, UINT, WPARAM, LPARAM );
LRESULT CALLBACK ChildWndProc( HWND, UINT, WPARAM, LPARAM );

TCHAR   szChildClass[] = TEXT( "ChildClass" );


int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
                              PSTR szCmdLine, int iCmdShow )
{
    static TCHAR szAppName[] = TEXT( "MyAttemptAtChildWindows" );
    HWND        hwnd;
    MSG         msg;
    WNDCLASS    wndclass, wndchildclass;

    wndclass.style          = CS_HREDRAW | CS_VREDRAW;
    wndclass.lpfnWndProc    = WndProc;
    wndclass.cbClsExtra     = 0;
    wndclass.cbWndExtra     = 0;
    wndclass.hInstance      = hInstance;
    wndclass.hIcon          = LoadIcon( NULL, IDI_APPLICATION );
    wndclass.hCursor        = LoadCursor( NULL, IDC_ARROW );
    wndclass.hbrBackground  = (HBRUSH) GetStockObject( LTGRAY_BRUSH );
    wndclass.lpszMenuName   = NULL;
    wndclass.lpszClassName  = szAppName;

    if( !RegisterClass( &wndclass ) )
    {
        MessageBox( NULL, TEXT( "Couldn't register wndclass!" ),
                    szAppName, MB_ICONERROR );
        return 0;
    }

    // Register our child window.
    wndchildclass.style         = CS_HREDRAW | CS_VREDRAW;
    wndchildclass.lpfnWndProc   = ChildWndProc;
    wndchildclass.cbClsExtra    = 0;
    wndchildclass.cbWndExtra    = sizeof( long );
    wndchildclass.hInstance          = hInstance;
    wndchildclass.hIcon              = NULL;
    wndchildclass.hCursor       = LoadCursor( NULL, IDC_ARROW );
    wndchildclass.hbrBackground = (HBRUSH) GetStockObject( WHITE_BRUSH );
    wndchildclass.lpszMenuName  = NULL;
    wndchildclass.lpszClassName = szChildClass;

    if( !RegisterClass( &wndchildclass ) )
    {
        MessageBox( NULL, TEXT( "Couldn't register wndchildclass!" ),
                    szAppName, MB_ICONERROR );
        return 0;
    }

    hwnd = CreateWindow( szAppName, TEXT( "ParentWindow" ),
                         WS_OVERLAPPEDWINDOW,
                         CW_USEDEFAULT, CW_USEDEFAULT,
                         CW_USEDEFAULT, CW_USEDEFAULT,
                         NULL, NULL, hInstance, NULL );

    ShowWindow( hwnd, iCmdShow );
    UpdateWindow( hwnd );

    while( GetMessage( &msg, NULL, 0, 0 ) )
    {
        TranslateMessage( &msg );
        DispatchMessage( &msg );
    }
    return msg.wParam;
}

LRESULT CALLBACK WndProc( HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam )
{
    static HWND      hwndChild;

    switch( message )
    {
    case WM_CREATE:

        hwndChild = CreateWindow( szChildClass,
                                  "ChildWindow",
                                  WS_OVERLAPPEDWINDOW | WS_CHILD,
                                  100, 100, 300, 200,
                                  hwnd,
                                  (HMENU) 0,
                                  (HINSTANCE) GetWindowLong( hwnd, GWL_HINSTANCE ),
                                  NULL );

        ShowWindow(hwndChild, SW_SHOW);
        UpdateWindow( hwndChild );

        return 0;

    case WM_DESTROY:
        PostQuitMessage( 0 );
        return 0;
    }
    return DefWindowProc( hwnd, message, wParam, lParam );
}

LRESULT CALLBACK ChildWndProc( HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam )
{
    HDC         hdc;
    PAINTSTRUCT ps;
   
    switch( message )
    {
    case WM_CREATE:
        return 0;

    case WM_PAINT:
        hdc = BeginPaint( hwnd, &ps );
        EndPaint( hwnd, &ps );
        return 0;
    }
    return DefWindowProc( hwnd, message, wParam, lParam );
}
0
 
Already_GoogledAuthor Commented:
The child style will allow the parent window to control the children. As in, if you move the parent window, the children will move with it. The parent will also clip the children if they try to go outside of it. Popup style windows turned out to be ok for my purposes in this post, but, still not sure why it's impossible to have the title bar be active with a child.
0
 
Already_GoogledAuthor Commented:
Hi Dan,

drichards had answered another question I had posted in the meantime, worth only 45 pts, I had asked him to post in this one so I could accept in here as well to give him the full number of points I thought he deserved, so it all worked out nicely.

Thanks for the link, I'll check it out,

Best regards
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.