Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- meny_with_ribbon.c
- Example for meny with ribbon (bar) moved by Up and Down arrows.
- You can choose colors for
- int mfc=4, mbc=7, // meny table foregroud and background color
- tfc=11, tbc=2, // meny title foregroud and background color
- ifc=10, ibc=6, // meny items foregroud and background color
- bfc=1, bbc=7, // meny item under bar foregroud and background color
- bar_item_num=3, // item under bar
- Code of each color:
- Name Value
- Black 0
- Blue 1
- Green 2
- Cyan 3
- Red 4
- Magenta 5
- Brown 6
- Light Gray 7
- Dark Gray 8
- Light Blue 9
- Light Green 10
- Light Cyan 11
- Light Red 12
- Light Magenta 13
- Yellow 14
- White 15
- You can find all my C programs at Dragan Milicev's pastebin:
- https://pastebin.com/u/dmilicev
- */
- #include <stdio.h>
- #include <string.h>
- #include <stdarg.h> // for variable list of arguments, va_start() and va_end()
- #include <windows.h> // tracking cursor all the time, for cursor position functions
- // gotoxy(x,y) , wherex() , wherey(), showcursor(), hidecursor()
- #define UP 72 // up arrow
- #define DOWN 80 // down arrow
- #define BUFFER_SIZE 100
- // ----------CURSOR CONTROL FUNCTIONS------------------------------------------
- // make console cursor invisible
- void hidecursor()
- {
- HANDLE consoleHandle = GetStdHandle(STD_OUTPUT_HANDLE);
- CONSOLE_CURSOR_INFO info;
- info.dwSize = 20;
- info.bVisible = FALSE;
- SetConsoleCursorInfo(consoleHandle, &info);
- }
- // make console cursor invisible
- void showcursor()
- {
- HANDLE consoleHandle = GetStdHandle(STD_OUTPUT_HANDLE);
- CONSOLE_CURSOR_INFO info;
- info.dwSize = 20;
- info.bVisible = TRUE;
- SetConsoleCursorInfo(consoleHandle, &info);
- }
- // place cursor at position ( x, y ) = ( row, column )
- void gotoxy( int x, int y )
- {
- COORD coord;
- coord.X = x;
- coord.Y = y;
- SetConsoleCursorPosition( GetStdHandle(STD_OUTPUT_HANDLE), coord );
- }
- // return x coordinate (column) of current cursor position
- // on failure return -1
- int wherex()
- {
- CONSOLE_SCREEN_BUFFER_INFO csbi;
- if (!GetConsoleScreenBufferInfo( GetStdHandle(STD_OUTPUT_HANDLE), &csbi ) )
- return -1;
- return csbi.dwCursorPosition.X;
- }
- // return y coordinate (row) of current cursor position
- // on failure return -1
- int wherey()
- {
- CONSOLE_SCREEN_BUFFER_INFO csbi;
- if (!GetConsoleScreenBufferInfo( GetStdHandle( STD_OUTPUT_HANDLE ), &csbi ) )
- return -1;
- return csbi.dwCursorPosition.Y;
- }
- // END OF-----CURSOR CONTROL FUNCTIONS------------------------------------------
- // ----------TEXT AND BACKGROUND COLOR CONTROL FUNCTIONS------------------------
- void ClearConsoleToColors(int ForgC, int BackC)
- {
- WORD wColor = ((BackC & 0x0F) << 4) + (ForgC & 0x0F);
- ///Get the handle to the current output buffer...
- HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
- ///This is used to reset the carat/cursor to the top left.
- COORD coord = {0, 0};
- ///A return value... indicating how many chars were written
- /// not used but we need to capture this since it will be
- /// written anyway (passing NULL causes an access violation).
- DWORD count;
- ///This is a structure containing all of the console info
- /// it is used here to find the size of the console.
- CONSOLE_SCREEN_BUFFER_INFO csbi;
- ///Here we will set the current color
- SetConsoleTextAttribute(hStdOut, wColor);
- if(GetConsoleScreenBufferInfo(hStdOut, &csbi))
- {
- ///This fills the buffer with a given character (in this case 32=space).
- FillConsoleOutputCharacter(hStdOut, (TCHAR) 32, csbi.dwSize.X * csbi.dwSize.Y, coord, &count);
- FillConsoleOutputAttribute(hStdOut, csbi.wAttributes, csbi.dwSize.X * csbi.dwSize.Y, coord, &count );
- ///This will set our cursor position for the next print statement.
- SetConsoleCursorPosition(hStdOut, coord);
- }
- return;
- } //ClearConsoleToColors()
- void SetColorAndBackground(int ForgC, int BackC)
- {
- WORD wColor = ((BackC & 0x0F) << 4) + (ForgC & 0x0F);;
- SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), wColor);
- return;
- } //SetColorAndBackground()
- // END OF----TEXT AND BACKGROUND COLOR CONTROL FUNCTIONS------------------------
- // choice_1,
- void choice_1(void)
- {
- printf("\n\n Your choice is choice_1 \n\n");
- }
- // choice_2,
- void choice_2(void)
- {
- printf("\n\n Your choice is choice_2 \n\n");
- }
- // choice_3,
- void choice_3(void)
- {
- printf("\n\n Your choice is choice_3 \n\n");
- }
- // choice_4,
- void choice_4(void)
- {
- printf("\n\n Your choice is choice_4 \n\n");
- }
- // choice_5,
- void choice_5(void)
- {
- printf("\n\n Your choice is choice_5 \n\n");
- }
- // choice_6,
- void choice_6(void)
- {
- printf("\n\n Your choice is choice_6 \n\n");
- }
- // choice_7,
- void choice_7(void)
- {
- printf("\n\n Your choice is choice_7 \n\n");
- }
- // choice_8,
- void choice_8(void)
- {
- printf("\n\n Your choice is choice_8 \n\n");
- }
- // choice_9,
- void choice_9(void)
- {
- printf("\n\n Your choice is choice_9 \n\n");
- }
- // meny_1() returns the ordinal number of the selected meny item.
- // If the Escape key is pressed, nothing is selected and then returns 0.
- int meny_1( int x, int y, // x,y coordinates of upper left angle of meny
- char meny_title[], // meny title
- int mfc, int mbc, // meny table foregroud and background color
- int tfc, int tbc, // meny title foregroud and background color
- int ifc, int ibc, // meny items foregroud and background color
- int bfc, int bbc, // meny item under bar foregroud and background color
- int bar_item_num, // ordinar number of meny item under bar
- int number_of_meny_items, ... ) // number of meny items, meny items...
- {
- int i, j, c_choice, width, max_width=strlen(meny_title);
- char buf[BUFFER_SIZE],
- bar_item[BUFFER_SIZE],
- upper_item[BUFFER_SIZE],
- lower_item[BUFFER_SIZE];
- va_list valist; // list of meny items
- hidecursor(); // hide cursor
- //system("CLS");
- // calculate max_width of meny title and items, upper, lower and bar item
- va_start(valist, number_of_meny_items); // initialize valist for number of meny items
- for (i = 0; i < number_of_meny_items; i++) // access all the meny items assigned to valist
- {
- sprintf( buf,"%s", va_arg(valist, char*) );
- width = strlen( buf ); // width of current meny item
- if ( max_width < width ) // calculate max_width of meny title and items
- max_width = width;
- if ( i+1 == bar_item_num - 1 ) // meny item above bar
- sprintf( upper_item,"%s", buf );
- if ( i+1 == bar_item_num ) // bar_item, meny item with bar
- sprintf( bar_item,"%s", buf );
- if ( i+1 == bar_item_num + 1 ) // bar_item, meny item below bar
- sprintf( lower_item,"%s", buf );
- }
- va_end(valist); // clean memory reserved for valist
- // print meny table
- SetColorAndBackground(mfc,mbc); // set whole meny color foreground and backgroud
- // line over title
- gotoxy(x,y);
- printf("%c",201); // upper left corner
- for(i = 0; i < max_width; i++)
- {
- gotoxy(x+i+1,y);
- printf("%c",205); // most upper line
- }
- printf("%c",187); // upper right corner
- // print meny title
- gotoxy(x,y+1);
- printf("%c",186); // most left vertical character
- width = strlen(meny_title); // center meny title
- SetColorAndBackground(tfc,tbc); // set whole meny color foreground and backgroud
- j=0; // to count spaces before title
- for(i = 0; i < (max_width-width)/2; i++) // spaces before title
- {
- gotoxy(x+i+1,y+1);
- printf(" ");
- j++; // count spaces before title
- }
- printf("%s", meny_title ); // print meny title
- width = strlen(meny_title) + j; // plus j (spaces before title)
- while( max_width-width ) // spaces after meny title
- {
- printf(" ");
- width++;
- }
- SetColorAndBackground(mfc,mbc); // set whole meny color foreground and backgroud
- printf("%c",186); // most right vertical line character, after meny title
- // print meny table line under title
- gotoxy(x,y+2);
- printf("%c",204); // most left cross
- for(i = 0; i < max_width; i++)
- printf("%c",205); // line under title
- printf("%c",185); // most right cross
- // print the lowest menu line
- gotoxy(x,y+number_of_meny_items+3);
- printf("%c",200); // down left corner
- for(i = 0; i < max_width; i++)
- printf("%c",205); // down line
- printf("%c",188); // down right corner
- // print lines with meny items
- va_start(valist, number_of_meny_items); // initialize valist for num number of arguments
- for (i = 0; i < number_of_meny_items; i++) // access all the arguments assigned to valist
- {
- // print most left vertical line character, after meny item
- SetColorAndBackground(mfc,mbc); // set whole meny color foreground and backgroud
- gotoxy(x,y+i+3);
- printf("%c",186); // print most left vertical line character, after meny item
- if ( i+1 == bar_item_num ) // bar_item, meny item under bar
- SetColorAndBackground(bfc,bbc); // set item meny color foreground and backgroud
- else // regular meny item
- SetColorAndBackground(ifc,ibc); // set item meny color foreground and backgroud
- // print meny item
- sprintf(buf,"%s", va_arg(valist, char*) );
- printf("%s", buf );
- width = strlen(buf);
- while( max_width - width ) // print spaces after meny item
- {
- printf(" ");
- width++;
- }
- // print most right vertical line character, after meny item
- SetColorAndBackground(mfc,mbc); // set whole meny color foreground and backgroud
- printf("%c",186); // most right vertical line character, after meny item
- }
- va_end(valist); // clean memory reserved for valist
- while( 1 ) { // an infinite loop that exits with choice 0 or Ecsape
- // scroll meny bar and switch meny choice
- switch( c_choice = _getch() ) { // based on the choices we call the appropriate functions
- case 13 : // Enter, return(bar_item_num)
- SetColorAndBackground(7,0); // reset colors to system default colors
- //gotoxy(0,y+number_of_meny_items+4); // put cursor to most left below meny
- system("CLS");
- showcursor(); // show cursor
- return(bar_item_num);
- break;
- case 27 : // ESC, exit from meny, return(0)
- SetColorAndBackground(7,0); // reset colors to system default colors
- //gotoxy(0,y+number_of_meny_items+4); // put cursor to most left below meny
- system("CLS");
- showcursor(); // show cursor
- return(0);
- break;
- case 0 : // if pressed function taster
- case 224 : // if pressed special function taster
- c_choice = _getch(); // let's empty that 0 or 224
- if( c_choice == UP ) // if pressed up arrow
- {
- if( bar_item_num > 1 ) // move bar up
- {
- bar_item_num--;
- // print lines with meny items
- va_start(valist, number_of_meny_items); // initialize valist for num number of arguments
- for (i = 0; i < number_of_meny_items; i++) // access all the arguments assigned to valist
- {
- // print meny table
- SetColorAndBackground(mfc,mbc); // set whole meny color foreground and backgroud
- gotoxy(x,y+i+3);
- printf("%c",186); // most left vertical line character
- if ( i+1 == bar_item_num ) // bar_item, meny item under bar
- SetColorAndBackground(bfc,bbc); // set item meny color foreground and backgroud
- else // regular meny item
- SetColorAndBackground(ifc,ibc); // set item meny color foreground and backgroud
- // print meny item
- sprintf(buf,"%s", va_arg(valist, char*) );
- printf("%s", buf );
- width = strlen(buf);
- while( max_width-width ) // spaces after meny item
- {
- printf(" ");
- width++;
- }
- // print meny table
- SetColorAndBackground(mfc,mbc); // set whole meny color foreground and backgroud
- printf("%c",186); // most right vertical line character, after meny item
- }
- va_end(valist); // clean memory reserved for valist
- }
- }
- if( c_choice == DOWN ) // if pressed down arrow
- { // move bar down
- if( bar_item_num < number_of_meny_items )
- {
- bar_item_num++;
- // print lines with meny items
- va_start(valist, number_of_meny_items); // initialize valist for num number of arguments
- for (i = 0; i < number_of_meny_items; i++) // access all the arguments assigned to valist
- {
- // print meny table
- SetColorAndBackground(mfc,mbc); // set whole meny color foreground and backgroud
- gotoxy(x,y+i+3);
- printf("%c",186); // most left vertical line character
- if ( i+1 == bar_item_num ) // bar_item, meny item under bar
- SetColorAndBackground(bfc,bbc); // set item meny color foreground and backgroud
- else // regular meny item
- SetColorAndBackground(ifc,ibc); // set item meny color foreground and backgroud
- // print meny item
- sprintf(buf,"%s", va_arg(valist, char*) );
- printf("%s", buf );
- width = strlen(buf);
- while( max_width-width ) // spaces after meny item
- {
- printf(" ");
- width++;
- }
- // print meny table
- SetColorAndBackground(mfc,mbc); // set whole meny color foreground and backgroud
- printf("%c",186); // most right vertical line character, after meny item
- }
- va_end(valist); // clean memory reserved for valist
- }
- }
- break;
- default:
- break;
- } // switch( c_choice )
- } // while( 1 )
- } // menu_1()
- // Main program
- int main(void)
- {
- int x=30, y=5; // x,y coordinates of upper left angle of meny
- char meny_title[]="MENY TITLE"; // meny title
- int mfc=4, mbc=7, // meny table foregroud and background color
- tfc=11, tbc=2, // meny title foregroud and background color
- ifc=10, ibc=6, // meny items foregroud and background color
- bfc=1, bbc=7, // meny item under bar foregroud and background color
- bar_item_num=3, // item under bar
- number_of_meny_items=9; // number of meny items
- switch(
- meny_1( x, y, // x,y coordinates of upper left angle of meny
- meny_title, // meny title
- mfc, mbc, // meny table foregroud and background color
- tfc, tbc, // meny title foregroud and background color
- ifc, ibc, // meny items foregroud and background color
- bfc, bbc, // meny item under bar foregroud and background color
- bar_item_num, // ordinar number of meny item under bar
- number_of_meny_items, // number of meny items
- " first item ", // meny items...
- " second item ",
- " third item ",
- " fourth item ",
- " fifth item ",
- " sixth item ",
- " seventh item ",
- " eighth item ",
- " ninth item " )
- ) { // based on the choices from meny_1 we call the appropriate function
- case 1 :
- choice_1(); // choice_1
- break;
- case 2 :
- choice_2(); // choice_2
- break;
- case 3 :
- choice_3(); // choice_3
- break;
- case 4 :
- choice_4(); // choice_4
- break;
- case 5 :
- choice_5(); // choice_5
- break;
- case 6 :
- choice_6(); // choice_6
- break;
- case 7 :
- choice_7(); // choice_7
- break;
- case 8 :
- choice_8(); // choice_8
- break;
- case 9 :
- choice_9(); // choice_9
- break;
- case 0 : // Zero to end work
- return(0);
- break;
- default:
- break;
- } // switch( meny_1() )
- return 0;
- } // main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement