Dialog box code, what's wrong?

Started by
2 comments, last by GLGunblade 16 years ago
Trying to get a modeless dialog box up on the go, and while there is no problem with compile I'm not sure how to get it to display.
BOOL MainDialog_OnCommand(HWND hWnd, WORD wCommand, WORD wNotify, HWND hControl)
{
	switch (wCommand)
	{
	  case IDC_BUTTON1:
		MessageBox(hWnd, "OK pressed", "", MB_ICONEXCLAMATION);
		// deliberately falling through the switch
/*	case IDCANCEL:
		EndDialog(hWnd, wCommand);
		break;*/
	}
	return TRUE;
}


BOOL MainDialogProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	switch (uMsg)
	{
	  case WM_COMMAND:
		return MainDialog_OnCommand(hWnd, LOWORD(wParam), HIWORD(wParam), (HWND)lParam);


	  case WM_CLOSE:
	//	EndDialog(hWnd, 0);
		return TRUE;
	}
	return FALSE;
} 
The next part is where I'm stuck. Where do I put it or what do I change so that it displays? I'm using nehe's opengl framework.

int WINAPI WinMain(	HINSTANCE	hInstance,			// Instance
					HINSTANCE	hPrevInstance,		// Previous Instance
					LPSTR		lpCmdLine,			// Command Line Parameters
					int			nCmdShow)			// Window Show State
{
	MSG		msg;									// Windows Message Structure
	BOOL	done=FALSE;								// Bool Variable To Exit Loop

	DialogBoxParam(GetModuleHandle(NULL), 
		MAKEINTRESOURCE(IDD_DIALOG1), NULL,  (DLGPROC) MainDialogProc,0);
//	return (0);

	// Ask The User Which Screen Mode They Prefer
	if (MessageBox(NULL,"Would You Like To Run In Fullscreen Mode?", "Start FullScreen?",MB_YESNO|MB_ICONQUESTION)==IDNO)
	{
		fullscreen=FALSE;							// Windowed Mode
	}

	// Create Our OpenGL Window
	if (!CreateGLWindow("Emotional Intelligence Project",S_Width,S_Height,16,fullscreen))
	{
		return 0;									// Quit If Window Was Not Created
	}

	while(!done)									// Loop That Runs While done=FALSE
	{
			
		if (PeekMessage(&msg,NULL,0,0,PM_REMOVE))	// Is There A Message Waiting?
		{
			if (msg.message==WM_QUIT)				// Have We Received A Quit Message?
			{
				done=TRUE;							// If So done=TRUE
			}
			else									// If Not, Deal With Window Messages
			{
				TranslateMessage(&msg);				// Translate The Message
				DispatchMessage(&msg);				// Dispatch The Message
			}
		}
		else										// If There Are No Messages
		{
			// Draw The Scene.  Watch For ESC Key And Quit Messages From DrawGLScene()
			if (active)								// Program Active?
			{
				if (keys[VK_ESCAPE])				// Was ESC Pressed?
				{
					done=TRUE;						// ESC Signalled A Quit
				}
				else								// Not Time To Quit, Update Screen
				{
					DrawGLScene();	
					// Draw The Scene
					SwapBuffers(hDC);				// Swap Buffers (Double Buffering)
				}
			}

			if (keys[VK_F1])						// Is F1 Being Pressed?
			{
				keys[VK_F1]=FALSE;					// If So Make Key FALSE
				KillGLWindow();						// Kill Our Current Window
				fullscreen=!fullscreen;				// Toggle Fullscreen / Windowed Mode
				// Recreate Our OpenGL Window
				if (!CreateGLWindow("Emotional Intelligence Project",S_Width,S_Height,16,fullscreen))
				{
					return 0;						// Quit If Window Was Not Created
				}
			}
		}
	}

	// Shutdown
	KillGLWindow();									// Kill The Window
	return (msg.wParam);							// Exit The Program
}

Advertisement
A few things...

1. You need to show your dialog box with ShowWindow to make it appear.

2. With modeless dialogs you need to to check each message to see if it's a dialog message using IsDialogMessage before dispatching it.

3. There's no need to call DialogBoxParam if you're not storing a parameter, just call DialogBox.

4. You don't need to use GetModuleHandle to get a handle to your app instance, it's passed to WinMain as hInstance.

5. You probably got this out of some sample code (as I've seen it in lots of sample code, include from MSDN), but I need to point it out because it's very bad:

DialogBoxParam(GetModuleHandle(NULL), MAKEINTRESOURCE(IDD_DIALOG1), NULL,  (DLGPROC) MainDialogProc, 0);


What's bad is where you cast "MainDialogProc". As a general rule you should never cast function pointers, especially with c-style casts. They can cover up compile errors, which can lead to extremely-hard-to-debug stack corruptions. In fact if you were to compile your code for 64-bit windows it would cover up such a compile error, since the return value for DialogProc is actually INT_PTR and not BOOL. In 32-bit these types have the same value, but in 64-bit they don't.
Example from : theForger's Win32 API Programming Tutorial
from MSDN: Creating a Modeless Dialog Box
thanks for the help. Now to try and actually get it working.

This topic is closed to new replies.

Advertisement