Advertisement

Win32 Menus?

Started by March 15, 2003 03:43 PM
7 comments, last by Toris 21 years, 8 months ago
Hi, I''m having trouble getting the menu to show on a window. I''ve created the resource in VC.NET by, in the project, right clicking on Resource Files, then Add > Add Resource. Then I right click on the new resource file in the resource view, and click add, select menu, and make it. Then I save all the files. I think this is the correct way of doing it, but it doesn''t seem to work, I have included the code I have written below. Any help on this is very much appreciated! Cheers, -Toris
  
// Include the standard windows header

#include <windows.h>

// Define our MAIN_WINDOWs class name

#define MAIN_WINDOW "WINCLASS1"

// Declare a boolean bQuit, that says whether the application should continue or not

bool bQuit = false;

// The main windows event handler

LRESULT CALLBACK WinProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam)
{
	switch(Msg)
	{
	case (WM_DESTROY):
		// If the main window is closed, set the quit boolean to true

		bQuit = true;
		break;
	default:
		break;
	}

	// If we''re not handling the message, let windows deal with it

	return DefWindowProc( hWnd, Msg, wParam, lParam );
}

// Main windows function

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCommandLine, int nCmdShow)
{
	// Define our message holder

	MSG Msg;

	// Define the main windows class

	WNDCLASSEX wcWinClass;
	wcWinClass.cbClsExtra = 0;
	wcWinClass.cbSize = sizeof(WNDCLASSEX);
	wcWinClass.cbWndExtra = 0;
	wcWinClass.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
	wcWinClass.hCursor = LoadCursor(NULL, IDC_ARROW);
	wcWinClass.hIcon = LoadIcon(NULL, IDI_WINLOGO);
	wcWinClass.hIconSm = LoadIcon(NULL, IDI_WINLOGO);
	wcWinClass.hInstance = hInstance;
	wcWinClass.lpfnWndProc = WinProc;
	wcWinClass.lpszClassName = MAIN_WINDOW;
	wcWinClass.lpszMenuName = NULL;
	wcWinClass.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC | CS_DBLCLKS;

	// Register the main windows class

	RegisterClassEx(&wcWinClass);

	// Create our main window

	HWND hMainWin = CreateWindowEx(NULL, MAIN_WINDOW, "Menu test", WS_OVERLAPPEDWINDOW | WS_VISIBLE, 0, 0, 400, 400, NULL, NULL, hInstance, NULL);

	// Load the menu resource for our main window

	HMENU hMainWinMenu = LoadMenu(hInstance, "IDR_MENU1");

	// Apply the previously loaded menu resource to our main window

	SetMenu(hMainWin, hMainWinMenu);

	// As long as bQuit is false, loop through the main logic

	while(bQuit == false)
	{
		PeekMessage(&Msg, NULL, 0, 0, PM_REMOVE);
		TranslateMessage(&Msg);
		DispatchMessage(&Msg);
	}

	// Exit, returning what we''re meant to return

	return(Msg.wParam);

}
  
throw in some error checking, for starters.
Advertisement
to put the menu in, u have to give a value to lpszMenuName, more specifically:

MAKEINTRESOURCE( /* name you gave to your menu */)
_________________________________________"You're just jelous because the voices only talk to me"
I put some error checking in as you stated niyaw, and I have worked out that it bails out on the following line:


      // Load the menu resource for our main windowHMENU hMainWinMenu;if(!(hMainWinMenu = LoadMenu(hInstance, "IDR_MENU1")))	return(0);  



