Thread: When to call a Dialog Box?

  1. #1
    Registered User
    Join Date
    Jan 2010
    Posts
    216

    Lightbulb When to call a Dialog Box?

    Hi there,

    I just about managed to get a very simple dialog box running in a small windows program. I'm actually quite pleased I made it even this far.

    It looks quite nice But... it causes the whole program to hang up and whenever I click anywhere on the entire window (including the box itself) I get that nice little noise Windows makes when something is wrong. I cannot even minimize or exit the window so I have to run in debug mode and close it from there.

    My suspicions are that I have called the dialog box in an incorrect way or at the wrong time and have screwed up the Windows message loop.

    Here's a portion of the code:

    Code:
    ShowWindow(hwnd, nCmdShow);    
    // Run the message loop.
    
    DialogBoxW(hInstance, (LPCWSTR)IDD_PROPPAGE_SMALL, hwnd, DlgProc);
    
    MSG msg = { };
    while (GetMessage(&msg, NULL, 0, 0) > 0)
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
    return 0;
    I've added bold to the dialog box function call. The DlgProc message handling function is as basic as I could get:

    Code:
    LRESULT CALLBACK DlgProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
    {
        switch (uMsg)
        {        
            case WM_INITDIALOG:
            {
                
            }
    
            case WM_COMMAND:
            {
                switch (LOWORD(wParam))
                {
                    // Button1 Pressed
                    case IDC_BUTTON1:
                    {
                        EndDialog(hwnd, 0);
                        return 0;
                    } break;
                }
            } 
        } // switch [uMsg]
        return 0;
    }
    I checked the identifier for the only button on there and it is indeed IDC_BUTTON1. If anyone can help please do thanks, or recommend an up to date book I can get for Win32 programs so I can get my learning sorted out. I wouldn't even know where to start

    I hope that made some sense, thanks

  2. #2
    Registered User
    Join Date
    Dec 2017
    Posts
    1,652
    It would be more usual to call DialogBoxW in response to a command from the main window, like from a menu item. If you don't want to make a menu item yet, you could just use a left-click of the window as the command to open the dialog box.
    Code:
    LRESULT CALLBACK MainWndProc(HWND hwnd, UINT uMsg WPARAM wParam, LPARAM lParam)
    {
        switch (uMsg) {
        case WM_LBUTTONDOWN:
            DialogBoxW(...);
            return 0;
        default:
            return DefWindowProc(hwnd, uMsg, wParam, lParam);
        }
        return 0;
    }
    I'm assuming the dialog box has a button to exit.

    Even though it's old, I think "Programming Windows 5th ed. Charles Petzold, 1998" is probably still basically correct. 6th edition is for C#, not C, so watch out.
    And that's the world in a nutshell, an appropriate receptacle.

  3. #3
    Registered User
    Join Date
    Jan 2010
    Posts
    216
    Hey thanks very much John I've ordered that book too, thought it was a bit dear at first until I saw how big it is lol!

    Cheers

  4. #4
    Registered User
    Join Date
    Jan 2010
    Posts
    216

    Thumbs up Solved (for now.....)

    Hi there,

    I finally got this to work. I bought the book John recommended and read through the code in one of the Dialog Box examples. I couldn't see too much difference in the way the DB was called. It helped me understand how to call it within the windows procedure function though as up to that point I'd been struggling to find a way to pass the hInstance into the DB call.

    It still failed however. I wondered then if it was a setting issue rather than a code issue and ultimately it was. When I checked the settings in the template for Charles Petzold's DB there were two settings that were different. One was I think Disabled being True in the Behaviour tab and the other (which was what ultimately fixed it) was the Style setting in the Appearance tab. It was set to Child and when I changed it to Popup that was it.

    Here's the code for the WinProc function:

    Code:
    LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
    {
        static HINSTANCE hInstance;
    
        switch (uMsg)
        {
        case WM_CREATE:
            hInstance = ((LPCREATESTRUCT) lParam)->hInstance;        
            return 0;
    
        case WM_LBUTTONDOWN:
            DialogBoxW(hInstance, (LPCWSTR)102, hwnd, DlgProc);
            return 0;
    
        case WM_DESTROY:
            PostQuitMessage(0);
            return 0;
    
        case WM_PAINT:
        {
            PAINTSTRUCT ps;
            HDC hdc = BeginPaint(hwnd, &ps);
    
            // All painting occurs here, between BeginPaint and EndPaint.
    
            FillRect(hdc, &ps.rcPaint, (HBRUSH)(COLOR_WINDOW + 1));
    
            EndPaint(hwnd, &ps);
        }
        return 0;
    
        }
        return DefWindowProc(hwnd, uMsg, wParam, lParam);
    }
    And here's the DlgProc function:

    Code:
    LRESULT CALLBACK DlgProc(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
    {
        switch (uMsg)
        {        
        case WM_INITDIALOG:        
            return TRUE;        
    
        case WM_COMMAND:
            switch (LOWORD(wParam))
            {
            // Button1 Pressed        
            case IDC_BUTTON1:
                EndDialog(hDlg, 0);
                return TRUE;                
            }
            break;
           
        } // switch [uMsg]
        return FALSE;
    }
    And that makes a nice simple DB which opens with a left mouse click and has just one button on it named Button1 which closes the DB. If you wish you can keep opening and closing the DB in that fashion.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. trying to call Dialog Box from within dll
    By bazelboday in forum Windows Programming
    Replies: 2
    Last Post: 04-18-2014, 03:57 AM
  2. Replies: 3
    Last Post: 06-24-2008, 03:14 PM
  3. Calling up a dialog from a dialog handler function
    By Welder in forum Windows Programming
    Replies: 2
    Last Post: 11-03-2007, 10:45 PM
  4. Replies: 7
    Last Post: 10-10-2006, 11:03 AM
  5. Using a dll to call a dialog in OnInitDialog of another.
    By mhandlon in forum Windows Programming
    Replies: 1
    Last Post: 03-22-2006, 11:10 AM

Tags for this Thread