Thread: Creating a window through menu

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

    Creating a window through menu

    Im trying to create a window through my menu, instead of using a dialog. But when the window is created, it gets all the information that my main window has... I have no idea how to fix this. Here is my code:

    Code:
            case WM_COMMAND:
                 switch(GET_WM_COMMAND_ID(wParam,lParam))
                 {
                     case HWND_FILE_EXIT:
                          PostQuitMessage(0);
                     break;
                     
                     case HWND_BASE_CONVERT:
                          {
                              HWND hBaseC;
                              HINSTANCE hInstance_base;
                              HWND hBase_Edit_Bin;
                              HWND hBase_Edit_Oct;
                              HWND hBase_Edit_Hex;
                              HFONT hf_BaseC = (HFONT)GetStockObject(DEFAULT_GUI_FONT);
                              
                              RegisterClassEx(&bccl);
                              
                              hBaseC = CreateWindowEx(WS_EX_TOOLWINDOW,
                                                      BC_ClassName,
                                                      "Base Converter",
                                                      WS_SYSMENU,
                                                      CW_USEDEFAULT,CW_USEDEFAULT,
                                                      200,200,
                                                      hwnd,
                                                      NULL,
                                                      hInstance_base,
                                                      NULL);
                              
                              ShowWindow(hBaseC,SW_NORMAL);
                              UpdateWindow(hBaseC);
                          }
                     break;
                 }
            break;
    I tried many things, this is what its currently at =/.

    EDIT:

    Ok, i tried creating the window in the main function... and having it called when HWND_BASE_CONVERT was called... but its not displaying it to the screen ....

    im calling the window like so:

    Code:
            case WM_COMMAND:
                 switch(GET_WM_COMMAND_ID(wParam,lParam))
                 {
                     case HWND_FILE_EXIT:
                          PostQuitMessage(0);
                     break;
                     
                     case HWND_BASE_CONVERT:
                          {
                              ShowWindow(GetDlgItem(hwnd,HWND_BASE_CONVERT),SW_NORMAL);
                              UpdateWindow(GetDlgItem(hwnd,HWND_BASE_CONVERT));
                          }
                     break;
                 }
            break;
    Last edited by Homunculus; 02-12-2006 at 02:56 PM.

  2. #2
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    When you create child windows, you should specify the WS_CHILD attribute, you can use the WS_VISIBLE attribute too. The HMENU parameter can be used as an identifier.

  3. #3
    Registered User
    Join Date
    Feb 2006
    Posts
    71

    I dunno why this isnt working

    Ya, I forgot about declaring WS_CHILD =/, but I still get the same type of error ( making a window (but smaller) like my main window ).

    Things I tried:

    - creating a window class just for this tool window
    - making it show to screen using ShowWindow and WS_VISIBLE
    - Creating window in the main function and in the WM_COMMAND message

    I can't think of anything else that might work =/... right now I have it set up that I have my window created in the main function and assigning it to the identifier. Then using the identifier to print out the window to screen when the HWND_BASE_CONVERT message is handled.

    my code in the main function that creates the child window of hwnd

    Code:
        hBaseC = CreateWindowEx(WS_EX_TOOLWINDOW,
                                BC_ClassName,
                                "Base Converter",
                                WS_CHILD|WS_SYSMENU,
                                CW_USEDEFAULT,CW_USEDEFAULT,
                                200,200,
                                hwnd,
                                (HMENU)HWND_BASE_CONVERT,
                                NULL,
                                NULL);
    my code using to print it out

    Code:
            case WM_COMMAND:
                 switch(GET_WM_COMMAND_ID(wParam,lParam))
                 {
                     case HWND_FILE_EXIT:
                          PostQuitMessage(0);
                     break;
                     
                     case HWND_BASE_CONVERT:
                          {
                              HWND hBase_Edit = GetDlgItem(hwnd,HWND_BASE_CONVERT);
                              ShowWindow(hBase_Edit,SW_NORMAL);
                              UpdateWindow(hwnd);
                              UpdateWindow(hBase_Edit);
                          }
                     break;
                 }
            break;
    I figured hwnd might need to be updated since a child window was being created on it =/.

    EDIT:

    ahhh, i think i see my problem, ok, I THINK CreateWindowEx sends a WM_CREATE message, and if it does so... its recreating my edit controls ontop of it , since i have my edit controls (for main window) created in it. I am almost positive this is my problem. Please respond if you have any suggestions or w/e....
    Last edited by Homunculus; 02-12-2006 at 06:28 PM.

  4. #4
    Registered User
    Join Date
    Feb 2006
    Posts
    71

    I tried so many different ways =(

    I can't get it to work =/, can someone give me a working example, and tell me why this isnt working:

    Code:
    #include <windows.h> //using win32 api
    #include <windowsx.h>//for additional win32 api functions
    #include "identifiers.h"//so I can access my identifiers
    
    const char g_ClassName[]="hwnd window class name";
    
    LRESULT CALLBACK WndProc(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam)
    {
        switch(msg)
        {
            case WM_CLOSE:
                 DestroyWindow(hwnd);
                 break;
            
            case WM_DESTROY:
                 PostQuitMessage(0);
                 break;
            
            case WM_COMMAND:
                 switch(GET_WM_COMMAND_ID(wParam,lParam))
                    {
                        case HWND_FILE_EXIT:
                             PostQuitMessage(0);
                             break;
                        
                        case EXTRA_BASE_CONVERT:
                             ShowWindow(GetDlgItem(hwnd,EXTRA_BASE_CONVERT),SW_NORMAL);
                             UpdateWindow(GetDlgItem(hwnd,EXTRA_BASE_CONVERT));
                             break;
                    }
            break;
            
            case WM_SIZE:
                 {
                     HWND hEdit = GetDlgItem(hwnd,HWND_EDIT);
                     HFONT hfDefault = (HFONT)GetStockObject(DEFAULT_GUI_FONT);
                     RECT hwnd_cord;
                     
                     GetClientRect(hwnd,&hwnd_cord);
                     SetWindowPos(hEdit,NULL,0,0,hwnd_cord.right,hwnd_cord.bottom,SWP_NOZORDER);
                     SendMessage(hEdit,WM_SETFONT,(WPARAM)hfDefault,MAKELPARAM(false,0));
                 }
            break;
            
            default:
                 return DefWindowProc(hwnd,msg,wParam,lParam);
        }
    }
    
    int WINAPI WinMain(HINSTANCE     hInstance,
                       HINSTANCE hPrevInstance,
                       LPSTR          nCmdLine,
                       int            nCmdShow)
    {
        HWND hwnd;
        MSG Message;
        HWND hEdit;
        HWND hBaseC;
        WNDCLASSEX wc;
        
    
        wc.cbSize = sizeof(WNDCLASSEX);
        wc.cbWndExtra = 0;
        wc.cbClsExtra = 0;
        wc.style = CS_DBLCLKS;
        wc.hbrBackground = CreateSolidBrush(RGB(0,0,100));
        wc.hIcon = LoadIcon(NULL,IDI_APPLICATION);
        wc.hIconSm = LoadIcon(NULL,IDI_APPLICATION);
        wc.hInstance = hInstance;
        wc.hCursor = LoadCursor(NULL,IDC_ARROW);
        wc.lpszMenuName = MAKEINTRESOURCE(HWND_MENU);
        wc.lpszClassName = g_ClassName;
        wc.lpfnWndProc = WndProc;
        
        RegisterClassEx(&wc); 
        
        hwnd = CreateWindowEx(WS_EX_TOOLWINDOW,
                              g_ClassName,
                              "Base Converter/NotePad",
                              WS_SYSMENU,
                              CW_USEDEFAULT,CW_USEDEFAULT,
                              CW_USEDEFAULT,CW_USEDEFAULT,
                              HWND_DESKTOP,
                              NULL,
                              hInstance,
                              NULL);
        
        hEdit = CreateWindowEx(WS_EX_CLIENTEDGE,
                              "EDIT",
                              "",
                              WS_CHILD|WS_VISIBLE|WS_VSCROLL|ES_MULTILINE,
                              CW_USEDEFAULT,CW_USEDEFAULT,
                              CW_USEDEFAULT,CW_USEDEFAULT,
                              hwnd,
                              (HMENU)HWND_EDIT,
                              GetModuleHandle(NULL),
                              NULL);
        
        hBaseC = CreateWindowEx(WS_EX_TOOLWINDOW,
                                g_ClassName,
                                "Base Converter",
                                WS_CHILD|WS_SYSMENU,
                                CW_USEDEFAULT,CW_USEDEFAULT,
                                CW_USEDEFAULT,CW_USEDEFAULT,
                                hwnd,
                                (HMENU)EXTRA_BASE_CONVERT,
                                hInstance,
                                NULL);
        
        ShowWindow(hwnd,nCmdShow);
                                 
        UpdateWindow(hwnd);
        
        while(GetMessage(&Message,NULL,0,0)>0)
        {
            TranslateMessage(&Message);
            DispatchMessage (&Message);
        }
        
        return Message.wParam;
    }
    I tried so many things =/ I decided to to try and make a notepadish program and implement my base converter in the menu (to see if I could create child windows through my menu...). If I do get something to appear, its like a blank square.

  5. #5
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    Re-structured your program a bit.. to meet the programming style and arrangement taught by Charles Petzold.. Just wanted to give you something that would compile.. this should get you started:

    Code:
    #include<windows.h> //using win32 api
    //#include <windowsx.h>//for additional win32 api functions
    //#include "identifiers.h"//so I can access my identifiers
    HINSTANCE hInstance;
    const char g_ClassName[]="hwnd window class name";
    
    LRESULT CALLBACK WndProc(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam);
     
    int WINAPI WinMain(HINSTANCE     hInstance,
                       HINSTANCE hPrevInstance,
                       LPSTR          nCmdLine,
                       int            nCmdShow)
    {
        HWND hwnd;
        MSG Message;
        HWND hEdit;
        HWND hBaseC;
        WNDCLASSEX wc;
        
    
        wc.cbSize = sizeof(WNDCLASSEX);
        wc.cbWndExtra = 0;
        wc.cbClsExtra = 0;
        wc.style = CS_DBLCLKS;
        wc.hbrBackground = CreateSolidBrush(RGB(0,0,100));
        wc.hIcon = LoadIcon(NULL,IDI_APPLICATION);
        wc.hIconSm = LoadIcon(NULL,IDI_APPLICATION);
        wc.hInstance = hInstance;
        wc.hCursor = LoadCursor(NULL,IDC_ARROW);
        //wc.lpszMenuName = MAKEINTRESOURCE(HWND_MENU);
        wc.lpszClassName = g_ClassName;
        wc.lpfnWndProc = WndProc;
        
        if (!RegisterClassEx (&wc))
            return 0;
        
         
        hwnd = CreateWindowEx(WS_EX_TOOLWINDOW,
                              g_ClassName,
                              "Base Converter/NotePad",
                              WS_SYSMENU,
                              CW_USEDEFAULT,CW_USEDEFAULT,
                              CW_USEDEFAULT,CW_USEDEFAULT,
                              HWND_DESKTOP,
                              NULL,
                              hInstance,
                              NULL);
                              
          ShowWindow(hwnd,nCmdShow);
          
          while(GetMessage(&Message,NULL,0,0)>0)
        {
            TranslateMessage(&Message);
            DispatchMessage (&Message);
        }
        
        return Message.wParam;
    }
    
    LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
            
       static HWND hEdit, hBaseC, HWND_EDIT, EXTRA_BASE_CONVERT;
       
       switch(message)
       {
                      
        case WM_CREATE:  
        
             hEdit = CreateWindow("EDIT", NULL,
                                   WS_CHILD|WS_VISIBLE|WS_VSCROLL|ES_MULTILINE,
                                   40, 290, 500, 100, hwnd,
                                   (HMENU)HWND_EDIT,
                                   (HINSTANCE)GetWindowLong(hwnd, GWL_HINSTANCE),
                                   NULL);
        
             hBaseC = CreateWindow("BUTTON", NULL, WS_CHILD|WS_SYSMENU,
                                    CW_USEDEFAULT,CW_USEDEFAULT,
                                    CW_USEDEFAULT,CW_USEDEFAULT,
                                    hwnd,
                                    (HMENU)EXTRA_BASE_CONVERT,
                                    (HINSTANCE)GetWindowLong(hwnd, GWL_HINSTANCE),
                                    NULL); 
             break; 
             
        case WM_CLOSE:
             
                 DestroyWindow(hwnd);
                 break;     
                                     
        case WM_DESTROY:
             
             PostQuitMessage(0);
             break;   
             
        default:  
                  
             return DefWindowProc(hwnd, message, wParam, lParam);
             }
             
        return 0;
        
    }
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  6. #6
    Registered User
    Join Date
    Feb 2006
    Posts
    71

    err

    my source did compile o.0, i bet your using that abomination called MSVC++. (I have to use it for computer science 120, I find it the most disgusting thing ever)

    EDIT:

    ya, i just noticed if you paste that code into your compiler, it messes the spacing in 3 spots, making it not compile =/
    Last edited by Homunculus; 02-15-2006 at 02:32 PM.

  7. #7
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    it's just that i never seen anyone put their windows procedure first.. and then try to register a window.. and then have the message loop at the end.

    Mostly all i have seen is 1) register a window 2) message loop 3) windows procedure.


    i'm using dev-cpp for my windows projects.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  8. #8
    Registered User
    Join Date
    Feb 2006
    Posts
    71

    I see

    Ya, I didn't mean anything offensive if it came across that way by saying w/e I said about msvc++...

    I like to have all my functions defined above my main function. I'm not really sure why =/.

    But anyways, can someone PLEASE, help me lol.

  9. #9
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    There is no "user default" value (CW_USEDEFAULT) for Edit controls.

    For your hBaseC it seems relevent to research dialog boxes.

  10. #10
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    Why not register a second wndclass for your toolwindow and specify a different callback?

    >>i bet your using that abomination called MSVC++.


    Better get used to it.

    From my experience most professional software development (for MS systems) is done in MSVC.
    In all the MS 'houses' I have worked in, we use it. IMO MSVC is a very good IDE.
    "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

  11. #11
    chococoder
    Join Date
    Nov 2004
    Posts
    515
    Almost all professional development in C++ for Windows uses either Microsoft (70% or so), Borland, or Intel compilers (about 25% combined).

    And while VC6 was indeed very bad, the current versions are quite nice.
    Visual Studio .NET is very good indeed. I went for Borland at the time because Visual Studio 6 was a disaster (usabillity mostly), but Microsoft has come a very long way.

    Their compilers are also among the best for Windows. Standard compliance is now very good (lagging just a bit behind Borland) and the performance of the generated code is excellent.

  12. #12
    Registered User xds4lx's Avatar
    Join Date
    Nov 2001
    Posts
    630
    Visual studio 2005's standard complience is almost 100% complete finally. To take it another step look into using STLPort instead of the ms version of the STL.
    "only two things are infinite, the universe and human stupidity, and im not sure about the former." - albert einstein

  13. #13
    Registered User
    Join Date
    Feb 2006
    Posts
    71

    ...

    Offtopic:

    I don't care, its all what you prefer. I was calling it an abomination because I don't like it =P, not only is the debugger thing overrated, it creates a stdafx.h file that needs to be used in every project =/. (every project I tried with it anyways)

    Keep in mind, I have no experience with it. I only use it to do consol applications at school. Its really just small things that annoy me when using it.

    Ontopic

    Tonto - It didn't make a difference anyways ( tried it earlier using actual numbers to create the size etc.)

    novacain - I tried creating a new window class for my child tool window, but it didn't work, the only thing left to do is use a seperate callback =P (hope it works =/ )

  14. #14
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    You should show us updated code. For these simple controls you're creating;

    Code:
    LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
            
    	static HWND hEdit, hBaseC; 
    
    	const int 
    		HWND_EDIT = 101, 
    		EXTRA_BASE_CONVERT = 102;
       
    	switch(message)
    	{
                      
    		case WM_CREATE:  
        
    			hEdit = CreateWindow("EDIT", NULL,
    								WS_CHILD | WS_VISIBLE | WS_VSCROLL | ES_MULTILINE,
    								40, 100, 400, 300, hwnd, (HMENU) HWND_EDIT,
    								(HINSTANCE) GetWindowLong(hwnd, GWL_HINSTANCE),
    								NULL);
        
    			hBaseC = CreateWindow("BUTTON", "Your Thing", 
    								WS_CHILD | WS_SYSMENU | WS_VISIBLE,
    								40, 40, 100, 20, hwnd, (HMENU) EXTRA_BASE_CONVERT,
    								(HINSTANCE) GetWindowLong(hwnd, GWL_HINSTANCE),
    								NULL); 
    			break; 
             
    		case WM_CLOSE:
    
    			DestroyWindow(hwnd);
    			break;     
                                     
    		case WM_DESTROY:
    
    			PostQuitMessage(WM_QUIT);
    			break;   
             
    		default:  
    
    			return DefWindowProc(hwnd, message, wParam, lParam);
    	}     
    	return 0;
    }
    If you want another little child window like your parent, it seems reasonable to either as novacain said make a new window class, or to make a dialog.

  15. #15
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    >> tried creating a new window class for my child tool window, but it didn't work, the only thing left to do is use a seperate callback

    What went wrong with the second wndclass?

    To change the callback a window uses you can call SetWindowLongPtr()


    >>not only is the debugger thing overrated

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

    I suppose it does not seem important until you have a large or complex app but you will find it is a tool you will soon not be able to do without.
    "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