Thread: DialogBox() Always returns -1

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    183

    DialogBox() Always returns -1

    Hello.

    Sorry, I realize there are millions of threads about dialog boxes here, but I've searched through them loads and haven't yet found an answer to my problem. Like most noobs here, I'm using the tutorial at www.winprog.net. Anyways, heres my code:

    Windows.cpp

    Code:
    #include <windows.h>
    
    #define IDC_STATIC '-1'
    
    const char g_szClassName[] = "WindowClass";
    HWND hWindowHandle;
    
    ////////////////////////////////////////////////////////////////////////////////
    ////////////////////////////// Control1 procedure //////////////////////////////
    ////////////////////////////////////////////////////////////////////////////////
    
    BOOL CALLBACK Control1DlgProc(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;
    }
    
    ////////////////////////////////////////////////////////////////////////////////
    ////////////////////////////////////////////////////////////////////////////////
    ////////////////////////////////////////////////////////////////////////////////
    
    // Step 4: the Window Procedure
    
    LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
    {
        switch(msg)
        {
            case WM_CREATE:
                
                int Control1_Result;
                Control1_Result = DialogBox(GetModuleHandle(NULL), MAKEINTRESOURCE(101), hwnd, Control1DlgProc);
                
                if(Control1_Result == -1)
                {
                    MessageBox(hWindowHandle, "Dialog failed!", "Error", MB_OK | MB_ICONINFORMATION);
                }    
                
                break;
            
            case WM_CLOSE:
                DestroyWindow(hwnd);
                break;
                
            case WM_DESTROY:
                PostQuitMessage(0);
                break;
                
            default:
                return DefWindowProc(hwnd, msg, wParam, lParam);
        }
        
        return 0;
    }
    
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
        LPSTR lpCmdLine, int nCmdShow)
    {
        WNDCLASSEX wc;
        MSG MainMessage;
    
        //Step 1: Registering the Window Class
        
        wc.cbSize        = sizeof(WNDCLASSEX);
        wc.style         = 0;
        wc.lpfnWndProc   = WndProc;
        wc.cbClsExtra    = 0;
        wc.cbWndExtra    = 0;
        wc.hInstance     = hInstance;
        wc.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
        wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
        wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 11);
        wc.lpszMenuName  = NULL;
        wc.lpszClassName = g_szClassName;
        wc.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);
    
        if(!RegisterClassEx(&wc))
        {
            MessageBox(NULL, "Window Registration Failed!", "Error!", MB_ICONEXCLAMATION | MB_OK);
            return 0;
        }
    
        // Step 2: Creating the Window
        
        hWindowHandle = CreateWindowEx(
            WS_EX_CLIENTEDGE | WS_EX_TOPMOST,
            g_szClassName,
            "Windows Application",
            WS_DLGFRAME,
            120, 120, 240, 240,
            NULL, NULL, hInstance, NULL);
    
        if(hWindowHandle == NULL)
        {
            MessageBox(NULL, "Window Creation Failed!", "Error!", MB_ICONEXCLAMATION | MB_OK);
            return 0;
        }
    
        ShowWindow(hWindowHandle, nCmdShow);
        UpdateWindow(hWindowHandle);
    
        // Step 3: The Message Loop
        
        while(GetMessage(&MainMessage, NULL, 0, 0) > 0)
        {
            TranslateMessage(&MainMessage);
            DispatchMessage(&MainMessage);
        }
        
        return MainMessage.wParam;
    }
    Control1.rc

    Code:
    #include <windows.h>
    
    #if !defined IDC_STATIC
    #define IDC_STATIC -1
    #endif
    
    IDD_Control1 DIALOG DISCARDABLE  120, 120, 240, 65
    
    STYLE DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU
    CAPTION "Windows Application"
    FONT 8, "MS Sans Serif"
    
    BEGIN
        
        DEFPUSHBUTTON   "&Yes",IDYES,174,18,50,14
        PUSHBUTTON      "&No",IDNO,174,35,50,14
        GROUPBOX        "Login", IDC_STATIC,7,7,225,52
        CTEXT           "Do you wish to login to Windows Application?", IDC_STATIC,16,18,144,33
        
    END
    Control1.h

    Code:
    #define IDD_CONTROL1 101
    My problem is that the dialog simply dosen't show. Hence the title.
    Any suggestions?
    Cheers!

  2. #2
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    It could be a copying error, but it appears that you haven't #included Control1.h at the top of Control1.rc. Also, IDD_CONTROL1 is different to IDD_Control1.

    The general style is to include the resource header (Control1.h) in both the resource file and the code file and then use the constants rather than using numbers directly.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    183
    Thanks for the reply.

    Ok so I changed it to:

    Code:
    Control1_Result = DialogBox(GetModuleHandle(NULL), MAKEINTRESOURCE(IDD_Control1), hwnd, Control1DlgProc);
    But I get: 55 C:\Dev-Cpp\Programs\Test\Windows.cpp `IDD_Control1' undeclared (first use this function)

    Even though I changed Control1.h to: #define IDD_Control1 101

    What's going on there? It says it is undefined when it isn't. Wierd.
    Thanks for the help and any more you can lend.

  4. #4
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    To reiterate anonytmouse's solution(and add a little):

    In Windows.cpp, replace:
    Code:
    #include <windows.h>
    
    #define IDC_STATIC '-1'
    with
    Code:
    #include <windows.h>
    #include "Control1.h"
    In Control1.rc, replace
    Code:
    #include <windows.h>
    
    #if !defined IDC_STATIC
    #define IDC_STATIC -1
    #endif
    with
    Code:
    #include <afxres.h>
    #include "Control1.h"
    This should ensure all the proper definitions are where they are supposed to be, as described by anonytmouse.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    183
    Ah. All is now clear. Except one thing: What is: afxres.h?

    But it's working now!
    Thanks for all your help.

  6. #6
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    >>What is: afxres.h?<<

    It was added to MinGW a few years ago to increase compatibility with existing msvc specific code that used resources; all it does is #include windows.h and #define IDC_STATIC (take a peek yourself in the 'include' directory of your dev-cpp or mingw installation, if you're interested).
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Lame null append cause buffer to crash
    By cmoo in forum C Programming
    Replies: 8
    Last Post: 12-29-2008, 03:27 AM
  2. Polynomials and ADT's
    By Emeighty in forum C++ Programming
    Replies: 20
    Last Post: 08-19-2008, 08:32 AM
  3. Replies: 2
    Last Post: 04-12-2004, 01:37 AM
  4. Proper way to end a dialogbox.
    By spoon_ in forum Windows Programming
    Replies: 2
    Last Post: 01-06-2003, 05:47 PM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM