Thread: dialog box problem keeps haunting me

  1. #1
    Madly in anger with you
    Join Date
    Nov 2005
    Posts
    211

    dialog box problem keeps haunting me

    hey all,
    I am just experimenting as I seem to be having very bad luck with creating dialog boxes with the API.

    well, I FINALLY got some code to work that actually displays the dialog box (usually the code will compile fine, but when I execute it there is no sign of the dialog box).

    here it is:

    Code:
    #include <windows.h>
    #include "resource.h"
    
    BOOL CALLBACK DialogProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
    {
    	switch(uMsg) {
    	case WM_INITDIALOG:
    		SetDlgItemText(hwndDlg, IDC_EDIT, "hello there");
    		return TRUE;
    	case WM_CLOSE:
    		EndDialog(hwndDlg, 0);
    		return TRUE;
    	}
    	return FALSE;
    }
    
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
    {
    	return DialogBox(hInstance, MAKEINTRESOURCE(IDD_DIALOG), NULL, (DLGPROC)DialogProc);
    }
    yes, indeed very simple code. IDC_EDIT is an edit box control on the dialog (obviously). as I said, I figured I'd go real simple with this one since I was having so many troubles with other controls. I was so happy to see that the dialog actually appeared for once.

    now here goes my question. on the dialog, there is also an OK (IDOK) button and a Cancel (IDCANCEL) button (by default). now, watch how I handle these button messages:

    Code:
    BOOL CALLBACK DialogProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
    {
    	switch(uMsg) {
    	case WM_INITDIALOG:
    		SetDlgItemText(hwndDlg, IDC_EDIT, "hello there");
    		return TRUE;
    	case WM_COMMAND:
    		switch(LOWORD(wParam)) {
    		case IDOK:
    			EndDialog(hwndDlg, 0);
    			return TRUE;
    		case IDCANCEL:
    			EndDialog(hwndDlg, 0);
    			return TRUE;
    		}
    	case WM_CLOSE:
    		EndDialog(hwndDlg, 0);
    		return TRUE;
    	}
    	return FALSE;
    }
    looks ok, right? thats what I thought. turns out that when I run this after this very small modification I get another one of those non appearing dialog boxes. lol

    could anyone here please tell me what looks wrong with the way I am handling those button messages? I would really like to know what I am doing wrong to keep getting these non appearing dialog boxes (like what can cause behaviour like this? I am sure I'm not the only one here that has had this problem). the program appears to be completing execution, so maybe I am adding a return statement somewhere where I shouldn't be?

    any help would be greatly appreciated, this same problem has been bugging me for a while now.


    thanks in advance!

    Intel Core 2 Quad Q6600 @ 2.40 GHz
    3072 MB PC2-5300 DDR2
    2 x 320 GB SATA (640 GB)
    NVIDIA GeForce 8400GS 256 MB PCI-E

  2. #2
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    What would happen in your code if a WM_COMMAND message came that was neither IDOK nor IDCANCEL?

    There's no default condition in your switch(LOWORD(wParam)) statement, nor is there a return or break after that switch, so any WM_COMMAND apart from IDOK or IDCANCEL will reach the end of that inner switch statement and then keep executing the contents of case WM_CLOSE.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  3. #3
    Madly in anger with you
    Join Date
    Nov 2005
    Posts
    211
    aha. my problem is not writing a break after WM_COMMAND (execution was falling through to WM_CLOSE). I was not expecting any messages to come other than the ones made by my controls. thanks for pointing that out Cat, here is my dialog callback now:

    Code:
    BOOL CALLBACK DialogProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
    {
    	switch(uMsg) {
    	case WM_INITDIALOG:
    		SetDlgItemText(hwndDlg, IDC_EDIT, "hello there");
    		return TRUE;
    	case WM_COMMAND:
    		switch(LOWORD(wParam)) {
    		case IDOK:
    			EndDialog(hwndDlg, 0);
    			return TRUE;
    		case IDCANCEL:
    			EndDialog(hwndDlg, 0);
    			return TRUE;
    		default:
    			return DefWindowProc(hwndDlg, uMsg, wParam, lParam);
    		}
    		break;
    	case WM_CLOSE:
    		EndDialog(hwndDlg, 0);
    		return TRUE;
    	}
    	return FALSE;
    }

    Intel Core 2 Quad Q6600 @ 2.40 GHz
    3072 MB PC2-5300 DDR2
    2 x 320 GB SATA (640 GB)
    NVIDIA GeForce 8400GS 256 MB PCI-E

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 02-13-2008, 02:59 PM
  2. Dialog Box error
    By JJFMJR in forum Windows Programming
    Replies: 4
    Last Post: 09-04-2007, 07:51 AM
  3. Parent of a BrowseForFolder dialog box
    By @nthony in forum Windows Programming
    Replies: 4
    Last Post: 01-08-2007, 02:54 PM
  4. Dialog Box Problems
    By Morgul in forum Windows Programming
    Replies: 21
    Last Post: 05-31-2005, 05:48 PM
  5. How to program a "back" button with MFC
    By 99atlantic in forum Windows Programming
    Replies: 3
    Last Post: 04-26-2005, 08:34 PM