Using Dialog boxes in VS2003 [SOLVED]

Started by
6 comments, last by JimPrice 19 years, 4 months ago
Hi, I'm having a lot of trouble adding a dialog to an editor program; wonder if anyone can help me. The dialogbox only consists of a close button at the moment, with ID IDC_CLOSE. I have the following code snippets:

// In the main window callback function
	case WM_COMMAND:
		hMenu = GetMenu(hWnd);

		switch(LOWORD(wParam))
		{
		case ID_INGREDIENT_ADDNEWINGREDIENT:
			DialogBox(hInstance, MAKEINTRESOURCE(IDD_ADDNEWINGREDIENT), hWnd, (DLGPROC)AddNewIngredientProc);
			break;
		}


// The dialog callback function
BOOL CALLBACK AddNewIngredientProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
	switch(message)
	{
	case WM_COMMAND:
		switch(LOWORD(wParam))
		{
		case IDC_CLOSE:
			EndDialog(hDlg, 0);
			return true;
		}
		break;
	}
	return false;
}


The program compiles and runs fine. When you click the menu item it displays the correct dialogbox. But it looks like the dialogbox never has the focus transfered (it's that slightly lighter color than usual that indicates it's not the active window) - it looks like it's the main window that still has focus. But neither receives any commands - I have to shut them down with TaskManager. I'm looking through Petzold, and can't find any reference to changing the focus. Had a look in MSDN and can't find any reference. Anyone have any clues? Thanks, Jim. [Edited by - JimPrice on December 3, 2004 12:35:52 PM]
Advertisement
HWND SetFocus(
HWND hwnd // handle of window to receive focus
);
Quote:
HWND SetFocus(
HWND hwnd // handle of window to receive focus
);


Yup, stright from MSDN. But maybe I phrased my question incorrectly - I have never seen any reference to needing to manually transfer focus when a dialog is created with DialogBox. Is this the case (and where is it documented on MSDN, cos I can't find it!), or am I doing / not doing something else wrong?

Thanks,
Jim.
Post your message pump (the GetMessage()/PeekMessage() and associated loop) and your dialog box's message handler (AddNewIngredientProc()). If I had to guess, I'd say that your dialog procedure isn't properly handling its messages. First of all, make sure the return value is BOOL (not LRESULT). If you do that, you shouldn't have to cast the function pointer to type DLGPROC. Second, NEVER call DefWndProc() inside a dialog procedure - simply return FALSE for messages you do not handle and return TRUE for messages you do handle. Dialog message procedures are very different from normal window message procedures.
Quote:
Post your message pump


Note : I'm deliberately getting, not peeking.

	while(GetMessage(&msg, NULL, 0, 0))	{		TranslateMessage(&msg);		DispatchMessage(&msg);	}


Quote:
dialog box's message handler


Identical to the original message, a la:
BOOL CALLBACK AddNewIngredientProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam){	switch(message)	{	case WM_COMMAND:		switch(LOWORD(wParam))		{		case IDC_CLOSE:			EndDialog(hDlg, 0);			return true;		}		break;	}	return false;}


Seems to do everything you thought it might not! (although thanks for the info about DLGPROC).

Have double checked all the resource ID's as well : resouce file snippet:

///////////////////////////////////////////////////////////////////////////////// Dialog//IDD_ADDNEWINGREDIENT DIALOGEX 32, 32, 293, 179STYLE DS_SETFONT | DS_MODALFRAME | DS_3DLOOK | DS_FIXEDSYS | WS_CHILD |     WS_CAPTION | WS_SYSMENUCAPTION "Add New Ingredient"FONT 8, "MS Shell Dlg", 0, 0, 0x0BEGIN    DEFPUSHBUTTON   "Close",IDC_CLOSE,218,149,50,14,BS_NOTIFY    EDITTEXT        IDC_EDIT2,43,78,112,18,ES_AUTOHSCROLLEND///////////////////////////////////////////////////////////////////////////////// DESIGNINFO//#ifdef APSTUDIO_INVOKEDGUIDELINES DESIGNINFO BEGIN    IDD_ADDNEWINGREDIENT, DIALOG    BEGIN        LEFTMARGIN, 7        RIGHTMARGIN, 286        TOPMARGIN, 7        BOTTOMMARGIN, 172    ENDEND


Any more suggestions?

Thanks for all the help,
Jim.

Ah whoops, I didn't see that you posted your dialog procedure in your first post. Anyways, I think the problem is that you're returning FALSE on the WM_INITDIALOG message:

Quote:Return Value

The dialog box procedure should return TRUE to direct the system to set the keyboard focus to the control specified by wParam. Otherwise, it should return FALSE to prevent the system from setting the default keyboard focus.

The dialog box procedure should return the value directly. The DWL_MSGRESULT value set by the SetWindowLong function is ignored.
Hah, yeah I'd spotted that myself and put it in - unfortunately no difference.

Oh well, I'll sleep on it and see what happens.

Thanks for the help, rating++.
Jim.
Hah - for anyone who's interested.

Had it set as a child window (which VS2003 appears to do as default) instead of a popup window.

Amazing what a nights sleep can do.

Jim.

This topic is closed to new replies.

Advertisement