Hey everyone,
I've been working on learning the Win32 API for C++ so that I can start programming windows programs. I haven't run into any issues until last night when I tried to include a menu in my basic window through resources. I'm using Visual C++ Express 2010 which doesn't have a resource editor, so I wrote the resource file by hand. Here is the code for my basic window:
//Win32Application.cpp
#include <windows.h>
#include <windowsx.h>
#include "Menu.h"
#include "newmenu.rc"
LRESULT CALLBACK WindowProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR nCmdLine, int nCmdShow)
{
WNDCLASSEX wc;
HWND hWnd;
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hbrBackground = (HBRUSH)COLOR_WINDOW;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
wc.lpfnWndProc = WindowProc;
wc.hInstance = hInstance;
wc.lpszMenuName = MAKEINTRESOURCE(101);
wc.lpszClassName = L"WindowClass1";
if(!RegisterClassEx(&wc))
{
MessageBox(NULL, L"Class Registration Failed", L"Error", MB_ICONEXCLAMATION | MB_OK);
return 0;
}
hWnd = CreateWindowEx(NULL, L"WindowClass1", L"My First Window", WS_OVERLAPPEDWINDOW, 200, 200, 400, 400, NULL, NULL, hInstance, NULL);
if(hWnd == NULL)
{
MessageBox(NULL, L"Window Creation Failed", L"Error", MB_ICONEXCLAMATION | MB_OK);
return 0;
}
ShowWindow(hWnd, nCmdShow);
MSG msg;
while(GetMessage(&msg, NULL, 0, 0) > 0)
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
LRESULT CALLBACK WindowProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case WM_COMMAND:
switch(LOWORD(wParam))
{
case ID_FILE_EXIT:
PostMessage(hWnd, WM_CLOSE, 0, 0);
break;
case ID_HELP_ABOUT:
MessageBox(NULL, L"This program was created\nby dgr231", L"About", MB_ICONINFORMATION | MB_OK);
break;
}
case WM_CLOSE:
DestroyWindow(hWnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, msg, wParam, lParam);
}
return 0;
}
Here is my header file:
//Menu.h
#define ID_MENU 101
#define ID_FILE_EXIT 201
#define ID_HELP_ABOUT 202
And here is my resource file:
//newmenu.rc
#include "Menu.h"
ID_MENU MENU
{
POPUP "File"
{
MENUITEM "E&xit", ID_FILE_EXIT
}
POPUP "Help"
{
MENUITEM "&About", ID_HELP_ABOUT
}
}
When I compile my program I get the following errors:
win32application\newmenu.rc(3): error C2059: syntax error : 'constant'
win32application\newmenu.rc(4): error C2143: syntax error : missing ';' before '{'
win32application\newmenu.rc(4): error C2447: '{' : missing function header (old-style formal list?)
The menu should have 2 items, each with one submenu. I'm not entirely sure what I'm doing wrong, so if someone could help point me in the right direction, that would be greatly appreciated.
-D