C Board  

Go Back   C Board > Platform Specific Boards > Windows Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 08-23-2006, 12:41 AM   #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
Bleech is offline   Reply With Quote
Old 08-23-2006, 12:55 AM   #2
Cat
Registered User
 
Join Date: May 2003
Posts: 1,199
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.
Cat is offline   Reply With Quote
Old 08-23-2006, 01:12 AM   #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
Bleech is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
[MSVC] Is it valid to link against an import .lib generated from an executable? pheres C++ Programming 9 02-13-2008 02:59 PM
Dialog Box error JJFMJR Windows Programming 4 09-04-2007 07:51 AM
Parent of a BrowseForFolder dialog box @nthony Windows Programming 4 01-08-2007 02:54 PM
Dialog Box Problems Morgul Windows Programming 21 05-31-2005 05:48 PM
How to program a "back" button with MFC 99atlantic Windows Programming 3 04-26-2005 08:34 PM


All times are GMT -6. The time now is 09:54 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

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