-
Menu not showing up.
I'm going through the Winprog tutorial and im on the part where you learn menus. I'm using the first method with a .rc file, but my menu doesn't show up.
Here is my main source(.cpp):
Code:
#include <windows.h>
#include "resource.h"
const char g_szClassName[] = "myWindowClass";
LRESULT CALLBACK WndProc ( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam )
{
switch(msg)
{
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;
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+0);
wc.lpszMenuName = MAKEINTRESOURCE(IDR_MYMENU);
wc.lpszClassName = g_szClassName;
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
if(!RegisterClassEx(&wc))
{
MessageBox(NULL, "Window Registration Failed!", "Error!",
MB_ICONEXCLAMATION | MB_OK);
return 0;
}
hwnd = CreateWindowEx( 0, //WS_EX_CLIENTEDGE
g_szClassName,
"Test",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, 260, 200,
NULL, NULL, hInstance, NULL );
if (hwnd == NULL)
{
MessageBox (NULL, "Window Creation Failed!", "Error!",
MB_ICONEXCLAMATION | MB_OK );
return 0;
}
ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);
while(GetMessage(&Msg, NULL, 0, 0) > 0)
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
return Msg.wParam;
}
resource.h:
Code:
#define IDR_MYMENU 101
#define IF_FILE_EXIT 9001
#define ID_STUFF_GO 9002
re.rc:
Code:
#include "resource.h"
IDR_MYMENU MENU
BEGIN
POPUP "&File"
BEGIN
MENUITEM "E&xit", ID_FILE_EXIT
END
POPUP "&Stuff"
BEGIN
MENUITEM "&Go", ID_STUFF_GO
MENUITEM "G&o somewhere else", 0, GRAYED
END
END
The .rc is included in my project. Using Dev-C++.
Is the problem with the syntax of the bold text in the main source (.cpp)?
-
Try #include <windows.h> before you #include "resource.h" in your resource script (re.rc).
-
LoadMenu(hInstance, MAKEINTRESOURCE(IDR_MENU));
or
wc.lpszMenuName = NULL;
.
.
.
LRESULT CALLBACK WndProc ( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam )
{
static HINSTANCE hInstance;
switch(msg)
{
case WM_CREATE:
hInstance = ((LPCREATESTRUCT) lParam)-> hInstance;
LoadMenu =(hInstance, MAKEINTRESOURCE(IDR_MENU));
return 0;
case WM_CLOSE:
DestroyWindow(hwnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc (hwnd, msg, wParam, lParam);
}
return
-
If you are still having trouble..
Try a GetMenu() in WM_INIT and test the returned HMENU is not NULL (if so the problem is with the menu creation).
If it is not NULL then the menu is in the dialog but probably needs to be painted with a DrawMenuBar() call.
-
In Dev-C++, go to the project menu and choose edit resource file. Include all of your resource file at the end of the one that is displayed.
-
You have a spelling error in resource.h
Code:
#define IF_FILE_EXIT 9001
should be
Code:
#define ID_FILE_EXIT 9001
Hence your re.rc won't compile into your executable and it'll never load. Once this was corrected it worked fine on my PC
--Chorus