Now, the new code is:


  // Include the standard windows header#include <windows.h>// Define our MAIN_WINDOWs class name#define MAIN_WINDOW "WINCLASS1"// Declare a boolean bQuit, that says whether the application should continue or notbool bQuit = false;// The main windows event handlerLRESULT CALLBACK WinProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam){	switch(Msg)	{	case (WM_DESTROY):		// If the main window is closed, set the quit boolean to true		bQuit = true;		break;	default:		break;	}	// If we're not handling the message, let windows deal with it	return DefWindowProc( hWnd, Msg, wParam, lParam );}// Main windows functionint WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCommandLine, int nCmdShow){	// Define our message holder	MSG Msg;	// Define the main windows class	WNDCLASSEX wcWinClass;	wcWinClass.cbClsExtra = 0;	wcWinClass.cbSize = sizeof(WNDCLASSEX);	wcWinClass.cbWndExtra = 0;	wcWinClass.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);	wcWinClass.hCursor = LoadCursor(NULL, IDC_ARROW);	wcWinClass.hIcon = LoadIcon(NULL, IDI_WINLOGO);	wcWinClass.hIconSm = LoadIcon(NULL, IDI_WINLOGO);	wcWinClass.hInstance = hInstance;	wcWinClass.lpfnWndProc = WinProc;	wcWinClass.lpszClassName = MAIN_WINDOW;	wcWinClass.lpszMenuName = NULL;	wcWinClass.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC | CS_DBLCLKS;	// Register the main windows class	RegisterClassEx(&wcWinClass);	// Create our main window	HWND hMainWin;	if(!(hMainWin = CreateWindowEx(NULL, MAIN_WINDOW, "Menu test", WS_OVERLAPPEDWINDOW | WS_VISIBLE, 0, 0, 400, 400, NULL, NULL, hInstance, NULL)))		return(0);			// Load the menu resource for our main window	HMENU hMainWinMenu;	if(!(hMainWinMenu = LoadMenu(hInstance, "IDR_MENU1")))		return(0);			// Apply the previously loaded menu resource to our main window	if(!(SetMenu(hMainWin, hMainWinMenu)))		return(0);			// As long as bQuit is false, loop through the main logic	while(bQuit == false)	{		PeekMessage(&Msg, NULL, 0, 0, PM_REMOVE);		TranslateMessage(&Msg);		DispatchMessage(&Msg);	}	// Exit, returning what we're meant to return	return(Msg.wParam);}  


I have also tried the method you mention Infuscare, using:


  wcWinClass.lpszMenuName = MAKEINTRESOURCE("IDR_MENU1");and also:wcWinClass.lpszMenuName = MAKEINTRESOURCE(IDR_MENU1);      


The one without brackets won't compile, as it complains about an "undeclared identifier".
I also tried it with the SetMenu etc... commented, and uncommented, both to no avail.

So again, I am stuck, I believe now it's some problem with my resources, I can't be sure though, if you believe it may be, I'll upload it to tripod for you to see...

Thanks again!
-Toris

[edited by - Toris on March 15, 2003 5:11:41 PM]

[edited by - Toris on March 15, 2003 5:12:33 PM]
how about

if(!(hMainWinMenu = LoadMenu(hInstance, MAKEINTRESOURCE(IDR_MENU1))))

?

also, you need to include resource.h, where your resource identifiers live.

[edited by - niyaw on March 15, 2003 5:20:23 PM]
Ok, I just tried:

if(!(hMainWinMenu = LoadMenu(hInstance, MAKEINTRESOURCE(IDR_MENU1))))

and I got undeclared identifier, so I then tried:

if(!(hMainWinMenu = LoadMenu(hInstance, MAKEINTRESOURCE("IDR_MENU1"))))

and it just bailed out like it usually does.
I then thought, well, maybe if I casted it to the correct type it might work:

if(!(hMainWinMenu = LoadMenu(hInstance, (LPCTSTR) MAKEINTRESOURCE("IDR_MENU1"))))

Again, no luck, I then thought, well, what's the value of IDR_MENU1, checked, it was 101, so I tried casting that:

if(!(hMainWinMenu = LoadMenu(hInstance, (LPCTSTR) 101)))

Funnily enough, that worked, so what would make that work, and not any of the previous attempts?


[edited by - Toris on March 15, 2003 5:25:20 PM]
Advertisement
Declare it in your class before you create the window.

i.e.

wcWinClass.lpszMenuName = *menu name here*;
*st0ned*
you didn''t include resource.h.

MAKEINTRESOURCE(x) is basically the same as (LPCTSTR) x, and IDR_MENU1 was defined to 101. thus,

MAKEINTRESOURCE(IDR_MENU1)

resolved to

(LPCTSTR) 101
Ah yes, working perfectly now!
Thanks hugely for all your replies!
Can''t believe after all that, the answer was as simple as forgetting to include a file

Cheers,
-Toris

This topic is closed to new replies.

Advertisement