-
Window - Menu problem...
Hey all,
I'm creating my first menu but it doesn't work for some vague reason.
I've tried several things, and the code compiles just like it should, but the menu doesn't show up when I run the .exe ...
Here's the window code and the resource:
Window
Code:
#include <windows.h>
#include <windowsx.h>
LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
PAINTSTRUCT ps;
switch (message)
{
case WM_CREATE:
MessageBox(NULL,"Program is now starting up...","Attention!", MB_OK | MB_ICONQUESTION);
return (0);
break;
case WM_PAINT:
BeginPaint(hwnd, &ps);
EndPaint(hwnd, &ps);
return(0);
break;
case WM_DESTROY:
PostQuitMessage (0);
break;
default:
return DefWindowProc (hwnd, message, wParam, lParam);
}
return 0;
}
char szClassName[ ] = "WINCLASS1";
int WINAPI WinMain (HINSTANCE hThisInstance,
HINSTANCE hPrevInstance,
LPSTR lpcmdline,
int ncmdshow)
{
HWND hwnd;
MSG msg;
WNDCLASSEX winclass;
winclass.hInstance = hThisInstance;
winclass.lpszClassName = szClassName;
winclass.lpfnWndProc = WindowProcedure;
winclass.style = CS_DBLCLKS | CS_OWNDC | CS_HREDRAW | CS_VREDRAW;
winclass.cbSize = sizeof (WNDCLASSEX);
winclass.hIcon = LoadIcon (NULL, IDI_APPLICATION);
winclass.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
winclass.hCursor = LoadCursor (NULL, IDC_ARROW);
winclass.lpszMenuName = "MainMenu";
winclass.cbClsExtra = 0;
winclass.cbWndExtra = 0;
winclass.hbrBackground = (HBRUSH) GetStockObject(BLACK_BRUSH);
if (!RegisterClassEx (&winclass))
return 0;
hwnd = CreateWindowEx (
0,
szClassName,
"Windows App",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
400,
400,
HWND_DESKTOP,
NULL, //I've tried to change this to "MainMenu" aswell..
hThisInstance,
NULL
);
ShowWindow (hwnd, ncmdshow);
while (GetMessage (&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
Resource
Code:
MainMenu MENU DISCARDABLE
{
POPUP "File"
{
MENUITEM "Open", MENU_FILE_ID_OPEN
MENUITEM "Close", MENU_FILE_ID_CLOSE
MENUITEM "Save", MENU_FILE_ID_SAVE
MENUITEM "Exit", MENU_FILE_ID_EXIT
}
POPUP "Help"
{
MENUITEM "About", MENU_HELP_ABOUT
}
}
I didn't forget to add the resource to the project, so that's not the problem. In fact, this is just like it's described in the book... Can anyone please tell me why the menu doesn't show up and what I'm doing wrong?
-
Presumably you have a resource header where you have defined numerical values for MENU_FILE_ID_OPEN, MENU_FILE_ID_CLOSE etc. If you have also defined a numerical value for 'MainMenu' then your menu will not be created/associated with the registered window class (see lpszMenuName of the WNDCLASSEX struct ).
If you have done this then you could either remove the numerical define for 'MainMenu' from your resource header or, arguably better, leave it alone and replace "MainMenu' with MAKEINTRESOURCE(MainMenu) for lpszMenuName of the WNDCLASSEX struct.
In short, you can use either a string or numerical value as resource item identifiers, not both.
As an aside, you don't need both a 'return' and a 'break' statement within a 'case' block of a 'switch' statement; one or the other will suffice.