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

I'm pretty sure you aren't supposed to compile a resource file with a C++ compiler. Look for a resource compiler instead.

Remove the #include "newmenu.rc" from the source file. It is only to be included via the Solution/Project that you have - i.e. that file should display in the Solution Explorer. To add the file to the solution, use Project / Add Existing Item and pick the .rc file - that should suffice.

PS. You might search the web for free resource editors.

Thank you both for the advice.

mitrmkar - I will give that a shot later tonight and if it works then I'll mark the thread as solved.

Do either of you have any recommendations as to a good free resource compiler?

You're using Visual C++ 2010.. u don't need a resource compiler. Do as mitmkar has said and it will work out that way.

EDIT: The program now works. Thanks for your help. I have marked this thread as solved.

Do either of you have any recommendations as to a good free resource compiler?

Just in case you misread/misunderstood my suggestion, if you already haven't, then search the web for free resource editors. In other words, get an editor with which to create the GUI, instead of writing the .rc files manually.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.