Menu Class v1 and v2

Pages: 12
Here is the Current Release of my Menu Class (Feel free to reuse, share, etc.):

An example to demonstrate the different features of the Menu class
Menu_Example.cpp: http://pastebin.com/UEguBKYN

The file that contains the Menu Class
lib\menu.h: http://pastebin.com/qA3eub6J

The file that contains the now standard function prototypes
lib\header.h: http://pastebin.com/U1g2FkkZ

The file that contains the now standard function definitions
lib\header.cpp: http://pastebin.com/nGfwUW88

Compressed folder that has all the files and a .exe
All library files are in the lib directory and the .exe is in the bin folder
ZIP: https://docs.google.com/open?id=0BxpujE9VEpKAUko5U0hsNGJrb2M

OS's Tested: Windows 32bit
Compiled with g++ 4.6.2


Menu Class v2 (still requires the header files)
menu2.h
: http://pastebin.com/8XqDGdQh
Menu_Example2.cpp: http://pastebin.com/iiCcsYeh
ZIP: https://docs.google.com/open?id=0BxpujE9VEpKAOHB0QVhzalRrNWs

Updates:
6/14/2012 - Allowed the menu to be relocated.
6/15/2012 - Removed all non standard function calls from my code. (Thank you Duoas)
6/18/2012 - Menu v2 released!
New features include calling functions from each option directly. No more messy code just to run your menu. Just select Play, and you're on your way.
Last edited on
It looks pretty cool, but im getting these errors:

F:\C++\Menu\_VP_MENU.cpp||In member function 'int Menu::Play(int)':|
F:\C++\Menu\_VP_MENU.cpp|18|error: 'VP_ClearScreen' was not declared in this scope|
F:\C++\Menu\_VP_MENU.cpp|38|error: 'VP_GoToXY' was not declared in this scope|
F:\C++\Menu\_VP_MENU.cpp|56|error: 'VP_GoToXY' was not declared in this scope|
||=== Build finished: 3 errors, 0 warnings ===|

Sorry about that. I have updated my main post with both header.h and header.cpp. I also included a sample use of the class in the main.cpp Example to save people the headache of writing their own.

I included detailed instructions, and some of the code hasn't been fully implemented yet, and there is still room for improvement, as there is in just about any code.

#welcomeCriticism
Can anyone suggest any improvements upon my code? Simple or complex, I don't care. I know it's not perfect, but every little thing would be greatly appreciated.
Mine didn't compile until I changed <string.h> to <string>. header.h is one big comment :)

Pretty cool. I can't think of anything to improve it. I will say that making a menu in a Win32 app is one of the easiest parts of that process:

1
2
3
4
5
6
7
8
9
10
11
ID_MENU MENU
{
    POPUP "File"
    {
        MENUITRM "New", ID_NEW
	MENUITEM "Save", ID_SAVE
        MENUITEM "Open", ID_OPEN
	MENUITEM "Close", ID_CLOSE
        MENUITEM "Exit", ID_EXIT
    }
}


Where the ID_... are #define as unique integers.


Actually, one thing. Once you make a selection, you don't want to see the menu anymore.
Last edited on
Yes, I do know that, but the menu is set on a loop, that way when, let's say the user selects the first option, which is play tetris, you play tetris and once tetris is done, you will return to the main menu. You can prevent the menu from looping by simply removing the do while loop. But that is the best way I can think of to show all the features of the menu class, and in my opinion, the way it would be used te most.

I'll have to change the string include then.

And yes, I know win32 API is much simpler, but this is the best I can do with the console.
Last edited on
#Volatile Pulse

Your menu class program, looks very good.
// TODO (Volatile Pulse#6#): Allow different positions of the menu.
I like your idea of placing the menu somewhere else than top left corner, so I'm going to work on doing that with my copy of your program.

Great job, and thanks for sharing..
I might be implementing that tonight actually. I want to be able to select positions in the nine popular positions, top, center, bottom, left, center, right. I also set it up so that it aligns that way as well, as if you had center align, or right align.

Another implementation I just thought about is invidiual colors for each menu, but a lot of people might not care about that. It's still something I'm thinking about though. Even possibly allowing different characters to be used to indicate specific letters should be a certain color, using !1 for white text, #1 for white background and then using ## or some other way to allow the symbol to be used without being mistaken for a color. More as an escape character.

