PDA

View Full Version : Menu's



Goat-X
12-16-2001, 01:14 AM
anyone know how to make a menu that i can use the up and down arrows to choose what to pick?

maes
12-16-2001, 07:30 AM
This is some code I wrote almost 5 years ago on a TC compiler.
the code is in dutch but I'll tell you what the arguments are, so you can change it.
->int aantal: number of menu items.
->char *hoofdmenu[]: the names of the menu items
->int x: the x position on the screen
->int y: the y position on the screen
->int mkleur: this is the color of the background
->int nkleur: this is the color of the menu background.
NOTE: not sure about mkleur and nkleur could be the other way around
->hoeveel: is just a counter
the function returns the number of the menu item.0 is the upper item.

you need also the function cursor. it turns on and of the cursor(the blinking underscore)

I wrote the main function right now. I can't test it because I don't use TC anymore. My guess is this code is very compiler specifc. so there is a chance it won't work. I hope it does. the basic idea behind the code is to change the background when the user presses a key, and keep track (with the pos variable) of how many time he pressed it



#include<stdio.h>
#include<conio.h>

#define UP 72 /* define of the keys*/
#define DOWN 80
#define ESC 27

void cursor(int type) /*change cursor*/
{
switch(type)
{
case 0 : _setcursortype(_NOCURSOR);break;/*cursor off*/
case 1 : _setcursortype(_NORMALCURSOR);break;/*normal cursor*/
default : break;
}
}

int menu (int aantal,char *hoofdmenu[] ,int x, int y,int mkleur, int nkleur)
{
int hoeveel,i=1,xx,yy,pos=0;

xx=x;
yy=y;
textbackground(nkleur);
cursor(0);
gotoxy(xx,yy);
textbackground(mkleur);
cprintf("%s",hoofdmenu[0]);
textbackground(nkleur);
for(hoeveel=1;hoeveel<aantal;hoeveel++) /*print meu items */
{
yy++;
gotoxy(xx,yy);
cprintf("%s",hoofdmenu[i]);
i++;
}
xx=x;
yy=y;
for(;;)
{
switch(getch())
{
case UP:if(pos!=0) /*move the background one up */
{
textbackground(nkleur);
gotoxy(x,yy);
cprintf("%s",hoofdmenu[pos]);
textbackground(mkleur);
pos--; /*decrease the position */
yy--;
gotoxy(x,yy);
cprintf("%s",hoofdmenu[pos]);
break;
}
else break;

case DOWN: if(pos!=(aantal-1)) /*move background one down*/
{
textbackground(nkleur);
gotoxy(x,yy);
cprintf("%s",hoofdmenu[pos]);
textbackground(mkleur);
pos++; /*increase position */
yy++;
gotoxy(x,yy);
cprintf("%s",hoofdmenu[pos]);
break;
}
else break;
case '\n':
case '\r':
case ESC:textbackground(nkleur);return(pos); /*return the position*/
}
}
}

int main()
{
int choice;
char *mainmenu[]={"option 1","option 2","option 3","option 4","option 5"};

textbackground(BLUE);
clrscr();
textcolor(YELLOW);
choice = menu(5,mainmenu,35,10,RED,BLUE);
textbackground(BLACK);
clrscr();
cursor(1); /*cursor on*/
switch(choice)
{
case 0:printf("option 1 is pressed");break;
case 1:printf("option 2 is pressed");break;
case 2:printf("option 3 is pressed");break;
case 3:printf("option 4 is pressed");break;
case 4:printf("option 5 is pressed");break;
}
return 0;
}

If you have any questions (translation or other) feel free to ask. But like I said, I haven't looked at it in 5 years and it had almost no comment(I'm a very sloppy (amateur)programmer :D)

I really hope this works and that this is what you are looking for.

Unregistered
12-16-2001, 01:09 PM
Very simple to do.

I create an array, such as this:

char menu[3][1][7] = {"First",
"Second",
"Third"};

I believe thats what I typed. 3 lines, 1 column wide, and 7 characters wide (make the array a little bigger).

Then I display the code using a for loop... for(i=0; i<3; i++) printf("%s", menu[0]);

I might have to to make it menu[0][1]... I don't remember without looking at my code.

You then highlight the one you want to start out with, and use a do loop. If the user goes up, un-highlight the current option, highlight the next one up. If the next option up is the top, either don't allow the user to scroll around, or allow them to.

If the user goes down, just reverse this.

The deal is, I never actually write in the array lines. If the user hits the up arrow, I do something like:

textcolor(BLUE);
gotoxy(x,y);
printf("%s", menu[x][x]);
textcolor(LIGHTBLUE);
gotoxy(x,y);
printf("%s", menu[x][x]);

The first is the menu option to un-highlight, the second is the option to highlight.

There are probably other ways to do this, but I found this method very convenient and easy to use.

Only took me maybe half an hour to write a lightbar driven menu that worked fine. And just to make sure it works, when the user hits enter, have it print that option to the screen.

Unregistered
06-17-2002, 05:47 AM
Hi,

I know this thread is slightly old now, but I've just spent the weekend trying to create a scrollable lightbar menu in C.

Creating the menu (as above posts) was easy, but having it scroll when it reaches the bottom item on the menu to display more items seems to be impossibly hard (or I'm going about it the wrong way).

Any help / ideas on this would be appreciated.

Thanks,
Alan.

sean
06-17-2002, 10:58 AM
The code poasted abve looks like the best option for your standard DOS menu - but if you want any customization, just do what he did, but then 'draw' the menu bit by bit. Shouldn't be too difficult if you dive right into it and just start coding.

Unregistered
06-17-2002, 12:30 PM
Hi,

The code for the normal menu isn't a problem, I've already done that. I only found this post when I was looking for a solution to the scrolling menu problem.

I've been coding all weekend, and still can't get it working right :( (I'm not the best programming in the world).

The way I had planned on doing it, was to have the array of filenames, an int to represent the position in the array (ie, that the lightbar is on), and 2 ints to hold the numbers of the top and bottom items currently displayed.

Then, if pos == either the top or the bottom of the menu, delete the first / last option (depending on what direction, shift everything up or down, and put a new option in.

Seems simple written down, but actually trying to do it is a lot harder (for me anyway :-) )

Anyone got a better way of doing this? Am I going about this completly the wrong way??

Thanks,
Alan.

sean
06-22-2002, 04:14 PM
You could just use some circular logic. If its at the top and they press up, just have it wrap around to the bottom. It'll be faster and save some mem.

Quantrizi
06-22-2002, 06:48 PM
The menu that was written in french doesn't work when you try to move up/down

sean
06-23-2002, 03:46 PM
French?