Thread: Creating a window through menu

  1. #16
    Registered User
    Join Date
    Feb 2006
    Posts
    71

    ...

    ok, well, I got some new code now, I went and looked into modeless dialog boxes lol, but I dont want to create my window through my resource file, so I came up with this:

    Code:
    #include <windows.h>
    #include "hex.h"
    
    const char g_ClassName[]="hwnd window class name";
    const char g_ChildClassName[]="child window class name";
    HWND child_window;
    HINSTANCE hInstance_child;
    
    LRESULT CALLBACK WndProc(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam)
    {
        switch(msg)
        {
            case WM_CLOSE:
                 PostQuitMessage(0);
                 break;
            
            case WM_DESTROY:
                 PostQuitMessage(0);
                 break;
                 
            case WM_COMMAND:
                 {
                    switch(LOWORD(wParam))
                    {
                        case HWND_FILE_EXIT:
                             PostQuitMessage(0);
                             break;
                        
                        case EXTRA_BASE_CONVERT:
                             ShowWindow(child_window,SW_SHOW);
                             UpdateWindow(child_window);
                             break;
                    }
                 }
            break;
            
            case WM_CREATE:
                 {
                     child_window = CreateWindowEx(WS_EX_CLIENTEDGE,
                                                   g_ChildClassName,
                                                   "Base Converter",
                                                   WS_OVERLAPPED|WS_CHILD|WS_SYSMENU,
                                                   10,10,
                                                   150,150,
                                                   hwnd,
                                                   (HMENU)EXTRA_BASE_CONVERT,
                                                   hInstance_child,
                                                   NULL);
                 }
            break;                                 
            
            default:
                 return DefWindowProc(hwnd,msg,wParam,lParam);
        }
    }
    
    bool CALLBACK chWndProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam)
    {
        switch(message)
        {
            case WM_CLOSE:
                 PostQuitMessage(0);
                 break;
            
            case WM_DESTROY:
                 PostQuitMessage(0);
                 break;
            
            default:
                 return DefWindowProc(hwnd,message,wParam,lParam);
        }
        return true;
    }
    
    int WINAPI WinMain(HINSTANCE     hInstance,
                       HINSTANCE hPrevInstance,
                       LPSTR          nCmdLine,
                       int            nCmdShow)
    {
        HWND hwnd;
        MSG Message;
        WNDCLASSEX wc;
        WNDCLASSEX wc_child;
        
        
        wc.cbSize = sizeof(WNDCLASSEX);
        wc.cbWndExtra = 0;
        wc.cbClsExtra = 0;
        wc.style = 0;
        wc.hbrBackground = CreateSolidBrush(RGB(0,0,100));
        wc.hInstance = hInstance;
        wc.hIcon = LoadIcon(NULL,IDI_WINLOGO);
        wc.hIconSm = LoadIcon(NULL,IDI_APPLICATION);
        wc.hCursor = LoadCursor(NULL,IDC_ARROW);
        wc.lpszMenuName = MAKEINTRESOURCE(HWND_MENU);
        wc.lpszClassName = g_ClassName;
        wc.lpfnWndProc = WndProc;
        
        wc_child.cbSize = sizeof(WNDCLASSEX);
        wc_child.cbWndExtra = 0;
        wc_child.cbClsExtra = 0;
        wc_child.style = 0;
        wc_child.hbrBackground = CreateSolidBrush(RGB(0,100,0));
        wc_child.hInstance = hInstance;
        wc_child.hIcon = LoadIcon(NULL,IDI_WINLOGO);
        wc_child.hIconSm = LoadIcon(NULL,IDI_APPLICATION);
        wc_child.hCursor = LoadCursor(NULL,IDC_ARROW);
        wc_child.lpszMenuName = NULL;
        wc_child.lpszClassName = g_ChildClassName;
        wc_child.lpfnWndProc = WndProc;
        
        RegisterClassEx(&wc);
        RegisterClassEx(&wc_child);
        
        hwnd = CreateWindowEx(WS_EX_CLIENTEDGE,
                              g_ClassName,
                              "Window Title",
                              WS_OVERLAPPEDWINDOW,
                              200,200,
                              200,200,
                              HWND_DESKTOP,
                              NULL,
                              hInstance,
                              NULL);
        
        ShowWindow(hwnd,nCmdShow);
        UpdateWindow(hwnd);
        
        while(GetMessage(&Message,NULL,0,0)>0)
        {
            TranslateMessage(&Message);
            DispatchMessage (&Message);
        }
        
        return Message.wParam;
    }
    ok, I think I know why its not working, the message loop has no way of getting to the second callback function (for my child windw). In the tutorial I seen in modeless dialog boxes, they used a message loop something like this:

    Code:
        while(GetMessage(&Msg, NULL, 0, 0))
        {
            if(!IsDialogMessage(g_hToolbar, &Msg))
            {
                TranslateMessage(&Msg);
                DispatchMessage(&Msg);
            }
        }
    since my child window isn't a dialog box I can't do that? (im about to go and try now =/)... If it doesn't work, whats another way that I could use in my message loop?

    >>The ability to watch the values (or messages) generated as your code runs is essential to fix some bugs.

    I never knew it could do that o.0, I should pay more attention =/. I can see how that could be very useful...

    EDIT:

    bleh, some errors in there, like I didn't put chWndProc for my child_wc class... I would just like to know what im doing wrong >.> because when i change it to that, it complains about datatype errors, something about bool
    Last edited by Homunculus; 02-18-2006 at 07:28 PM.

  2. #17
    Registered User
    Join Date
    Feb 2006
    Posts
    71

    ...

    I could easily make it through my resource file and all, but I would rather not. I want to be able to put edit boxes and stuff in my child window, but I have no idea how to through the resource file =/

  3. #18
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    >>wc_child.lpfnWndProc = WndProc;

    This is the callback the wndclass will use. Currently both are set to the same callback.

    Look at Tonto's and the brain's callbacks. Notice how they all return 0 for messages that get processed.

    This is required to tell the OS that default processing is not required.

    May be added by the compiler or you should be getting a warning "not all control paths return a value".
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Creating a menu system
    By ICool in forum C Programming
    Replies: 9
    Last Post: 09-17-2007, 12:18 PM
  2. WM_CAPTION causing CreateWindowEx() to fail.
    By Necrofear in forum Windows Programming
    Replies: 8
    Last Post: 04-06-2007, 08:23 AM
  3. creating a child window
    By rakan in forum Windows Programming
    Replies: 2
    Last Post: 01-23-2007, 03:22 PM
  4. Why only 32x32? (OpenGL) [Please help]
    By Queatrix in forum Game Programming
    Replies: 2
    Last Post: 01-23-2006, 02:39 PM
  5. Winamp Vis if anyone can help
    By Unregistered in forum Windows Programming
    Replies: 6
    Last Post: 01-27-2002, 12:43 AM