PDA

Click to See Complete Forum and Search --> : Menus again...


CyberCarsten
Mar 2nd, 2001, 06:34 AM
This code comes from a C++ book I'm currently reading, and it should show a menu....but it doesn't
The .EXE file that is on the cd accompanied with the book uses this code, and it works fine...what is wrong???


// PROG6_2.CPP - basic menus

// INCLUDES ///////////////////////////////////////////////
#define WIN32_LEAN_AND_MEAN

#include <windows.h>
#include <windowsx.h>
#include <winuser.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "mymenu.h"

// DEFINES ////////////////////////////////////////////////

// defines for windows
#define WINDOW_CLASS_NAME "WINCLASS1"
#define WINDOW_WIDTH 320
#define WINDOW_HEIGHT 200

// GLOBALS ////////////////////////////////////////////////
HWND main_window_handle = NULL; // save the window handle
char buffer[80]; // used to print text

// FUNCTIONS //////////////////////////////////////////////
LRESULT CALLBACK WindowProc(HWND hwnd,
UINT msg,
WPARAM wparam,
LPARAM lparam)
{
// this is the main message handler of the system
PAINTSTRUCT ps; // used in WM_PAINT
HDC hdc; // handle to a device context

// what is the message
switch(msg)
{
case WM_CREATE:
{
// do initialization stuff here
return(0);
} break;

// process menu messages
case WM_COMMAND:
{
// get the dc
hdc = GetDC(hwnd);

// set the color
SetTextColor(hdc,RGB(0,0,255));
SetBkColor(hdc,RGB(0,0,0));
SetBkMode(hdc,OPAQUE);

// what menu item?
switch(wparam)
{
case ID_MYMENU_FILE_OPEN:
{
// print message
TextOut(hdc,0,100,"File->Open ",strlen("File->Open "));
} break;
case ID_MYMENU_FILE_CLOSE:
{
// print message
TextOut(hdc,0,100,"File->Close ",strlen("File->Close "));
} break;
case ID_MYMENU_FILE_EXIT:
{
// terminate the application
PostMessage(hwnd,WM_DESTROY,0,0);
} break;
case ID_MYMENU_HELP_ABOUT:
{
// print message
TextOut(hdc,0,100,"Help->About ",strlen("Help->About "));

// put up a message box
MessageBox(hwnd,"Poor Man's About","About Menus",MB_OK);

} break;

default: break;
} // end switch

// release the dc
ReleaseDC(hwnd,hdc);

// inform windows we handled message
return(0);
} break;

case WM_PAINT:
{
// start painting
hdc = BeginPaint(hwnd,&ps);

// end painting
EndPaint(hwnd,&ps);
return(0);
} break;

case WM_DESTROY:
{
// kill the application
PostQuitMessage(0);
return(0);
} break;

default:break;

} // end switch

// process any messages that we didn't take care of
return (DefWindowProc(hwnd, msg, wparam, lparam));

} // end WinProc

// WINMAIN ////////////////////////////////////////////////
int WINAPI WinMain( HINSTANCE hinstance,
HINSTANCE hprevinstance,
LPSTR lpcmdline,
int ncmdshow)
{

WNDCLASS winclass; // this will hold the class we create
HWND hwnd; // generic window handle
MSG msg; // generic message
HDC hdc; // generic dc
PAINTSTRUCT ps; // generic paintstruct

// first fill in the window class stucture
winclass.style = CS_DBLCLKS | CS_OWNDC |
CS_HREDRAW | CS_VREDRAW;
winclass.lpfnWndProc = WindowProc;
winclass.cbClsExtra = 0;
winclass.cbWndExtra = 0;
winclass.hInstance = hinstance;
winclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
winclass.hCursor = LoadCursor(NULL, IDC_ARROW);
winclass.hbrBackground = GetStockObject(BLACK_BRUSH);
winclass.lpszMenuName = NULL;
winclass.lpszClassName = WINDOW_CLASS_NAME;

// register the window class
if (!RegisterClass(&winclass))
return(0);

// create the window
if (!(hwnd = CreateWindow(WINDOW_CLASS_NAME, // class
"Basic Menus", // title
WS_OVERLAPPEDWINDOW | WS_VISIBLE,
0,0, // x,y
WINDOW_WIDTH, // width
WINDOW_HEIGHT, // height
NULL, // handle to parent
NULL, // handle to menu
hinstance,// instance
NULL))) // creation parms
return(0);

// save the window handle in a global
main_window_handle = hwnd;

// load the menu in and attach it (method 3)
SetMenu(hwnd,LoadMenu(hinstance, "MYMENU"));

// enter main event loop
while(1)
{
if (PeekMessage(&msg,NULL,0,0,PM_REMOVE))
{
// test if this is a quit
if (msg.message == WM_QUIT)
break;

// translate any accelerator keys
TranslateMessage(&msg);

// send the message to the window proc
DispatchMessage(&msg);
} // end if

// main game processing goes here

} // end while

// return to Windows like this
return(msg.wParam);

} // end WinMain

