Thread: Taskbars; 'Cannot create top level child window'

  1. #1
    Registered User
    Join Date
    Jun 2006
    Posts
    36

    Taskbars; 'Cannot create top level child window'

    My current problem involves taskbars. I've added libcomctl32.a to the linker stuff in my project options in Dev C++ and included commctrl.h and it compiles OK. I have the code in my WM_CREATE for the main window. HWND hStatus is outside of it.
    Code:
            InitCommonControls();
    
    	hStatus = CreateWindowEx(0, STATUSCLASSNAME, NULL,
            WS_CHILD | WS_VISIBLE | SBARS_SIZEGRIP, 0, 40, 0, 40,
            g_hWnd, (HMENU)ID_STATUS, GetModuleHandle(NULL), NULL);
    
            int statwidths[] = {100, -1};
    
        	SendMessage(hStatus, SB_SETPARTS, 2, (LPARAM)statwidths);
        	SendMessage(hStatus, SB_SETTEXT, 0, (LPARAM)"Hi there :)");
    This is from the winprog status bar tutorial. Problem is, CreateWindowEx returns NULL and the error code is 1406 which apparently is 'Cannot create top level child window'. what? MSDN says something about it, but it is very brief and it's not from the developer perspective.

    A child window is 'any window that is not a top level window', but I can't see what's going on in the above that makes it like that. I read other posts that said the code there works fine. Any ideas?

  2. #2
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342
    Maybe you made a mistake similar to the one in your other post?
    The reason I say that, is because that code compiles and runs fine for me.

  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Let me guess. You create the main window like this:
    Code:
    g_hWnd = CreateWindowEx(...);
    The effect of this is: g_hWnd doesn't get a value until CreateWindowEx returns. Which is after WM_CREATE is dispatched to the new window. (The reason for that is, if WM_CREATE returns FALSE, window creation fails.)

    So the g_hWnd you use in WM_CREATE is still NULL - to the system, it looks like you want the window to be top-level, which contradicts you passing WS_CHILD.

    The solution is to never use global window handles in the WndProc; always use the HWND passed to the procedure.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  4. #4
    Registered User
    Join Date
    Jun 2006
    Posts
    36
    Thanks, now the status bar appears just fine. I changed the application to use 1 big parent window and child windows for the status bar and scroll area. Now I'd like to know if there's a GetSytemMetric-esque function / message for the status bars so I can figure out sizes for the scrolling child window when resizing and such. MSDN states the font determines the height but ...

    -I can't find a function that returns the height of the font.
    -There's still the rest of the area that isn't taken up by text.

    I first figured that the window size you create the status bar with determines it's height but it turns out it's not that easy.

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    I can't find a function that returns the height of the font.
    GetFontMetrics. Or just GetFont/GetObject, the LOGFONT structure might contain the necessary stuff.

    There's still the rest of the area that isn't taken up by text.
    That ought to be a simple calculation based on the font size.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multiple Top Level Windows
    By quark_77 in forum Windows Programming
    Replies: 0
    Last Post: 07-13-2008, 02:32 PM
  2. Just starting Windows Programming, School me!
    By Shamino in forum Windows Programming
    Replies: 17
    Last Post: 02-22-2008, 08:14 AM
  3. how i create a window whith all it's elements
    By rasheed in forum Windows Programming
    Replies: 1
    Last Post: 05-31-2006, 06:53 PM
  4. Adding buttons, edit boxes, etc to the window
    By rainmanddw in forum Windows Programming
    Replies: 1
    Last Post: 04-10-2006, 03:07 PM
  5. Why only 32x32? (OpenGL) [Please help]
    By Queatrix in forum Game Programming
    Replies: 2
    Last Post: 01-23-2006, 02:39 PM