Sign in to follow this  
mrtie_dye

Win32 Api Menu Question

Recommended Posts

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]

Share this post


Link to post
Share on other sites
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,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"



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]

Share this post


Link to post
Share on other sites
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).

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
Quote:

Try the following after every function dealing with the menu and see what it returns:

Thanks for that code. I can see it comming in handy in a lot of places. But the only function that uses IDR_MAINMENU is in WinMain.

wcx.lpszMenuName=MAKEINTRESOURCE(IDR_MAINMENU);

I tried placeing you code in several diffrent places in WinMain, including right after the call to MAKEINTRESOURCE. Not matter where I place it in WinMain, be it before or after the call to MAKEINTRESOURCE, it retuns "The system cannot find the file specified."

So I don't think this helps much. I just wish I knew which file the system can't find. I placed it in other code outside WinMain, and it returned "The operation completed sucessfully."

Quote:

Is your development environment utilizing the same porgram and arguments with respect to the filenames?

Where would I check this in MinGW Studio? But just so you know, I can get menus to work in other programs, and I can get the TerrainDlg to work the way I want it to in this one, but I can't get the two working in the same program. I know this can be done. Why can't I?

Thanks so much for all of ya'lls help. If you can think of anything else, I am all ears.

Share this post


Link to post
Share on other sites
Ahh, the joys of programming. Out of left field somewhere, the thought came to me that when I copied and pasted Programmer16's code, I pasted it into the same project, and just deleted the extra files. So, I created an entire new project,and whadda ya know? It worked. Man, I have been struggeling with this for 2 days.

Programmer16 was correct that you can only have one resource file. I figured since MinGW Studio did not complain, it must allow multiple resource files.

Thanks so much for all your help guys. I really have learned a lot more than you realize from this whole thing.

Share this post


Link to post
Share on other sites
Edit: Okay maybe so I took too long to play around with MingGW studio. Glad you've worked it out.

Sorry about taking so long, I had to download your Development Enviornment and play around with it.

Dev-C++ has a compiler output control that displays the arguments given to programs called during the compilation as well as any error messages.

Looks like this:


Try starting from scratch by copying and pasting your code into a temporary file, erasing any of the files you have been working with and re-pasting your code back into the new files. Since the code worked on others, it has to be something to do with the settings from what I can gather.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this