///////////////////////////////////////////////////////////

Technocrat
Mar 2nd, 2001, 09:58 AM
Did you just copy this code into a cpp file, along with mymenu.h? If you did you will need to have a resource, ".rc", file that goes with the mymenu.h

CyberCarsten
Mar 2nd, 2001, 10:13 AM
I have the .h and .rc file, but the .rc file isn't mentioned in the .h file....
Do you know whats wrong???

Technocrat
Mar 2nd, 2001, 10:18 AM
It's actually the other way around .h file is mentioned in the .rc file.

Can you zip up your whole project and post it, I will be happy to take a look at the whole thing.

CyberCarsten
Mar 2nd, 2001, 10:26 AM
Here it is! :)

Technocrat
Mar 2nd, 2001, 10:37 AM
This is your entire project?

Technocrat
Mar 2nd, 2001, 11:06 AM
Ok after stratching my head for a few minutes I got it. You must have have made your project as a console app and not a Win32 app. Create a new empty, win32 app and import your cpp, rc, and h files. Then you have a problem with your background brush. I just made it NULL to test it, but it works, and your menu shows.

Technocrat
Mar 2nd, 2001, 11:08 AM
Just change to this:


winclass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);

CyberCarsten
Mar 2nd, 2001, 11:23 AM
It still doesn't work....
How should I add the .rc file and the .h files???


#include "mymenu.h"


Or...

Technocrat
Mar 2nd, 2001, 11:24 AM
Which compiler are you using?

CyberCarsten
Mar 2nd, 2001, 11:26 AM
Borland C++ 4.5

Technocrat
Mar 2nd, 2001, 11:28 AM
I dont know Borland. You should just be able to to add new files to your win32 app. Would you like me to send you the working copy I made?

CyberCarsten
Mar 2nd, 2001, 11:32 AM
Yes, that would be fine thanks...:)

Technocrat
Mar 2nd, 2001, 11:36 AM
Ok it was made in MSVC++

CyberCarsten
Mar 2nd, 2001, 11:39 AM
I get 26 errors on compile :D

CyberCarsten
Mar 2nd, 2001, 11:43 AM
Now I tried adding the files to a Win32 GUI...No errors...No menu.....
What is wrong here!!!!???????????????????????????????????????????

Technocrat
Mar 2nd, 2001, 11:50 AM
But it tried to run?
Try stepping through it and see if something is not set right and it is exiting

CyberCarsten
Mar 2nd, 2001, 11:52 AM
It ran...I also tried to compile it with Borland C++ 5.5 compiler...again it ran....but no menus....
I'm an EXTREME newbie to C++!!! :)

Technocrat
Mar 2nd, 2001, 11:57 AM
Its ok we were all there once. It sucks but you will get over it.

Is it ok if I ICQ you, this back and forth thing is slow.

CyberCarsten
Mar 2nd, 2001, 11:58 AM
Sure, I just have to a new ICQ nr. I lost the password for the old one...DOOOHHH!!! :) 2 sec.! :)

Technocrat
Mar 2nd, 2001, 12:02 PM
Mine # 23241737

CyberCarsten
Mar 2nd, 2001, 12:05 PM
Great! :) Mine is 110200534

Technocrat
Mar 2nd, 2001, 12:08 PM
It will not find you, prob because you are so new, add me, then I will add you when I respond

CyberCarsten
Mar 2nd, 2001, 12:09 PM
It won't go online....Can't establish connection????

Amon Ra
Mar 2nd, 2001, 10:01 PM
hey CyberCarsten, how long have you been doing c++?
Did you start with console??

