Thread: Menu's

  1. #1
    Registered User
    Join Date
    Dec 2001
    Posts
    1

    Question Menu's

    anyone know how to make a menu that i can use the up and down arrows to choose what to pick?

  2. #2
    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

    Code:
    #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.
    Last edited by maes; 12-16-2001 at 07:37 AM.

  3. #3
    Unregistered
    Guest
    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.

  4. #4
    Unregistered
    Guest
    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.

  5. #5
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    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.

  6. #6
    Unregistered
    Guest
    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.

  7. #7
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    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.

  8. #8
    I am the worst best coder Quantrizi's Avatar
    Join Date
    Mar 2002
    Posts
    644
    The menu that was written in french doesn't work when you try to move up/down

  9. #9
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    French?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Win32 menus and resources help
    By firestorm in forum Windows Programming
    Replies: 24
    Last Post: 04-12-2005, 01:23 PM
  2. Creating pop up menus
    By Ti22 in forum C++ Programming
    Replies: 22
    Last Post: 01-18-2005, 09:27 PM
  3. help with menus example?
    By The Gweech in forum Windows Programming
    Replies: 3
    Last Post: 03-30-2004, 04:41 PM
  4. Menu's
    By Benzakhar in forum Windows Programming
    Replies: 1
    Last Post: 02-20-2004, 10:13 PM
  5. adding menus at runtime
    By bennyandthejets in forum Windows Programming
    Replies: 3
    Last Post: 11-22-2002, 05:07 AM