Thread: WIN32 Creating 3rd window failed Error 1407

  1. #1
    Grey Wizard C_Sparky's Avatar
    Join Date
    Sep 2009
    Posts
    50

    WIN32 Creating 3rd window failed Error 1407

    I am rather new to GUI programming under the Win32 API library. I was able to create a main window, and then a button you can click that brings up a message box. But when I try to create a second button (third window) in the same method as the second button it fails and gives me ERROR_CANNOT_FIND_WND_CLASS. But I have already registered the class, do I have to register another one?

    Code:
    #include <windows.h>  //include all the basics
    #include <tchar.h>    //string and other mapping macros
    #include <string>
    
    
    //=============================================================================
    //message processing function declarations
    LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);
    
    
    
    HWND button;
    HWND Button2;
    
    
    //=============================================================================
    int WINAPI WinMain(HINSTANCE hInst,HINSTANCE,LPSTR pStr,int nCmd)
    {
    LPCTSTR classname = "Window";
    WNDCLASSEX wcx={0};  
    
    wcx.cbSize         = sizeof(WNDCLASSEX);           
    wcx.lpfnWndProc    = WndProc;             
    wcx.hInstance      = hInst;          
    
    wcx.hIcon         = reinterpret_cast<HICON>(LoadImage(0,IDI_APPLICATION,
                                                IMAGE_ICON,0,0,LR_SHARED));
    wcx.hCursor       = reinterpret_cast<HCURSOR>(LoadImage(0,IDC_ARROW,
                                                  IMAGE_CURSOR,0,0,LR_SHARED));
    wcx.hbrBackground = reinterpret_cast<HBRUSH>(COLOR_BTNFACE+1);   
    wcx.lpszClassName = classname;
    wcx.style = CS_HREDRAW | CS_VREDRAW;
    
    if (!RegisterClassEx(&wcx))
      {
      MessageBox(NULL,"Register failed.","",MB_OK);
      return -1;
      }
    
    int desktopwidth=GetSystemMetrics(SM_CXSCREEN);
    int desktopheight=GetSystemMetrics(SM_CYSCREEN);
    
    HWND hwnd=CreateWindowEx(0,                     //extended styles
                             classname,     //name: wnd 'class'
                             _T("ButtonProg"), //wnd title
                             WS_OVERLAPPEDWINDOW,   //wnd style
                             desktopwidth/4,        //position:left
                             desktopheight/4,       //position: top
                             desktopwidth/2,        //width
                             desktopheight/2,       //height
                             0,                     //parent wnd handle
                             0,                     //menu handle/wnd id
                             hInst,                 //app instance
                             0);                    //user defined info
    if (!hwnd)
      {
      MessageBox(NULL,"Wnd failed.","",MB_OK);
      return -1;
      }
      
    button = CreateWindowEx(0,
                            "button",
                            "MessageBox",
                            WS_VISIBLE | WS_CHILD | BS_PUSHBUTTON,
                            20,
                            40,
                            100,
                            20,
                            hwnd,
                            NULL,
                            hInst,
                            NULL);
    Button2 = CreateWindowEx(0,
                                 "Button2",
                                 "Button2",
                                 WS_VISIBLE | WS_CHILD | BS_PUSHBUTTON,
                                 20,
                                 80,
                                 200,
                                 20,
                                 hwnd,
                                 NULL,
                                 hInst,
                                 NULL);
        DWORD Error = GetLastError();
        char ErrorBuffer[1024];
        wsprintf(ErrorBuffer, "Error creating. Error Code %d, Hex: %X.", Error, Error);
        MessageBox(NULL,ErrorBuffer,"",MB_OK);                        
    if(!radioButton){MessageBox(NULL,"radio failed.","",MB_OK);}
    
    ShowWindow(hwnd,nCmd); 
    UpdateWindow(hwnd);
    
    MSG msg;
    while (GetMessage(&msg,0,0,0)>0)
      {
      TranslateMessage(&msg);
      DispatchMessage(&msg);
      }
    return static_cast<int>(msg.wParam);
    
    }
    
    //=============================================================================
    
    LRESULT CALLBACK WndProc(HWND hwnd,UINT uMsg,WPARAM wParam,LPARAM lParam)
    {
        
    HWND hWndCtl = (HWND)lParam;
    switch (uMsg)
      {
      case WM_COMMAND:
            switch(wParam){
                case BN_CLICKED:
                    if(hWndCtl == button){
                        
                        MessageBox(NULL,"MsgBox","",MB_OK);
    
                        }
                break;
                }
            break;
      case WM_CREATE:
        break;
      case WM_DESTROY:
            
        PostQuitMessage(0);
        
        return 0;
        
      default:
        
        return DefWindowProc(hwnd,uMsg,wParam,lParam);  
        
      }
      
    }
    //=============================================================================
    Last edited by C_Sparky; 10-03-2009 at 08:56 PM.

  2. #2
    Registered User
    Join Date
    Sep 2009
    Posts
    37
    you dont define a window class at the 2nd and third window except u define for each button a new class!

  3. #3
    Registered User
    Join Date
    Sep 2009
    Posts
    37
    Code:
    button = CreateWindowEx(0,
                            "button",
                            "MessageBox",
                            WS_VISIBLE | WS_CHILD | BS_PUSHBUTTON,
    you define here the classsname as "button"
    Code:
    Button2 = CreateWindowEx(0,
                                 "Button2",
                                 "Button2",
                                 WS_VISIBLE | WS_CHILD | BS_PUSHBUTTON,
    you define here the classsname as "Button2"

    the class Button2 dont exists , so it wont find it?!!?
    the class name button is predefined by windows.h
    Last edited by punkywow; 10-04-2009 at 12:49 AM.

  4. #4
    Grey Wizard C_Sparky's Avatar
    Join Date
    Sep 2009
    Posts
    50
    Quote Originally Posted by punkywow View Post
    Code:
    button = CreateWindowEx(0,
                            "button",
                            "MessageBox",
                            WS_VISIBLE | WS_CHILD | BS_PUSHBUTTON,
    you define here the classsname as "button"
    Code:
    Button2 = CreateWindowEx(0,
                                 "Button2",
                                 "Button2",
                                 WS_VISIBLE | WS_CHILD | BS_PUSHBUTTON,
    you define here the classsname as "Button2"

    the class Button2 dont exists , so it wont find it?!!?
    the class name button is predefined by windows.h

    Haha oh my goodness, simple mistake, thanks. I completely rewrote it and it works perfectly now, thanks.

  5. #5
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    Also as you used the WS_CHILD style the HMENU param should be cast to the int resource ID number of the control, which should be unique on a window/dialog. (you have set them all to 0)
    "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. Profiler Valgrind
    By afflictedd2 in forum C++ Programming
    Replies: 4
    Last Post: 07-18-2008, 09:38 AM
  2. Window scrollbar
    By maxorator in forum Windows Programming
    Replies: 2
    Last Post: 10-07-2005, 12:31 PM
  3. My Window Class
    By Epo in forum Game Programming
    Replies: 2
    Last Post: 07-10-2005, 02:33 PM
  4. Tab Controls - API
    By -KEN- in forum Windows Programming
    Replies: 7
    Last Post: 06-02-2002, 09:44 AM
  5. Winamp Vis if anyone can help
    By Unregistered in forum Windows Programming
    Replies: 6
    Last Post: 01-27-2002, 12:43 AM