Thursday, 17 October 2013

A Mini Drop Down Menu

Well 164 lines of code for something this simple......ah that's life!
It looks awfully messy, but I tried to comment most of the lines to explain what they do. So if you copy and paste this into C::B, it should look readable.

It demonstrates the creation of a drop down menu from which the user can select any one of the 5 items: Cans, Boxes, Pieces, Bottles, Strips with the arrow key.

Gotoxy, SetColorAndBackground (requires THIS header file) and the get_key functions are dreadfully fun to play with.
The integer y tracks where the highlight is as usual. If you haven't done something like this already, give it a shot. It can be fun.

The Down arrow key highlights the next item, the Enter key selects the current highlighted item, the Esc key exits the program.
With a bit more effort, the drop down menu can be made to appear like a solid box if you are willing to set a different background colour for the items in it instead of the surrounding black. But you must erase it's trails after the selection. I didn't do it to keep the program shorter.




Program:

#include <myconstdwin.h>

int get_key(void);

int main()
{
    int loop1=1,loop2=1,key,y=0; //loop 1 & 2 contain the while loops, key gets the keypress, y tracks highlight.

    system("cls"); //clear the screen
    gotoxy(0,0);
    printf("Select one item:");
    gotoxy(0,2);
    printf("This line is just here to demonstrate it's reappearing illusion");
    SetColorAndBackground(15,9); //white text, blue background makes it look like it's highlighted
    gotoxy(19,0); //int y is set to 0 because the highlight is originally at cans, located at zero
    printf("Cans   ");
    SetColorAndBackground(8,0); //the down symbol after cans makes it "pretty". Print it "de-highlighted"
    printf("%c",31);
    gotoxy(26,0); //send the cursor over the down symbol while waiting for user keypress
    while(loop1) //start first loop waiting for user to press down
    {
        key=get_key();
        if(key==592 || key==584) //When user hits down a "drop down" appears with can still highlighted.
        {
            gotoxy(19,1);
            printf("Boxes  "); //The spaces after the items make the dropdown look even. Remove them if you don't believe me
            gotoxy(19,2);
            printf("Pieces ");
            gotoxy(19,3);
            printf("Bottles");
            gotoxy(19,4);
            printf("Strips ");
            gotoxy(26,0);
            while(loop2) //with the "drop down" activated, we wait for user to select an item.
            {
                key=get_key();
                if(key==592) //User Hits Down
                {
                    if(y==0) //user hits down when can is highlighted
                    {
                        gotoxy(19,0);
                        printf("Cans   "); //print this "unhighlighted"
                        SetColorAndBackground(15,9); //bring in the highlighting colours
                        gotoxy(19,1); //Move to where Boxes is to be printed
                        printf("Boxes  "); //print "Boxes" highlighted
                        SetColorAndBackground(8,0); //set colour back to normal.
                        y++; //increase y by 1 because we moved up one.
                        gotoxy(26,0); //always move the cursor to where the down symbol is.
                    }
                    else if(y==1) //user hits down when Boxes is highlighted.
                    {
                        gotoxy(19,1);
                        printf("Boxes  "); //print this "unhighlighted"
                        SetColorAndBackground(15,9); //bring in the highlighting colours
                        gotoxy(19,2); //Move to where Pieces is to be printed
                        printf("Pieces "); //print this highlighted
                        SetColorAndBackground(8,0); //set colour back to normal.
                        y++; //increase y by 1 because we moved up one.
                        gotoxy(26,0); //always move the cursor to where the down symbol is.
                    }
                    else if(y==2) //user hits down when Pieces is highlighted.
                    {
                        gotoxy(19,2);
                        printf("Pieces "); //print this "unhighlighted"
                        SetColorAndBackground(15,9); //bring in the highlighting colours
                        gotoxy(19,3); //Move to where Bottles is to be printed
                        printf("Bottles"); //print this highlighted
                        SetColorAndBackground(8,0); //set colour back to normal.
                        y++; //increase y by 1 because we moved up one.
                        gotoxy(26,0); //always move the cursor to where the down symbol is.
                    }
                    else if(y==3) //user hits down when Bottles is highlighted.
                    {
                        gotoxy(19,3);
                        printf("Bottles"); //print this "unhighlighted"
                        SetColorAndBackground(15,9); //bring in the highlighting colours
                        gotoxy(19,4); //Move to where Strips is to be printed
                        printf("Strips "); //print this highlighted
                        SetColorAndBackground(8,0); //set colour back to normal.
                        y++; //increase y by 1 because we moved up one.
                        gotoxy(26,0); //always move the cursor to where the down symbol is.
                    }
                    else if(y==4) //user hits down when Strips is highlighted.
                    {
                        gotoxy(19,4);
                        printf("Strips "); //print this "unhighlighted"
                        SetColorAndBackground(15,9); //bring in the highlighting colours
                        gotoxy(19,0); //Move to where Cans is to be printed
                        printf("Cans   "); //print this highlighted
                        SetColorAndBackground(8,0); //set colour back to normal.
                        y=0; //Y BECOMES 0 BECAUSE WE HAVE MOVED FROM THE LAST ITEM "STRIPS" TO THE FIRST ITEM "CAN"
                        gotoxy(26,0); //always move the cursor to where the down symbol is.
                    }
                }
                else if(key==13) //What ever is highlighted (remember int y is tracking it), user hits enter
                {
                    if(y==0) //Cans
                    {
                        gotoxy(0,1);
                        printf("You have selected \"Cans\". Thank you!");
                    }
                    else if(y==1) //Boxes, pieces, bottles,strips
                    {
                        gotoxy(0,1);
                        printf("You have selected \"Boxes\". Thank you!");
                    }
                    else if(y==2) //Pieces, bottles,strips
                    {
                        gotoxy(0,1);
                        printf("You have selected \"Pieces\". Thank you!");
                    }
                    else if(y==3) //Bottles,strips
                    {
                        gotoxy(0,1);
                        printf("You have selected \"Bottles\". Thank you!");
                    }
                    else if(y==4) //Strips
                    {
                        gotoxy(0,1);
                        printf("You have selected \"Strips\". Thank you!");
                    }
                    /*reprint the broken line. Also make the "Bottles" & "Strips" below it disappear*/
                    gotoxy(0,2);
                    printf("This line is just here to demonstrate it's reappearing illusion");
                    gotoxy(19,3);
                    printf("             ");
                    gotoxy(19,4);
                    printf("             ");
                    gotoxy(26,0); //always move the cursor to where the down symbol is.
                    getch();
                    /*make "you have selected" disappear*/
                    gotoxy(0,1);
                    printf("                                                  ");
                    y=0; //set y back to zero because we are starting over from "Cans"
                    gotoxy(26,0); //always move the cursor to where the down symbol is.
                    loop2=0; //break the second loop so that the whole menu appears when user hits down again
                }
                else if(key==27) //escape key
                {
                    system("cls"); //clear screen
                    exit(0); //exit program
                }
            }
            loop2=1; //make sure this loop starts again
        }
        else if(key==27) //escape key
        {
            system("cls"); //clear screen
            exit(0); //exit the program
        }
    }
}

int get_key(void)
{
    int c = getch();
    switch (c)
    {
      case 0:   return getch()+256;
      case 224: return getch()+512;
    }
    return c;
}



No comments:

Post a Comment