Dialog Box error

This is a discussion on Dialog Box error within the Windows Programming forums, part of the Platform Specific Boards category; Hey all, I'm creating a dialog box in my program. However every time I go to create it, it fails. ...

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    102

    Dialog Box error

    Hey all, I'm creating a dialog box in my program. However every time I go to create it, it fails. Heres the code:
    (Windows Vista, Dev c++ 4.9.9.2)
    Resource:
    Code:
    #include <windows.h>
    
    #include "Dialogs.h"
    
    #if !defined (_WINDOWS) 
    #define _WINDOWS 
    #endif 
    
    
    DB_NEW DIALOG DISCARDABLE  50, 50, 700, 560
    STYLE DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU
    CAPTION "Create New.."
    FONT 8, "MS Sans Serif"
    
    BEGIN
    
        DEFPUSHBUTTON   "&OK",IDOK,174,18,50,14
        PUSHBUTTON      "&Cancel",IDCANCEL,174,35,50,14
    
    END
    Dialog Ids
    Code:
    #ifndef DialogTypes
    #define DialogTypes
    
    #define DB_New 99999
    
    #endif
    Section in Main that uses it
    Code:
    BOOL CALLBACK NewDlgProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam)
    {
        switch(Message)
        {
            case WM_INITDIALOG:
            return TRUE;
            
            case WM_COMMAND:
                switch(LOWORD(wParam))
                {
                    case IDOK:
                        EndDialog(hwnd, IDOK);
                    break;
                    case IDCANCEL:
                        EndDialog(hwnd, IDCANCEL);
                    break;
                }
                
            break;
            default:
                return FALSE;
        }
        return TRUE;
    
    }
                
                    
    
    LRESULT CALLBACK MainWindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
    {
        static HDC hDC;
        static HGLRC hRC;
        int height, width;
    
        // dispatch messages
        switch (uMsg)
        {   
        case WM_CREATE:         // window creation
            hDC = GetDC(hWnd);
            
            SetupPixelFormat(hDC);
            
            hRC = wglCreateContext(hDC);
            
            wglMakeCurrent(hDC, hRC);
                                
            HMENU MainMenu;
            HMENU FileMenu;
                    
            MainMenu=CreateMenu();  
            FileMenu=CreateMenu();  
            AppendMenu(FileMenu,MF_STRING,ID_New,"&New");  
            AppendMenu(FileMenu,MF_STRING,ID_Load,"&Load");  
            AppendMenu(FileMenu,MF_STRING,ID_Close,"&Close");
            AppendMenu(FileMenu,MF_STRING,ID_Save, "&Save");
            AppendMenu(FileMenu,MF_STRING,ID_SaveAs, "S&ave As..");
            AppendMenu(FileMenu,MF_SEPARATOR,0,"");  
            AppendMenu(FileMenu,MF_STRING,ID_Exit,"E&xit"); 
            InsertMenu(MainMenu,ID_File,MF_POPUP,(UINT)FileMenu,"File");  
                    
            HMENU SubMenus;
            SubMenus = CreateMenu();
                    
            AppendMenu(SubMenus,MF_STRING,ID_Wall,"&Wall");  
            AppendMenu(SubMenus,MF_STRING,ID_Floor,"&Floor");  
            AppendMenu(SubMenus,MF_STRING,ID_Stair,"&Stair");
            AppendMenu(SubMenus,MF_STRING,ID_Light,"&Light");
            InsertMenu(MainMenu,ID_Create,MF_POPUP,(UINT)SubMenus,"Create");  
                    
            HMENU SubMenus2;
            SubMenus2 = CreateMenu();
                    
            AppendMenu(SubMenus2,MF_STRING,ID_Undo,"&Undo");  
            AppendMenu(SubMenus2,MF_STRING,ID_Redo,"&Redo");
            AppendMenu(SubMenus2,MF_SEPARATOR,0,"");
            AppendMenu(SubMenus2,MF_STRING,ID_Properties,"&Properties");
            InsertMenu(MainMenu,ID_Edit,MF_POPUP,(UINT)SubMenus2,"Edit"); 
                    
            HMENU SubMenus3;
            SubMenus3 = CreateMenu();
                    
            AppendMenu(SubMenus3,MF_STRING,ID_Wireframe,"&Wireframe");  
            AppendMenu(SubMenus3,MF_STRING | MF_CHECKED,ID_Textured,"&Textured");  
            AppendMenu(SubMenus3,MF_STRING,ID_Regular,"&Regular");
            AppendMenu(SubMenus3,MF_SEPARATOR,0,"");  
            AppendMenu(SubMenus3,MF_STRING,ID_ResetView,"R&eset View"); 
            AppendMenu(SubMenus3,MF_SEPARATOR,0,"");   
            AppendMenu(SubMenus3,MF_STRING,ID_TopView,"&Top View");  
            AppendMenu(SubMenus3,MF_STRING,ID_XView,"&X View");
            AppendMenu(SubMenus3,MF_STRING,ID_ZView,"&Z View");
            InsertMenu(MainMenu,ID_View,MF_POPUP,(UINT)SubMenus3,"View");   
                    
            HMENU SubMenus4;
            SubMenus4 = CreateMenu();
                    
            AppendMenu(SubMenus4,MF_STRING,ID_Overview,"&Overview");  
            InsertMenu(MainMenu,ID_Help,MF_POPUP,(UINT)SubMenus4,"Help"); 
                    
            if (!SetMenu(hWnd,MainMenu)) {    return FALSE; }
            
            break;
    
        case WM_DESTROY:            // window destroy
        case WM_QUIT:
        case WM_CLOSE:                  // windows is closing
    
            // deselect rendering context and delete it
            wglMakeCurrent(hDC, NULL);
            wglDeleteContext(hRC);
    
            // send WM_QUIT to message queue
            PostQuitMessage(0);
            break;
    
        case WM_SIZE:
            
            windowHeight = HIWORD(lParam);        // retrieve width and height
            windowWidth = LOWORD(lParam);
            
            glViewport(0, 0, windowWidth, windowHeight);        // reset the viewport to new dimensions
            glMatrixMode(GL_PROJECTION);            // set projection matrix current matrix
            glLoadIdentity();                       // reset projection matrix
    
            // calculate aspect ratio of window
            gluPerspective(52.0f,(GLfloat)windowWidth/(GLfloat)windowHeight,0.0001f,1000.0f);
    
            break;
    
        case WM_ACTIVATEAPP:        // activate app
            break;
    
        case WM_PAINT:              // paint
            PAINTSTRUCT ps;
            BeginPaint(hWnd, &ps);
            EndPaint(hWnd, &ps);
            break;
    
        case WM_LBUTTONDOWN:        // left mouse button
            break;
    
        case WM_RBUTTONDOWN:        // right mouse button
            break;
    
        case WM_MOUSEMOVE:          // mouse movement
            break;
    
        case WM_LBUTTONUP:          // left button release
            break;
    
        case WM_RBUTTONUP:          // right button release
            break;
    
        case WM_KEYUP:
            break;
    
        case WM_KEYDOWN:
    
            break;
            
        case WM_COMMAND:
            
             switch(LOWORD(wParam))
             {
                  case ID_Exit:
                        PostQuitMessage(0);
                        exiting = true;
                        
                        break;
                        
                  case ID_New:
                    {
                        if(!pCurrentLevel)
                        {
                            int ret = DialogBox(GetModuleHandle(NULL), 
                            MAKEINTRESOURCE(DB_New), hWnd, NewDlgProc);
                            
                            if(ret == IDOK){
                                MessageBox(hWnd, "Dialog exited with IDOK.", "Notice",
                                    MB_OK | MB_ICONINFORMATION);
                            }
                            else if(ret == IDCANCEL){
                                MessageBox(hWnd, "Dialog exited with IDCANCEL.", "Notice",
                                    MB_OK | MB_ICONINFORMATION);
                            }
                            else if(ret == -1){
                                MessageBox(hWnd, "Dialog failed!", "Error",
                                    MB_OK | MB_ICONINFORMATION);
                            }
    
                            pCurrentLevel = new Level();
                            pCurrentLevel->theme = Hedge_Maze;
                            pCurrentLevel->changed = true;
                            pCurrentLevel->width = 35;
                            pCurrentLevel->depth = 35;
                            pCurrentLevel->floors = 1;
                            
                            if(!pCurrentLevel->Setup())
                                MessageBox(hWnd, "Error setting up level.", "Error", MB_OK);
                            
                            break;
                        }
                        
                        else
                            MessageBox(hWnd, "You already have a level open", "New Level..", MB_OK);
                            
                        break;
                    }
                    
                  case ID_Floor:
                    {
                        if(pCurrentLevel)
                            ++pCurrentLevel->floors;
                            
                        else
                            MessageBox(hWnd, "You don't have a level open.", "New Floor..", MB_OK);
                            
                        break;
                    }
                        
             }
    
        default:
            break;
        }
        return DefWindowProc(hWnd, uMsg, wParam, lParam);
    }
    Can anyone tell me why it always fails?
    My Favorite Programming Line:
    Code:
    #define true ((rand() % 2) ? true : false)

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Can you Define "fails"? You post a fairly large program, but without any details of what works and what fails, it means that the reader has to figure out which parts of your code works and which doesn't - which is, if you did't realize it, quite hard work.

    --
    Mats

  3. #3
    Registered User
    Join Date
    Apr 2007
    Posts
    102
    Yes, good point lol sorry. Heres the problem:

    Code:
    switch(LOWORD(wParam))
             {
                  case ID_Exit:
                        PostQuitMessage(0);
                        exiting = true;
                        
                        break;
                        
                  case ID_New:
                    {
                        if(!pCurrentLevel)
                        {
                            int ret = DialogBox(GetModuleHandle(NULL), 
                            MAKEINTRESOURCE(DB_New), hWnd, NewDlgProc);  
                            
                            if(ret == IDOK){
                                MessageBox(hWnd, "Dialog exited with IDOK.", "Notice",
                                    MB_OK | MB_ICONINFORMATION);
                            }
                            else if(ret == IDCANCEL){
                                MessageBox(hWnd, "Dialog exited with IDCANCEL.", "Notice",
                                    MB_OK | MB_ICONINFORMATION);
                            }
                            else if(ret == -1){
                                MessageBox(hWnd, "Dialog failed!", "Error",
                                    MB_OK | MB_ICONINFORMATION);  
                            }
    
                            pCurrentLevel = new Level();
                            pCurrentLevel->theme = Hedge_Maze;
                            pCurrentLevel->changed = true;
                            pCurrentLevel->width = 35;
                            pCurrentLevel->depth = 35;
                            pCurrentLevel->floors = 1;
                            
                            if(!pCurrentLevel->Setup())
                                MessageBox(hWnd, "Error setting up level.", "Error", MB_OK);
                            
                            break;
                        }
    My dialog never displays when I hit the new menu, it always returns -1.
    The parts in bold are the error prone parts most likely.
    My Favorite Programming Line:
    Code:
    #define true ((rand() % 2) ? true : false)

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Try calling GetLastError() to see what the actual error is.

    --
    Mats

  5. #5
    Registered User Joelito's Avatar
    Join Date
    Mar 2005
    Location
    Tijuana, BC, México
    Posts
    308
    I think because you didn't add the WS_VISIBLE in your dialog editor.
    * PC: Intel Core 2 DUO E6550 @ 2.33 GHz with 2 GB RAM: Archlinux-i686 with xfce4.
    * Laptop: Intel Core 2 DUO T6600 @ 2.20 GHz with 4 GB RAM: Archlinux-x86-64 with xfce4.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner Needs help in Dev-C++
    By Korrupt Lawz in forum C++ Programming
    Replies: 20
    Last Post: 09-28-2010, 01:17 AM
  2. Avoiding Global variables
    By csonx_p in forum Windows Programming
    Replies: 32
    Last Post: 05-19-2008, 12:17 AM
  3. How to monitor process creation?
    By markiz in forum Windows Programming
    Replies: 31
    Last Post: 03-17-2008, 02:39 PM
  4. file reading
    By gunghomiller in forum C++ Programming
    Replies: 9
    Last Post: 08-07-2007, 10:55 PM
  5. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21