Win32 Api Menu Question

Started by
11 comments, last by Xiachunyi 18 years, 10 months ago
I know it is a lot of code to look over, but could someone please see if they can figure out why my menu (IDR_MAINMENU) is not showing up in this window? I have tried everything I can think of, and am now convinced it is something very small I am not thinking of. Thanks in advance for any help. main.cpp

#include <windows.h>

#include "TerrainDlg.h" 
#include "MainMenu.h"

const char g_szClassName[] = "myWindowClass";

HWND g_hToolbar = NULL;
HMENU hmenuBar;

HWND hWndMain;

LRESULT CALLBACK WndProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam)
{
	hmenuBar = GetMenu(hwnd);
	switch(Message)
	{
		case WM_CREATE:
			g_hToolbar = CreateDialog(GetModuleHandle(NULL), MAKEINTRESOURCE(IDD_TERRAIN_DLG),
				hwnd, ToolDlgProc);
			if(g_hToolbar != NULL)
			{
				ShowWindow(g_hToolbar, SW_SHOW);
			}
			else
			{
				MessageBox(hwnd, "CreateDialog returned NULL", "Warning!",	
					MB_OK | MB_ICONINFORMATION);
			}
		break;
		case WM_COMMAND:
			switch(LOWORD(wParam))
			{
				case ID_NEW_MAP:
				{
					break;
				}
				
				case ID_FILE_EXIT:
					PostMessage(hwnd, WM_CLOSE, 0, 0);
					break;
				case ID_FILE_OPEN:
					//DoFileOpen(hwnd);
					break;
				case ID_FILE_SAVEAS:
					//DoFileSave(hwnd);
					break;
				case ID_TERRAIN_SHOW:
					ShowWindow(g_hToolbar, SW_SHOW);
				break;
				case ID_TERRAIN_HIDE:
					ShowWindow(g_hToolbar, SW_HIDE);
				break;
			}
		break;
		case WM_CLOSE:
			DestroyWindow(hwnd);
		break;
		case WM_DESTROY:
			DestroyWindow(g_hToolbar);
			PostQuitMessage(0);
		break;
		default:
			return DefWindowProc(hwnd, Message, wParam, lParam);
	}
	return 0;
}

int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nShowCmd)
{
	//create window class
	WNDCLASSEX wcx;

	//set the size of the structure
	wcx.cbSize=sizeof(WNDCLASSEX);

	//class stxle
	wcx.style = CS_OWNDC | CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS;

	//window procedure
	wcx.lpfnWndProc=WndProc;

	//class eytra
	wcx.cbClsExtra=0;

	//window eytra
	wcx.cbWndExtra=0;

	//application handle
	wcx.hInstance=hInstance;

	//icon
	wcx.hIcon=LoadIcon(NULL,IDI_APPLICATION);

	//cursor
	wcx.hCursor=LoadCursor(NULL,IDC_ARROW);

	//background color
	wcx.hbrBackground=(HBRUSH)GetStockObject(BLACK_BRUSH);

	//menu
	wcx.lpszMenuName=MAKEINTRESOURCE(IDR_MAINMENU);

	//class name
	wcx.lpszClassName=g_szClassName;

	//small icon
	wcx.hIconSm=NULL;

	//register the window class, return 0 if not successful
	if(!RegisterClassEx(&wcx)) return(0);

	//create main window
	hWndMain=CreateWindowEx(WS_EX_DLGMODALFRAME,g_szClassName,"Test Window", WS_BORDER | WS_SYSMENU | WS_CAPTION| WS_VISIBLE /*| WS_MAXIMIZEBOX*/ | WS_MINIMIZEBOX,0,0,1024,768,NULL,NULL,hInstance,NULL);

	//error check
	if(!hWndMain) return(0);

	//message structure
	MSG msg;

	//message pump
	for( ; ; )	
	{
		//look for a message
		if(PeekMessage(&msg,NULL,0,0,PM_REMOVE))
		{
			//there is a message

			//check that we arent quitting
			if(msg.message==WM_QUIT) break;
			
			//translate message
			TranslateMessage(&msg);

			//dispatch message
			DispatchMessage(&msg);
		}
	}
	
	//return the wparam from the WM_QUIT message
	return(msg.wParam);
}





mainmenu.rc

#include "MainMenu.h"

IDR_MAINMENU MENU
{
	POPUP "&File"
	{
			POPUP "&New"
			{
				MENUITEM "&Map", ID_NEW_MAP
			}
		MENUITEM "&Open", ID_FILE_OPEN, GRAYED
		MENUITEM "&Save", ID_FILE_SAVE, GRAYED
		MENUITEM "Save &As",ID_FILE_SAVEAS, GRAYED
		MENUITEM SEPARATOR
		MENUITEM "&Close", ID_FILE_EXIT
	}
	//POPUP "&Edit"
	//{
	//}
	POPUP "&View"
	{
		POPUP "&Toolbars"
		{
			MENUITEM "Terrain", ID_VIEW_TERRAIN
			MENUITEM "Objects", ID_VIEW_OBJECTS
		}
	}
}






