My Dialog wont display

Hi ~

I've being trying to implement an essential dialog into my program and it won't work. MENUSTYLE is the name of my dialog. Here is my main.cpp file:

/*{ 2007 Elixir Software. Menu code Milonic.com*/

#include <windows.h>
#include "defines.h"

const char g_szClassName[] = "myWindowClass";
static HINSTANCE g_hInst = NULL;

/*Initialise Variables*/
HWND hEdit = NULL;
HFONT hfDefault = NULL;

int ret = NULL; //Variable to store value returned by MENUSTYLE dialog

//MENUSTYLE Procedure
BOOL CALLBACK MStyleProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam)
{
switch(Message)
{
case WM_INITDIALOG:

return TRUE;
case WM_COMMAND:
switch(LOWORD(wParam))
{}
break;
}
return FALSE;
}

//The Window Procedure
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case WM_CREATE:
{
/*Create Edit Box*/
hEdit = CreateWindowEx(WS_EX_CLIENTEDGE, "EDIT", "",
WS_CHILD | WS_VISIBLE | WS_VSCROLL | WS_HSCROLL | ES_MULTILINE | ES_AUTOVSCROLL | ES_AUTOHSCROLL,
-2, -2, 442, 352, hwnd, (HMENU)EDIT_CODE, GetModuleHandle(NULL), NULL);
if(hEdit == NULL)
MessageBox(hwnd, "Could not create code view edit box.", "Error", MB_OK | MB_ICONERROR);

SendMessage(hEdit,WM_SETFONT,(WPARAM)GetStockObject(DEFAULT_GUI_FONT),MAKELPARAM(1,0));

/*Set focus*/
SetFocus(hEdit);
break;
}
case WM_COMMAND:
{
switch (LOWORD(wParam))

case DM_FILE_NEW:
{
switch (ret = DialogBox(g_hInst, "MENUSTYLE", hwnd, MStyleProc))
;
//MENUSTYLE declared in style.rc
case -1:
{
MessageBox(GetActiveWindow(),
"Error: Failed to display MENUSTYLE dialog box.\n\nPossible Solution:\n\nRe-attempt to display the dialog. If that doesn\'t\nwork, re-boot your computer.",
"Error", MB_OK | MB_ICONSTOP);
break;
}
break;
}
break;
}
// Break WM_COMMAND

case WM_CLOSE:
DestroyWindow(hwnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
}
return 0;
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
WNDCLASSEX wc;
HWND hwnd;
MSG Msg;

//Step 1: Registering the Window Class
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = 0;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wc.lpszMenuName = MAKEINTRESOURCE(MYMENU);//Defined in menu.rc
wc.lpszClassName = g_szClassName;
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);

if(!RegisterClassEx(&wc))
{
MessageBox(NULL, "Error: Window Registration Failed!", "Error!",
MB_ICONEXCLAMATION | MB_OK);
return 0;
}

// Step 2: Creating the Window
hwnd = CreateWindowEx(
WS_EX_CLIENTEDGE,
g_szClassName,
"Elixir DHTML Javascript Menu",
WS_OVERLAPPEDWINDOW,
300, 200, 450, 405,
NULL, NULL, hInstance, NULL);

if(hwnd == NULL)
{
MessageBox(NULL, "Error: Window Creation Failed!", "Error!",
MB_ICONEXCLAMATION | MB_OK);
return 0;
}

ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);

// Step 3: The Message Loop
while(GetMessage(&Msg, NULL, 0, 0) > 0)
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
return Msg.wParam;
}

If it helps, here is my dialog source (so far):

#include <windows.h>

//About Dialog

MENUSTYLE DIALOG 20, 25, 180, 130
/*Left side, distance from left of parent
Top, distance from top of parent
Right, distance from left of dialog / child (width)
Bottom, distance from top of dialog (height)*/

STYLE DS_MODALFRAME | DS_3DLOOK | WS_POPUP | WS_VISIBLE | WS_CAPTION | WS_SYSMENU
CAPTION "Customise Menu"
FONT 8, "MS Sans Serif"

BEGIN
CONTROL "About This Program", -1, "button", BS_GROUPBOX | WS_CHILD | WS_VISIBLE | WS_GROUP, 10, 5, 160, 110
END