CyberCarsten
Mar 3rd, 2001, 02:56 AM
I have been console programming for about 5 months, so I tought it was time to move up the ladder! :)

Amon Ra
Mar 3rd, 2001, 01:03 PM
What kind of stuff were you doin in console programming? Classes??...
thanx:)

CyberCarsten
Mar 3rd, 2001, 01:12 PM
That to, and all sorts of other things....If statements, loops etc...

parksie
Mar 3rd, 2001, 03:49 PM
Did you do pointers, addresses, arrays, references, stuff like that? They're pretty heavily used in the Win32 API.

Amon Ra
Mar 3rd, 2001, 04:14 PM
Are these related to pointers? Cuz , i know how to get the address and the actual value of a variable using a pointer.

parksie
Mar 3rd, 2001, 04:18 PM
Pointers are related to references, but the difference is that you can't mess around with references as much -- they're a lot safer. An example:

int func(int *ptr) {
return (*ptr += 5); // Increment the data pointed to by ptr
}

int reffunc(int &ptr) {
return ptr += 5;
}

int main() {
int x = 3;

func(&x);
reffunc(x);
}

Amon Ra
Mar 3rd, 2001, 07:52 PM
Ok, i understand :) thanx.

Amon Ra
Mar 3rd, 2001, 11:50 PM
Actually Parksie, i dont understand the few lines you wrote:(. What is the difference between the 2 functions? Thanx :)

parksie
Mar 4th, 2001, 06:00 AM
func requires a pointer to the variable, which it then has to dereference (turning a pointer into a reference). Unfortunately if this pointer is invalid then the program will fail.

reffunc requires a reference to the variable, which it can use straight off. It doesn't need to dereference it since it's already a reference. This way it is protected against bad input.

The difference between pointers and references is slightly hazy and I only really understood it after just playing around seeing what worked and what didn't. :(

CyberCarsten
Mar 4th, 2001, 06:49 AM
I don't know if you have looked at the code above.
It's driving me crazy!!! I have tried to compile it with BC4.5, Bc5.5 and Dev-C++ 4, but the menu just wont show up! I don't get any errors....
Do you know what's wrong????

parksie
Mar 4th, 2001, 06:52 AM
Try this:

SetMenu(hwnd,LoadMenu(hinstance, MAKEINTRESOURCE(MYMENU)));

CyberCarsten
Mar 4th, 2001, 06:58 AM
It errors out: Undefined symbol MYMENU in function WinMain

parksie
Mar 4th, 2001, 07:00 AM
What's the resource ID of your menu? As in the number...there should be a preprocessor definition for it in one of the .h files.

CyberCarsten
Mar 4th, 2001, 07:01 AM
This is my mymenu.h file:


// MYMENU.H - header with menu id's
// Notice the clean naming I used for the id's you
// can very quickly figure out the menu, sub menu, and item

#define ID_MYMENU_FILE_OPEN 1000
#define ID_MYMENU_FILE_CLOSE 1001
#define ID_MYMENU_FILE_EXIT 1002
#define ID_MYMENU_HELP_ABOUT 2000


parksie
Mar 4th, 2001, 07:04 AM
There should be an ID for the menu bar itself. What is it called in your menu editor?

CyberCarsten
Mar 4th, 2001, 07:09 AM
the Item id box is grayed out, and I can't view it.

parksie
Mar 4th, 2001, 07:12 AM
Can you zip up all the .h, .rc, .cpp files and anything that looks like source code, please?

I'll see what I can do.

CyberCarsten
Mar 4th, 2001, 07:16 AM
Here it is :)

parksie
Mar 4th, 2001, 07:17 AM
In mymenu.h, add the line

#define MYMENU 1000