mainmenu.h

#ifndef MAINMENU_H
#define MAINMENU_H

#define IDR_MAINMENU 		200
#define ID_NEW_MAP			201
#define ID_FILE_OPEN		203
#define ID_FILE_SAVE		204
#define ID_FILE_SAVEAS		205
#define ID_FILE_EXIT		206
#define ID_VIEW_TERRAIN		207
#define ID_VIEW_OBJECTS		208


#endif






terraindlg.rc

#include "TerrainDlg.h"

IDD_TERRAIN_DLG DIALOGEX 350,0,171,218
CAPTION "Terrain Toolbar"
FONT 8,"MS Sans Serif"
//STYLE 0x10CF0000
//EXSTYLE 0x00000000
{
}
//IDC_TERRAIN001_B BITMAP "groundb.bmp"
//IDC_TERRAIN001_W BITMAP "groundw.bmp"




terraindlg.h

#ifndef TERRAINDLG_H
#define TERRAINDLG_H

#include <windows.h>

BOOL CALLBACK ToolDlgProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam);

#define IDD_TERRAIN_DLG			101
#define IDC_TERRAIN001_B		102
#define IDC_TERRAIN001_W		103
#define ID_TERRAIN_SHOW			104
#define	ID_TERRAIN_HIDE			105


#endif




terraindlg.cpp

#include "TerrainDlg.h"

BOOL CALLBACK ToolDlgProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam)
{
	switch(Message)
	{
		case WM_COMMAND:
			switch(LOWORD(wParam))
			{
			}
		break;
		default:
			return FALSE;
	}
	return TRUE;
}




[Edited by - mrtie_dye on June 3, 2005 3:47:08 PM]
Marriage is the #1 cause of divorce
Advertisement
Your problem is that your edit menu is empty. Either add something to it or delete it (I added undo to it and it worked fine).

//note that I had to remove your terrain dialog because I didn't have the code
main.cpp
#include <windows.h>#include "MainMenu.h"const char g_szClassName[] = "myWindowClass";HWND g_hToolbar = NULL;HMENU hmenuBar;HWND hWndMain;LRESULT CALLBACK WndProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam){	hmenuBar = GetMenu(hwnd);	switch(Message)	{		case WM_CREATE:			g_hToolbar = CreateDialog(GetModuleHandle(NULL), MAKEINTRESOURCE(IDD_TERRAIN_DLG),				hwnd, ToolDlgProc);			if(g_hToolbar != NULL)			{				ShowWindow(g_hToolbar, SW_SHOW);			}			else			{				MessageBox(hwnd, "CreateDialog returned NULL", "Warning!",						MB_OK | MB_ICONINFORMATION);			}		break;		case WM_COMMAND:			switch(LOWORD(wParam))			{				case ID_NEW_MAP:				{					break;				}								case ID_FILE_EXIT:					PostMessage(hwnd, WM_CLOSE, 0, 0);					break;				case ID_FILE_OPEN:					//DoFileOpen(hwnd);					break;				case ID_FILE_SAVEAS:					//DoFileSave(hwnd);					break;				case ID_TERRAIN_SHOW:					ShowWindow(g_hToolbar, SW_SHOW);				break;				case ID_TERRAIN_HIDE:					ShowWindow(g_hToolbar, SW_HIDE);				break;			}		break;		case WM_CLOSE:			DestroyWindow(hwnd);		break;		case WM_DESTROY:			DestroyWindow(g_hToolbar);			PostQuitMessage(0);		break;		default:			return DefWindowProc(hwnd, Message, wParam, lParam);	}	return 0;}int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nShowCmd){	//create window class	WNDCLASSEX wcx;	//set the size of the structure	wcx.cbSize=sizeof(WNDCLASSEX);	//class stxle	wcx.style = CS_OWNDC | CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS;	//window procedure	wcx.lpfnWndProc=WndProc;	//class eytra	wcx.cbClsExtra=0;	//window eytra	wcx.cbWndExtra=0;	//application handle	wcx.hInstance=hInstance;	//icon	wcx.hIcon=LoadIcon(NULL,IDI_APPLICATION);	//cursor	wcx.hCursor=LoadCursor(NULL,IDC_ARROW);	//background color	wcx.hbrBackground=(HBRUSH)GetStockObject(BLACK_BRUSH);	//menu	wcx.lpszMenuName=MAKEINTRESOURCE(IDR_MAINMENU);	//class name	wcx.lpszClassName=g_szClassName;	//small icon	wcx.hIconSm=NULL;	//register the window class, return 0 if not successful	if(!RegisterClassEx(&wcx)) return(0);	//create main window	hWndMain=CreateWindowEx(WS_EX_DLGMODALFRAME,g_szClassName,"Test Window", WS_BORDER | WS_SYSMENU | WS_CAPTION| WS_VISIBLE /*| WS_MAXIMIZEBOX*/ | WS_MINIMIZEBOX,0,0,1024,768,NULL,NULL,hInstance,NULL);	//error check	if(!hWndMain) return(0);	//message structure	MSG msg;	//message pump	for( ; ; )		{		//look for a message		if(PeekMessage(&msg,NULL,0,0,PM_REMOVE))		{			//there is a message			//check that we arent quitting			if(msg.message==WM_QUIT) break;						//translate message			TranslateMessage(&msg);			//dispatch message			DispatchMessage(&msg);		}	}		//return the wparam from the WM_QUIT message	return(msg.wParam);}