Please help!

~ Chris Howarth
[5424 byte] By [chrishowarth] at [2007-11-20 8:39:06]
# 1 Re: My Dialog wont display
Did you mean to do this? (if so I dont know what the purpose would be):

switch (ret = DialogBox(g_hInst, "MENUSTYLE", hwnd, MStyleProc))
;

I'd suggest avoiding shortcuts like that until you have things figured out.
Martin O at 2007-11-9 13:30:31 >
# 2 Re: My Dialog wont display
What do you mean by shortcut?
chrishowarth at 2007-11-9 13:31:31 >
# 3 Re: My Dialog wont display
What do you mean by shortcut?
Let me guess! He wants to say "tricky cool code". ;)

Well, as is stated in DialogBox documentation, in case of failure you can check its returned value:

0 means the parent handle you have passed is not a valid window handle.
if it's -1 then call GetLastError to see what's going wrong.

However, what I do suspect as a possible cause, is that "MENUSTYLE" is not a resource name but a resource ID (integer value) so you have to use MAKEINTRESOURCE macro as follows:

int nRet = DialogBox(g_hInst, MAKEINTRESOURCE(MENUSTYLE), hwnd, MStyleProc);
ovidiucucu at 2007-11-9 13:32:40 >
# 4 Re: My Dialog wont display
What do you mean by shortcut?

Ovidiucucu guessed correctly: 'tricky cool code'.

I meant doing this:

switch (ret = DialogBox(g_hInst, "MENUSTYLE", hwnd, MStyleProc))

Instead of this:

ret = DialogBox(g_hInst, "MENUSTYLE", hwnd, MStyleProc);
switch (ret)

Granted, both should work, but why not make things as simple as possible (when you're struggling with basics of win32)? Maybe then wierd things like 'switch(someValue);' will be more obvious.

BUT...I think ovidiucucu probably guessed the problem.

Here's a tip: Using Visual Studio, create a Win32 Gui App. It will create a simple app for you with an example of exactly what you're trying to do: create a dialog. I'd suggest studying that.
Martin O at 2007-11-9 13:33:33 >
# 5 Re: My Dialog wont display
The error returned seems to be any value. If I put

case -1: {
MessageBox(hwnd,"Error was -1","Another bloody error",MB_OK | MB_ICONSTOP);
break;
}
case 0: {
MessageBox(hwnd,"Error was 0","Another bloody error",MB_OK | MB_ICONSTOP);
break;
}

Then the result is that -1 was the problem. If I re-arrange them so that case 0: is at the top, then it seems that 0 is the value returned! This all worked fine for my last program.
chrishowarth at 2007-11-9 13:34:42 >
# 6 Re: My Dialog wont display
Since all cases report "Another bloody error", then no matter the returned value. :) ;)