CyberCarsten
Mar 4th, 2001, 07:24 AM
Still no menu.... :(

parksie
Mar 4th, 2001, 07:26 AM
Hmm...I can't see what Borland's doing there. In VC++ when you create a menu it automatically makes an ID for it.

CyberCarsten
Mar 4th, 2001, 07:28 AM
When I tried to compile and run the program under BC4.5, it said that it was unable to run the program.
The I tried to run the progam from the Explorer, it told me that, the file CW3215.DLL was missing.

Then I compiled it in Dev C++. It compiled, but no menu....

parksie
Mar 4th, 2001, 07:29 AM
Was BC4.5 installed properly? The CW DLL should have been installed into your system folder.

CyberCarsten
Mar 4th, 2001, 07:30 AM
It should, but errors do happen...
Would it help to reinstall BC45???
Was does the DLL file do???

parksie
Mar 4th, 2001, 07:31 AM
I think it's some kind of runtime DLL. It's similar to msvcrt.dll in that case.

CyberCarsten
Mar 4th, 2001, 07:32 AM
Could it have something to do with the menus not showing up????

parksie
Mar 4th, 2001, 07:34 AM
No. It'll just prevent the program from running at all. I think it's a problem with the resource file -- something's definitely not right there, as the ID should have been allocated by the editor :confused:

This is why I stopped using Borland :rolleyes:

CyberCarsten
Mar 4th, 2001, 07:35 AM
Yes, I would like to kick it to the moon, but, it's all I got...

parksie
Mar 4th, 2001, 07:37 AM
Yeah :(
Well...I'll make some tweaks to yours and see if it works...

CyberCarsten
Mar 4th, 2001, 07:38 AM
Thank you! I really appreciate it! :)

parksie
Mar 4th, 2001, 07:48 AM
Okay...I've had a go but can't guarantee anything since I don't have BC...

CyberCarsten
Mar 4th, 2001, 08:30 AM
Hi again! Thanks for looking at my project.
I get 2 errors:

Line 56: Undefined symbol S_HREDRAW in funtion WinMain

Line 74: Statement missing ; in function WinMain

parksie
Mar 4th, 2001, 08:34 AM
Okay...replace the S_HREDRAW with CS_HREDRAW.

And add a semicolon to the end of this line:

main_window_handle = CreateWindow(WINDOW_CLASS_NAME, "Hello Carsten! The C++ Programmer!", WS_OVERLAPPEDWINDOW | WS_VISIBLE, 500, 500, 320 ,200, NULL, NULL, hinstance, NULL)


I think I missed a few things when I rearranged the code so that I could understand it ;)

CyberCarsten
Mar 4th, 2001, 08:37 AM
:D OK

I'm sorry to say this, but....still no menu.....is it the compiler or Windows thats screwed up....again....

parksie
Mar 4th, 2001, 08:40 AM
Bollocks.

I think it's the compiler...using it that way worked fine with VC. :(

CyberCarsten
Mar 4th, 2001, 08:43 AM
That's strange...
I tried it with the new Borland C++ 5.5.1 compiler, and compiled the .cpp file as a Windows program. Nothing happend...
Then I tried compiling your code in Dev-C++ 4...nothing happend either......:confused:

parksie
Mar 4th, 2001, 08:49 AM
Last-ditch effort -- I'll put my code into a VC++ project and see if it works, then send you the code files.

CyberCarsten
Mar 4th, 2001, 08:50 AM
Ok, thanks...

parksie
Mar 4th, 2001, 11:41 AM
Okay, this works in VC++...

CyberCarsten
Mar 4th, 2001, 01:19 PM
Hi! :)
The buttons work fine, but still no menu... compiled with Dev C++ 4.
In BC I get an error at line 8.

parksie
Mar 4th, 2001, 01:21 PM
Replace __stdcall with CALLBACK should make it work in Borland.

I don't know what the other problem is, since it works perfectly for me.

CyberCarsten
Mar 4th, 2001, 01:26 PM
Now it totally errors out and can't run the program, not even from the explorer. I think my borland files are fuc***!
I Have an Bc45 menu example, that worked yesterday, but when I use my/your code, it doesn't....

Amon Ra
Mar 4th, 2001, 04:49 PM
So hoiw do you know if one is a pointer, and another is a reference??

parksie
Mar 4th, 2001, 04:54 PM
Because a reference has the same type as the value it references. A pointer has the type of a pointer to that type.

CyberCarsten
Mar 5th, 2001, 06:54 AM
parksie!! I did it!!
I forgot to include the mymenu.rc in the compiler options!! DOOOH!!!
Thanks for all your help! :)

parksie
Mar 5th, 2001, 11:53 AM
Hehe always the simple things :rolleyes:

I've got many bruises from kicking myself senseless over things like this :)

CyberCarsten
Mar 5th, 2001, 12:21 PM
Same here!!!! :D