-
Dialog Box error
Hey all, I'm creating a dialog box in my program. However every time I go to create it, it fails. Heres the code:
(Windows Vista, Dev c++ 4.9.9.2)
Resource:
Code:
#include <windows.h>
#include "Dialogs.h"
#if !defined (_WINDOWS)
#define _WINDOWS
#endif
DB_NEW DIALOG DISCARDABLE 50, 50, 700, 560
STYLE DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU
CAPTION "Create New.."
FONT 8, "MS Sans Serif"
BEGIN
DEFPUSHBUTTON "&OK",IDOK,174,18,50,14
PUSHBUTTON "&Cancel",IDCANCEL,174,35,50,14
END
Dialog Ids
Code:
#ifndef DialogTypes
#define DialogTypes
#define DB_New 99999
#endif
Section in Main that uses it
Code:
BOOL CALLBACK NewDlgProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam)
{
switch(Message)
{
case WM_INITDIALOG:
return TRUE;
case WM_COMMAND:
switch(LOWORD(wParam))
{
case IDOK:
EndDialog(hwnd, IDOK);
break;
case IDCANCEL:
EndDialog(hwnd, IDCANCEL);
break;
}
break;
default:
return FALSE;
}
return TRUE;
}
LRESULT CALLBACK MainWindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
static HDC hDC;
static HGLRC hRC;
int height, width;
// dispatch messages
switch (uMsg)
{
case WM_CREATE: // window creation
hDC = GetDC(hWnd);
SetupPixelFormat(hDC);
hRC = wglCreateContext(hDC);
wglMakeCurrent(hDC, hRC);
HMENU MainMenu;
HMENU FileMenu;
MainMenu=CreateMenu();
FileMenu=CreateMenu();
AppendMenu(FileMenu,MF_STRING,ID_New,"&New");
AppendMenu(FileMenu,MF_STRING,ID_Load,"&Load");
AppendMenu(FileMenu,MF_STRING,ID_Close,"&Close");
AppendMenu(FileMenu,MF_STRING,ID_Save, "&Save");
AppendMenu(FileMenu,MF_STRING,ID_SaveAs, "S&ave As..");
AppendMenu(FileMenu,MF_SEPARATOR,0,"");
AppendMenu(FileMenu,MF_STRING,ID_Exit,"E&xit");
InsertMenu(MainMenu,ID_File,MF_POPUP,(UINT)FileMenu,"File");
HMENU SubMenus;
SubMenus = CreateMenu();
AppendMenu(SubMenus,MF_STRING,ID_Wall,"&Wall");
AppendMenu(SubMenus,MF_STRING,ID_Floor,"&Floor");
AppendMenu(SubMenus,MF_STRING,ID_Stair,"&Stair");
AppendMenu(SubMenus,MF_STRING,ID_Light,"&Light");
InsertMenu(MainMenu,ID_Create,MF_POPUP,(UINT)SubMenus,"Create");
HMENU SubMenus2;
SubMenus2 = CreateMenu();
AppendMenu(SubMenus2,MF_STRING,ID_Undo,"&Undo");
AppendMenu(SubMenus2,MF_STRING,ID_Redo,"&Redo");
AppendMenu(SubMenus2,MF_SEPARATOR,0,"");
AppendMenu(SubMenus2,MF_STRING,ID_Properties,"&Properties");
InsertMenu(MainMenu,ID_Edit,MF_POPUP,(UINT)SubMenus2,"Edit");
HMENU SubMenus3;
SubMenus3 = CreateMenu();
AppendMenu(SubMenus3,MF_STRING,ID_Wireframe,"&Wireframe");
AppendMenu(SubMenus3,MF_STRING | MF_CHECKED,ID_Textured,"&Textured");
AppendMenu(SubMenus3,MF_STRING,ID_Regular,"&Regular");
AppendMenu(SubMenus3,MF_SEPARATOR,0,"");
AppendMenu(SubMenus3,MF_STRING,ID_ResetView,"R&eset View");
AppendMenu(SubMenus3,MF_SEPARATOR,0,"");
AppendMenu(SubMenus3,MF_STRING,ID_TopView,"&Top View");
AppendMenu(SubMenus3,MF_STRING,ID_XView,"&X View");
AppendMenu(SubMenus3,MF_STRING,ID_ZView,"&Z View");
InsertMenu(MainMenu,ID_View,MF_POPUP,(UINT)SubMenus3,"View");
HMENU SubMenus4;
SubMenus4 = CreateMenu();
AppendMenu(SubMenus4,MF_STRING,ID_Overview,"&Overview");
InsertMenu(MainMenu,ID_Help,MF_POPUP,(UINT)SubMenus4,"Help");
if (!SetMenu(hWnd,MainMenu)) { return FALSE; }
break;
case WM_DESTROY: // window destroy
case WM_QUIT:
case WM_CLOSE: // windows is closing
// deselect rendering context and delete it
wglMakeCurrent(hDC, NULL);
wglDeleteContext(hRC);
// send WM_QUIT to message queue
PostQuitMessage(0);
break;
case WM_SIZE:
windowHeight = HIWORD(lParam); // retrieve width and height
windowWidth = LOWORD(lParam);
glViewport(0, 0, windowWidth, windowHeight); // reset the viewport to new dimensions
glMatrixMode(GL_PROJECTION); // set projection matrix current matrix
glLoadIdentity(); // reset projection matrix
// calculate aspect ratio of window
gluPerspective(52.0f,(GLfloat)windowWidth/(GLfloat)windowHeight,0.0001f,1000.0f);
break;
case WM_ACTIVATEAPP: // activate app
break;
case WM_PAINT: // paint
PAINTSTRUCT ps;
BeginPaint(hWnd, &ps);
EndPaint(hWnd, &ps);
break;
case WM_LBUTTONDOWN: // left mouse button
break;
case WM_RBUTTONDOWN: // right mouse button
break;
case WM_MOUSEMOVE: // mouse movement
break;
case WM_LBUTTONUP: // left button release
break;
case WM_RBUTTONUP: // right button release
break;
case WM_KEYUP:
break;
case WM_KEYDOWN:
break;
case WM_COMMAND:
switch(LOWORD(wParam))
{
case ID_Exit:
PostQuitMessage(0);
exiting = true;
break;
case ID_New:
{
if(!pCurrentLevel)
{
int ret = DialogBox(GetModuleHandle(NULL),
MAKEINTRESOURCE(DB_New), hWnd, NewDlgProc);
if(ret == IDOK){
MessageBox(hWnd, "Dialog exited with IDOK.", "Notice",
MB_OK | MB_ICONINFORMATION);
}
else if(ret == IDCANCEL){
MessageBox(hWnd, "Dialog exited with IDCANCEL.", "Notice",
MB_OK | MB_ICONINFORMATION);
}
else if(ret == -1){
MessageBox(hWnd, "Dialog failed!", "Error",
MB_OK | MB_ICONINFORMATION);
}
pCurrentLevel = new Level();
pCurrentLevel->theme = Hedge_Maze;
pCurrentLevel->changed = true;
pCurrentLevel->width = 35;
pCurrentLevel->depth = 35;
pCurrentLevel->floors = 1;
if(!pCurrentLevel->Setup())
MessageBox(hWnd, "Error setting up level.", "Error", MB_OK);
break;
}
else
MessageBox(hWnd, "You already have a level open", "New Level..", MB_OK);
break;
}
case ID_Floor:
{
if(pCurrentLevel)
++pCurrentLevel->floors;
else
MessageBox(hWnd, "You don't have a level open.", "New Floor..", MB_OK);
break;
}
}
default:
break;
}
return DefWindowProc(hWnd, uMsg, wParam, lParam);
}
Can anyone tell me why it always fails?
-
Can you Define "fails"? You post a fairly large program, but without any details of what works and what fails, it means that the reader has to figure out which parts of your code works and which doesn't - which is, if you did't realize it, quite hard work.
--
Mats
-
Yes, good point lol sorry. Heres the problem:
Code:
switch(LOWORD(wParam))
{
case ID_Exit:
PostQuitMessage(0);
exiting = true;
break;
case ID_New:
{
if(!pCurrentLevel)
{
int ret = DialogBox(GetModuleHandle(NULL),
MAKEINTRESOURCE(DB_New), hWnd, NewDlgProc);
if(ret == IDOK){
MessageBox(hWnd, "Dialog exited with IDOK.", "Notice",
MB_OK | MB_ICONINFORMATION);
}
else if(ret == IDCANCEL){
MessageBox(hWnd, "Dialog exited with IDCANCEL.", "Notice",
MB_OK | MB_ICONINFORMATION);
}
else if(ret == -1){
MessageBox(hWnd, "Dialog failed!", "Error",
MB_OK | MB_ICONINFORMATION);
}
pCurrentLevel = new Level();
pCurrentLevel->theme = Hedge_Maze;
pCurrentLevel->changed = true;
pCurrentLevel->width = 35;
pCurrentLevel->depth = 35;
pCurrentLevel->floors = 1;
if(!pCurrentLevel->Setup())
MessageBox(hWnd, "Error setting up level.", "Error", MB_OK);
break;
}
My dialog never displays when I hit the new menu, it always returns -1.
The parts in bold are the error prone parts most likely.
-
Try calling GetLastError() to see what the actual error is.
--
Mats
-
I think because you didn't add the WS_VISIBLE in your dialog editor.