Thoughts?
#Volatile Pulse

Concerning the nine popular positions, does this mean your menu would be 3 selections across and 3 down, instead of being listed vertically? Or even 4 or 5 across or down even? That would make an interesting onscreen menu. And the only reason I see for using different colors in the menu, would be so you highlight the letter that the user presses, to activate the choice line, or can use the enter key, after menu keyboard selection. I think that would work very nicely.

If you get the menu positioning worked out today, or even tomorrow, I, for one, would be very happy to see the results, as I'm sure a few of the other programmers that are watching your thread, would be too.
I have already started, and should be done everything within a few hours. A sample of what's done so far is here:
http://www.cplusplus.com/forum/beginner/73218/

I've only just begun on the title and separator part, but the options and selectors should be done shortly.

As for the colors, I don't think it will be a highly requested feature, just one more thing that can be done. But I could implement something similar to how windows API is set up. Put an ampersand in front of a character to signify the key to press. For example, "My &1st option" would display:
My 1st option
And allows the user to press the 1 to automatically select that option. Two ampersands would display a single ampersand, but I would have to set up a default color for the user to press, maybe a blue or green.
I just completed it. I was tutoring someone last night and was unable to finish it until tonight. Hope you guys enjoy. I tested it a little bit, but if you notice anything wrong with anything in the menu, don't hesitate to point it out.

The original post has been updated to reflect the new URL.
This seems very cool, ill have to implement this in my little console game i've been creating here: http://www.cplusplus.com/forum/lounge/73282/
Sadly i dont have access to my programming computer right now, but im wondering what your menu could do better for(if you have the extra time, i encourage you to try out my game and laugh at my current menu). Im definitely interested in using your menu though, thanks!
Mine still isnt compiling, im getting a headache amount of errors trying to get this to work. Can you put up a link to a zip of the files that you knows works?
Yeah, I'm working on setting up the account to store my .zip and source files. You're using Code::Blocks on WinOS right? Can you give me a general example of your errors?
@Volatile Pulse

I added in your updated menu class, and then looked through main.cpp, to see how you implemented the improved menu system, and didn't see any change. After then looking at the menu class again, did I see a way to use it. I then added in the command to center it mid screen, and it worked beautifully. Nicely done. Now the fun part. Seeing if I can incorporate your that menu system into one of my programs.
I'm sorry for the headache for everyone that has been having issues. I'm updating my original post now to include a downloadable .zip to make it easier for everyone else. Included is the lib folder with the three required files, and a bin folder with a copy of my test example.
I have everything updated, looking good, and have updated all of my links in my first post. I have removed ALL non standard function calls that I'm personally aware of (thanks Duoas), and have created a better example for the menu class.
Hey guys, just wanted to give you a heads up that there is going to be a new release of the menu coming in the, hopefully, very near future. It implements some new features in C++11 so since some people's compilers may not yet still support it, I'm leaving my original menu up.

The update will first consist of a way to pass functions to the menu so that when the user selects an option from the menu, the menu class will automatically call that function. It will save you lines of coding when implementing the menu.

Another hopeful addition will be to add radio style options in the menu. Think more of the lines as an on/off switch. And also be able to accept other settings such as setting a difficulty in a game from easy, medium, hard, etc and allowing numbers 1-10.

Currently just implementing the function calls, but that should be done shortly.
@Volatile Pulse

The new additions to your menu class, sound very interesting, and I would be happy to be able to try them out, when completed. My problem, though, is I don't know which compilers are C++11 compliant. I use Microsoft Visual Studio 2008 Express Edition, and have, but haven't installed, Microsoft Visual Studio 2010 Express. Can either of these be used? Or should I look at a different one?

Thanks..
I don't believe VC++10 supports much C++11, mainly since the C++11 standard was released in 2011. However, it might be able to support it, try to #include <functional> into a project and see if the compiler can find it. If it does, you should be fine, if not, you might need to upgrade.

I personally use the MinGW release 4.6.2 which supports, most if not all, of the C++11 standard. I use this with the latest nightly build over at Code::Blocks. It's a bit of a hassle to set up, but definitely well worth the added features in my opinion.

Edit: You may need to see if your compiler has an option for C++0x or C++11 support through the command line. The gcc's line is -std=c++0x.
Last edited on
Pages: 12