mainmenu.rc
#include "MainMenu.h"IDR_MAINMENU MENU{	POPUP "&File"	{			POPUP "&New"			{				MENUITEM "&Map", ID_NEW_MAP			}		MENUITEM "&Open", ID_FILE_OPEN, GRAYED		MENUITEM "&Save", ID_FILE_SAVE, GRAYED		MENUITEM "Save &As",ID_FILE_SAVEAS, GRAYED		MENUITEM SEPARATOR		MENUITEM "&Close", ID_FILE_EXIT	}	//POPUP "&Edit"	//{	//}	POPUP "&View"	{		POPUP "&Toolbars"		{			MENUITEM "Terrain", ID_VIEW_TERRAIN			MENUITEM "Objects", ID_VIEW_OBJECTS		}	}}IDD_TERRAIN_DLG DIALOGEX 350,0,171,218CAPTION "Terrain Toolbar"FONT 8,"MS Sans Serif"//STYLE 0x10CF0000//EXSTYLE 0x00000000{}//IDC_TERRAIN001_B BITMAP "groundb.bmp"//IDC_TERRAIN001_W BITMAP "groundw.bmp"


mainmenu.h
#ifndef MAINMENU_H#define MAINMENU_H#include <windows.h>BOOL CALLBACK ToolDlgProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam);#define IDD_TERRAIN_DLG			101#define IDC_TERRAIN001_B		102#define IDC_TERRAIN001_W		103#define ID_TERRAIN_SHOW			104#define	ID_TERRAIN_HIDE			105#define IDR_MAINMENU 			200#define ID_NEW_MAP			201#define ID_FILE_OPEN			203#define ID_FILE_SAVE			204#define ID_FILE_SAVEAS			205#define ID_FILE_EXIT			206#define ID_VIEW_TERRAIN			207#define ID_VIEW_OBJECTS			208#define ID_EDIT_UNDO			209 #endif


TerrainDlg.cpp
#include "mainmenu.h"BOOL CALLBACK ToolDlgProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam){	switch(Message)	{		case WM_COMMAND:			switch(LOWORD(wParam))			{			}		break;		default:			return FALSE;	}	return TRUE;}


[Edited by - Programmer16 on June 3, 2005 4:11:26 PM]
I have included the code for the terrain dialog. I also tried your suggestion, and it still does not work. I even removed the edit menu all together. Still nothing.
Marriage is the #1 cause of divorce
Okay, now I'm getting a different error (MSVC++ says that you can only use 1 resource file). So, I added the two resource scripts and resource headers together (I left TerrainDlg.cpp alone) and it works fine now.

Hope that helps!

Edit:
I also changed the code in my first post (this code seems to work the way that you're looking for. The app starts and the main window is created with the menu, and the terrain dialog is showing).
I am using MinGW Studio, and it does not complain about the multiple resource files. I also copied and pasted your code, and it still does not work. UGHHH!!!
Marriage is the #1 cause of divorce
Make sure you are including your resource file in your compiling. This took me almost a half a year to figure out.
Quote:
Make sure you are including your resource file in your compiling. This took me almost a half a year to figure out.


Are you talking about the following line in main.cpp?

#include "MainMenu.h"

Or somewhere else? Not sure what you mean by "including your resource file in your compiling". Thanks for any clarification you can offer.
Marriage is the #1 cause of divorce
I have Dev-C++ and my resource file has a .rc extension on the end. It is not a header file.

I can right-click on my project icon, go to project options, go to files, and there will be my resource file in a list. Sometimes when I click on that resource file, the check box "include in compilation" is unchecked and I have to check it for that file to compile.

If your resource file is a header, then I am not sure how that would work.
Quote:
If your resource file is a header, then I am not sure how that would work.


In the resource.rc file, or in this case, the MainMenu.rc file, the first line is:

#include "MainMenu.h"

Then, in main.cpp, we also do

#include "MainMenu.h"

This usually links everything together.
Marriage is the #1 cause of divorce
Ah sorry about that, I thought you meant that your resource file, the script, was actually your header file and not your IDs for your resources.

I can't see anything wrong with the code.

Try the following after every function dealing with the menu and see what it returns:
char buffer[256];FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM,0,GetLastError(),MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT),buffer,256,0);MessageBox(hwnd,buffer,"Error",MB_OK|MB_ICONERROR);


The only other thing I can think of is that it is not compiling with the IDs. In my compile log, another program is executed to read the resource script which is: "windres.exe -i Project1_private.rc --input-format=rc -o Project1_private.res -O coff" Is your development environment utilizing the same porgram and arguments with respect to the filenames?

This topic is closed to new replies.

Advertisement