However, to enjoy of '0' case, take a look at this FAQ ( http://www.dev-archive.com/forum/showthread.php?t=318721).
ovidiucucu at 2007-11-9 13:35:39 >
# 7 Re: My Dialog wont display
You never acknowledged the switch(someValue); problem. Did you fix that? Did you understand my post pointing that out? You've provided a snippet of a case statement--first prove you know how to use switch-case statements by providing all the code. I need to be convinced that you didnt make the same mistake I pointed out previously before I'll bother taking guesses at the 1000 other things you might have done wrong. BTW: all the code doesnt mean everything in the program you're currently working on. It means providing a 'Short Self Contained Compilable Sample' that demonstrates your problem. Often times, the very act of creating a 'SSCCS' will clarify your problem & you might even figure it out yourself.
Martin O at 2007-11-9 13:36:43 >
# 8 Re: My Dialog wont display
OK, here is a the self-contained WM_COMMAND:

case WM_COMMAND:
{
switch (LOWORD(wParam))

case DM_FILE_NEW:
{
switch (ret = DialogBox(g_hInst, "MENUSTYLE", hwnd, MStyleProc))
;
//MENUSTYLE declared in style.rc
case -1:
{
MessageBox(GetActiveWindow(),
"Error: -1 Failed to display MENUSTYLE dialog box.\n\nPossible Solution:\n\nRe-attempt to display the dialog. If that doesn\'t\nwork, re-boot your computer.",
"Error", MB_OK | MB_ICONSTOP);

break;
}
break;
}
break;
}

Here is the dialog resource:
#include <windows.h>

//About Dialog

MENUSTYLE DIALOG 20, 25, 180, 130
/*Left side, distance from left of parent
Top, distance from top of parent
Right, distance from left of dialog / child (width)
Bottom, distance from top of dialog (height)*/

STYLE DS_MODALFRAME | DS_3DLOOK | WS_POPUP | WS_VISIBLE | WS_CAPTION | WS_SYSMENU
CAPTION "Customise Menu"
FONT 8, "MS Sans Serif"

BEGIN
CONTROL "About This Program", -1, "button", BS_GROUPBOX | WS_CHILD | WS_VISIBLE | WS_GROUP, 10, 5, 160, 110
END

The dialog resource is added to my project; I have not done anything else.
chrishowarth at 2007-11-9 13:37:45 >
# 9 Re: My Dialog wont display
If you loolk deeper in the .rc file you'll find something like

"MENUSTYLE", DIALOG
BEGIN
...
END
or
MENUSTYLE, DIALOG
BEGIN
...
END

First case "MENUSTYLE", between quotes is the dialog resource name, in the second one MENUSTYLE, with no quotes is the dialog resource ID.
In first case you can pass to DialogBox the resource name as it is and you have done, in the second case you have to use MAKEINTRESOURCE macro like I showed in my first post.

Just curious,... did you even read my first post?
ovidiucucu at 2007-11-9 13:38:42 >
# 10 Re: My Dialog wont display
Yes, I did read your first post.

If I use quote marks is my dialog "MENUSTYLE", DIALOG
BEGIN
...
END
I get an error (my compiler doesn't give the name of resource errors).

If I use commas in my resource I get errors. If I define an abitruary ID for my dialog and use MAKEINTRESOURCE it still doesn't show.

Am i still doing something wrong? Sorry...
chrishowarth at 2007-11-9 13:39:41 >
# 11 Re: My Dialog wont display
Well, then zip and attach the project here.
ovidiucucu at 2007-11-9 13:40:39 >
# 12 Re: My Dialog wont display
Attached.
chrishowarth at 2007-11-9 13:41:48 >
# 13 Re: My Dialog wont display
switch (ret = DialogBox(g_hInst, "MENUSTYLE", hwnd, MStyleProc))
;
//MENUSTYLE declared in style.rc
case -1:
{
MessageBox(GetActiveWindow(),
"Error: -1 Failed to display MENUSTYLE dialog box.\n\nPossible Solution:\n\nRe-attempt to display the dialog. If that doesn\'t\nwork, re-boot your computer.",
"Error", MB_OK | MB_ICONSTOP);

break;
}
break;
What's with the extra ';' after your 'switch' statement? Martin asked about this earlier, and I didn't see a response.

Viggy
MrViggy at 2007-11-9 13:42:43 >
# 14 Re: My Dialog wont display
I said (please) "zip and attach the project" and not "selected source file".

However, beside already said about that switch, you have another "big" problem: 2 .rc files in the same project (menu.rc and style.rc). At least Visual Studio does not alow that. Most possible the only one which compiles is menu.rc and, dor that reason DialogBox, which uses a dialog resource template from style.rc, fails.
ovidiucucu at 2007-11-9 13:43:48 >
# 15 Re: My Dialog wont display
I did attach the source files. It is just called 'Source.zip'. I will look into those points.
chrishowarth at 2007-11-9 13:44:50 >
# 16 Re: My Dialog wont display
I did attach the source files. It is just called 'Source.zip'.
I have WinZip installed and that missing 's' was just a typo.

Giving, posting, attaching as much as possible exact info can help us finding solutions faster.
As for example, because you did not attach the whole project, I had to make one myself, then add your files to it, then... then I had to guess the possible cause (two .rc files in the same project).
ovidiucucu at 2007-11-9 13:45:52 >
# 17 Re: My Dialog wont display
So would you really want the Code::Blocks (.cbp) project file? I doubt you use Code::Blocks, so how would it help?

However, thanks to Martin O's and your help I have finally sorted out the problem.

Thankyou!
chrishowarth at 2007-11-9 13:46:48 >