Advertisement
dmilicev

meny_with_ribbon.c

Feb 11th, 2020
576
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 16.31 KB | None | 0 0
  1. /*
  2.  
  3.     meny_with_ribbon.c
  4.  
  5.  
  6.     Example for meny with ribbon (bar) moved by Up and Down arrows.
  7.  
  8.     You can choose colors for
  9.  
  10.     int mfc=4, mbc=7,               // meny table foregroud and background color
  11.         tfc=11, tbc=2,              // meny title foregroud and background color
  12.         ifc=10, ibc=6,              // meny items foregroud and background color
  13.         bfc=1, bbc=7,               // meny item under bar foregroud and background color
  14.         bar_item_num=3,             // item under bar
  15.  
  16.  Code of each color:
  17.  
  18.  Name        Value
  19.  
  20. Black          0
  21. Blue           1
  22. Green          2
  23. Cyan           3
  24. Red            4
  25. Magenta        5
  26. Brown          6
  27. Light Gray     7
  28. Dark Gray      8
  29. Light Blue     9
  30. Light Green   10
  31. Light Cyan    11
  32. Light Red     12
  33. Light Magenta 13
  34. Yellow        14
  35. White         15
  36.  
  37.  
  38.     You can find all my C programs at Dragan Milicev's pastebin:
  39.  
  40.     https://pastebin.com/u/dmilicev
  41.  
  42. */
  43.  
  44.  
  45. #include <stdio.h>
  46. #include <string.h>
  47. #include <stdarg.h>     // for variable list of arguments, va_start() and va_end()
  48. #include <windows.h>    // tracking cursor all the time, for cursor position functions
  49.                         // gotoxy(x,y) , wherex() , wherey(), showcursor(), hidecursor()
  50.  
  51. #define UP               72 // up arrow
  52. #define DOWN             80 // down arrow
  53. #define BUFFER_SIZE     100
  54.  
  55.  
  56. // ----------CURSOR CONTROL FUNCTIONS------------------------------------------
  57.  
  58. // make console cursor invisible
  59. void hidecursor()
  60. {
  61.    HANDLE consoleHandle = GetStdHandle(STD_OUTPUT_HANDLE);
  62.    CONSOLE_CURSOR_INFO info;
  63.    info.dwSize = 20;
  64.    info.bVisible = FALSE;
  65.    SetConsoleCursorInfo(consoleHandle, &info);
  66. }
  67.  
  68. // make console cursor invisible
  69. void showcursor()
  70. {
  71.    HANDLE consoleHandle = GetStdHandle(STD_OUTPUT_HANDLE);
  72.    CONSOLE_CURSOR_INFO info;
  73.    info.dwSize = 20;
  74.    info.bVisible = TRUE;
  75.    SetConsoleCursorInfo(consoleHandle, &info);
  76. }
  77.  
  78. // place cursor at position ( x, y ) = ( row, column )
  79. void gotoxy( int x, int y )
  80. {
  81.     COORD coord;
  82.  
  83.     coord.X = x;
  84.     coord.Y = y;
  85.  
  86.     SetConsoleCursorPosition( GetStdHandle(STD_OUTPUT_HANDLE), coord );
  87. }
  88.  
  89. // return x coordinate (column) of current cursor position
  90. // on failure return -1
  91. int wherex()
  92. {
  93.     CONSOLE_SCREEN_BUFFER_INFO csbi;
  94.  
  95.     if (!GetConsoleScreenBufferInfo( GetStdHandle(STD_OUTPUT_HANDLE), &csbi ) )
  96.         return -1;
  97.  
  98.     return csbi.dwCursorPosition.X;
  99. }
  100.  
  101. // return y coordinate (row) of current cursor position
  102. // on failure return -1
  103. int wherey()
  104. {
  105.     CONSOLE_SCREEN_BUFFER_INFO csbi;
  106.  
  107.     if (!GetConsoleScreenBufferInfo( GetStdHandle( STD_OUTPUT_HANDLE ), &csbi ) )
  108.         return -1;
  109.  
  110.     return csbi.dwCursorPosition.Y;
  111. }
  112.  
  113. // END OF-----CURSOR CONTROL FUNCTIONS------------------------------------------
  114.  
  115.  
  116. // ----------TEXT AND BACKGROUND COLOR CONTROL FUNCTIONS------------------------
  117.  
  118. void ClearConsoleToColors(int ForgC, int BackC)
  119. {
  120.      WORD wColor = ((BackC & 0x0F) << 4) + (ForgC & 0x0F);
  121.      ///Get the handle to the current output buffer...
  122.      HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
  123.      ///This is used to reset the carat/cursor to the top left.
  124.      COORD coord = {0, 0};
  125.      ///A return value... indicating how many chars were written
  126.      ///   not used but we need to capture this since it will be
  127.      ///   written anyway (passing NULL causes an access violation).
  128.      DWORD count;
  129.      ///This is a structure containing all of the console info
  130.      /// it is used here to find the size of the console.
  131.      CONSOLE_SCREEN_BUFFER_INFO csbi;
  132.      ///Here we will set the current color
  133.      SetConsoleTextAttribute(hStdOut, wColor);
  134.      if(GetConsoleScreenBufferInfo(hStdOut, &csbi))
  135.      {
  136.           ///This fills the buffer with a given character (in this case 32=space).
  137.           FillConsoleOutputCharacter(hStdOut, (TCHAR) 32, csbi.dwSize.X * csbi.dwSize.Y, coord, &count);
  138.           FillConsoleOutputAttribute(hStdOut, csbi.wAttributes, csbi.dwSize.X * csbi.dwSize.Y, coord, &count );
  139.           ///This will set our cursor position for the next print statement.
  140.           SetConsoleCursorPosition(hStdOut, coord);
  141.      }
  142.      return;
  143. } //ClearConsoleToColors()
  144.  
  145. void SetColorAndBackground(int ForgC, int BackC)
  146. {
  147.      WORD wColor = ((BackC & 0x0F) << 4) + (ForgC & 0x0F);;
  148.      SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), wColor);
  149.      return;
  150. } //SetColorAndBackground()
  151.  
  152. // END OF----TEXT AND BACKGROUND COLOR CONTROL FUNCTIONS------------------------
  153.  
  154.  
  155. // choice_1,
  156. void choice_1(void)
  157. {
  158.     printf("\n\n Your choice is choice_1 \n\n");
  159. }
  160.  
  161. // choice_2,
  162. void choice_2(void)
  163. {
  164.     printf("\n\n Your choice is choice_2 \n\n");
  165. }
  166.  
  167. // choice_3,
  168. void choice_3(void)
  169. {
  170.     printf("\n\n Your choice is choice_3 \n\n");
  171. }
  172.  
  173. // choice_4,
  174. void choice_4(void)
  175. {
  176.     printf("\n\n Your choice is choice_4 \n\n");
  177. }
  178.  
  179. // choice_5,
  180. void choice_5(void)
  181. {
  182.     printf("\n\n Your choice is choice_5 \n\n");
  183. }
  184.  
  185. // choice_6,
  186. void choice_6(void)
  187. {
  188.     printf("\n\n Your choice is choice_6 \n\n");
  189. }
  190.  
  191. // choice_7,
  192. void choice_7(void)
  193. {
  194.     printf("\n\n Your choice is choice_7 \n\n");
  195. }
  196.  
  197. // choice_8,
  198. void choice_8(void)
  199. {
  200.     printf("\n\n Your choice is choice_8 \n\n");
  201. }
  202.  
  203. // choice_9,
  204. void choice_9(void)
  205. {
  206.     printf("\n\n Your choice is choice_9 \n\n");
  207. }
  208.  
  209.  
  210. // meny_1() returns the ordinal number of the selected meny item.
  211. // If the Escape key is pressed, nothing is selected and then returns 0.
  212. int meny_1( int x, int y,       // x,y coordinates of upper left angle of meny
  213.             char meny_title[],  // meny title
  214.             int mfc, int mbc,   // meny table foregroud and background color
  215.             int tfc, int tbc,   // meny title foregroud and background color
  216.             int ifc, int ibc,   // meny items foregroud and background color
  217.             int bfc, int bbc,   // meny item under bar foregroud and background color
  218.             int bar_item_num,   // ordinar number of meny item under bar
  219.             int number_of_meny_items, ... ) // number of meny items, meny items...
  220. {
  221.     int i, j, c_choice, width, max_width=strlen(meny_title);
  222.     char buf[BUFFER_SIZE],
  223.          bar_item[BUFFER_SIZE],
  224.          upper_item[BUFFER_SIZE],
  225.          lower_item[BUFFER_SIZE];
  226.     va_list valist;             // list of meny items
  227.  
  228.     hidecursor();               // hide cursor
  229.     //system("CLS");
  230.  
  231.     // calculate max_width of meny title and items, upper, lower and bar item
  232.     va_start(valist, number_of_meny_items);     // initialize valist for number of meny items
  233.     for (i = 0; i < number_of_meny_items; i++)  // access all the meny items assigned to valist
  234.     {
  235.         sprintf( buf,"%s", va_arg(valist, char*) );
  236.         width = strlen( buf );  // width of current meny item
  237.  
  238.         if ( max_width < width )                // calculate max_width of meny title and items
  239.             max_width = width;
  240.  
  241.         if ( i+1 == bar_item_num - 1 )          // meny item above bar
  242.             sprintf( upper_item,"%s", buf );
  243.  
  244.         if ( i+1 == bar_item_num )              // bar_item, meny item with bar
  245.             sprintf( bar_item,"%s", buf );
  246.  
  247.         if ( i+1 == bar_item_num + 1 )          // bar_item, meny item below bar
  248.             sprintf( lower_item,"%s", buf );
  249.     }
  250.     va_end(valist);                             // clean memory reserved for valist
  251.  
  252.     // print meny table
  253.     SetColorAndBackground(mfc,mbc); // set whole meny color foreground and backgroud
  254.     // line over title
  255.     gotoxy(x,y);
  256.     printf("%c",201);               // upper left corner
  257.     for(i = 0; i < max_width; i++)
  258.     {
  259.         gotoxy(x+i+1,y);
  260.         printf("%c",205);           // most upper line
  261.     }
  262.     printf("%c",187);               // upper right corner
  263.  
  264.     // print meny title
  265.     gotoxy(x,y+1);
  266.     printf("%c",186);               // most left vertical character
  267.     width = strlen(meny_title);     // center meny title
  268.     SetColorAndBackground(tfc,tbc); // set whole meny color foreground and backgroud
  269.     j=0;                            // to count spaces before title
  270.     for(i = 0; i < (max_width-width)/2; i++)    // spaces before title
  271.     {
  272.         gotoxy(x+i+1,y+1);
  273.         printf(" ");
  274.         j++;                        // count spaces before title
  275.     }
  276.     printf("%s", meny_title );      // print meny title
  277.     width = strlen(meny_title) + j; // plus j (spaces before title)
  278.     while( max_width-width )        // spaces after meny title
  279.     {
  280.         printf(" ");
  281.         width++;
  282.     }
  283.     SetColorAndBackground(mfc,mbc); // set whole meny color foreground and backgroud
  284.     printf("%c",186);               // most right vertical line character, after meny title
  285.  
  286.     // print meny table line under title
  287.     gotoxy(x,y+2);
  288.     printf("%c",204);               // most left cross
  289.     for(i = 0; i < max_width; i++)
  290.         printf("%c",205);           // line under title
  291.     printf("%c",185);               // most right cross
  292.  
  293.     // print the lowest menu line
  294.     gotoxy(x,y+number_of_meny_items+3);
  295.     printf("%c",200);                       // down left corner
  296.     for(i = 0; i < max_width; i++)
  297.         printf("%c",205);                   // down line
  298.     printf("%c",188);                       // down right corner
  299.  
  300.     // print lines with meny items
  301.     va_start(valist, number_of_meny_items); // initialize valist for num number of arguments
  302.     for (i = 0; i < number_of_meny_items; i++) // access all the arguments assigned to valist
  303.     {
  304.         // print most left vertical line character, after meny item
  305.         SetColorAndBackground(mfc,mbc);     // set whole meny color foreground and backgroud
  306.         gotoxy(x,y+i+3);
  307.         printf("%c",186);                   // print most left vertical line character, after meny item
  308.  
  309.         if ( i+1 == bar_item_num )          // bar_item, meny item under bar
  310.             SetColorAndBackground(bfc,bbc); // set item meny color foreground and backgroud
  311.         else                                // regular meny item
  312.             SetColorAndBackground(ifc,ibc); // set item meny color foreground and backgroud
  313.  
  314.         // print meny item
  315.         sprintf(buf,"%s", va_arg(valist, char*) );
  316.         printf("%s", buf );
  317.         width = strlen(buf);
  318.         while( max_width - width )          // print spaces after meny item
  319.         {
  320.             printf(" ");
  321.             width++;
  322.         }
  323.  
  324.         // print most right vertical line character, after meny item
  325.         SetColorAndBackground(mfc,mbc);     // set whole meny color foreground and backgroud
  326.         printf("%c",186);                   // most right vertical line character, after meny item
  327.     }
  328.     va_end(valist);                         // clean memory reserved for valist
  329.  
  330.     while( 1 ) {    // an infinite loop that exits with choice 0 or Ecsape
  331.  
  332.         // scroll meny bar and switch meny choice
  333.         switch( c_choice = _getch() ) {         // based on the choices we call the appropriate functions
  334.  
  335.             case 13  :                  // Enter, return(bar_item_num)
  336.                     SetColorAndBackground(7,0); // reset colors to system default colors
  337.                     //gotoxy(0,y+number_of_meny_items+4);   // put cursor to most left below meny
  338.                     system("CLS");
  339.                     showcursor();       // show cursor
  340.                     return(bar_item_num);
  341.                     break;
  342.             case 27  :                  // ESC, exit from meny, return(0)
  343.                     SetColorAndBackground(7,0); // reset colors to system default colors
  344.                     //gotoxy(0,y+number_of_meny_items+4);   // put cursor to most left below meny
  345.                     system("CLS");
  346.                     showcursor();       // show cursor
  347.                     return(0);
  348.                     break;
  349.             case  0  :                  // if pressed function taster
  350.             case 224 :                  // if pressed special function taster
  351.                     c_choice = _getch();    // let's empty that 0 or 224
  352.  
  353.                     if( c_choice == UP )                            // if pressed up arrow
  354.                     {
  355.                         if( bar_item_num > 1 )                      // move bar up
  356.                         {
  357.                             bar_item_num--;
  358.  
  359.                             // print lines with meny items
  360.                             va_start(valist, number_of_meny_items); // initialize valist for num number of arguments
  361.                             for (i = 0; i < number_of_meny_items; i++) // access all the arguments assigned to valist
  362.                             {
  363.                                 // print meny table
  364.                                 SetColorAndBackground(mfc,mbc);     // set whole meny color foreground and backgroud
  365.                                 gotoxy(x,y+i+3);
  366.                                 printf("%c",186);                   // most left vertical line character
  367.  
  368.                                 if ( i+1 == bar_item_num )          // bar_item, meny item under bar
  369.                                     SetColorAndBackground(bfc,bbc); // set item meny color foreground and backgroud
  370.                                 else                                // regular meny item
  371.                                     SetColorAndBackground(ifc,ibc); // set item meny color foreground and backgroud
  372.  
  373.                                 // print meny item
  374.                                 sprintf(buf,"%s", va_arg(valist, char*) );
  375.                                 printf("%s", buf );
  376.                                 width = strlen(buf);
  377.                                 while( max_width-width )            // spaces after meny item
  378.                                 {
  379.                                     printf(" ");
  380.                                     width++;
  381.                                 }
  382.  
  383.                                 // print meny table
  384.                                 SetColorAndBackground(mfc,mbc);     // set whole meny color foreground and backgroud
  385.                                 printf("%c",186);                   // most right vertical line character, after meny item
  386.                             }
  387.                             va_end(valist);                         // clean memory reserved for valist
  388.                         }
  389.                     }
  390.  
  391.                     if( c_choice == DOWN )                          // if pressed down arrow
  392.                     {                                               // move bar down
  393.                         if( bar_item_num < number_of_meny_items )
  394.                         {
  395.                             bar_item_num++;
  396.  
  397.                             // print lines with meny items
  398.                             va_start(valist, number_of_meny_items); // initialize valist for num number of arguments
  399.                             for (i = 0; i < number_of_meny_items; i++) // access all the arguments assigned to valist
  400.                             {
  401.                                 // print meny table
  402.                                 SetColorAndBackground(mfc,mbc);     // set whole meny color foreground and backgroud
  403.                                 gotoxy(x,y+i+3);
  404.                                 printf("%c",186);                   // most left vertical line character
  405.  
  406.                                 if ( i+1 == bar_item_num )          // bar_item, meny item under bar
  407.                                     SetColorAndBackground(bfc,bbc); // set item meny color foreground and backgroud
  408.                                 else                                // regular meny item
  409.                                     SetColorAndBackground(ifc,ibc); // set item meny color foreground and backgroud
  410.  
  411.                                 // print meny item
  412.                                 sprintf(buf,"%s", va_arg(valist, char*) );
  413.                                 printf("%s", buf );
  414.                                 width = strlen(buf);
  415.                                 while( max_width-width )            // spaces after meny item
  416.                                 {
  417.                                     printf(" ");
  418.                                     width++;
  419.                                 }
  420.  
  421.                                 // print meny table
  422.                                 SetColorAndBackground(mfc,mbc);     // set whole meny color foreground and backgroud
  423.                                 printf("%c",186);                   // most right vertical line character, after meny item
  424.                             }
  425.                             va_end(valist);                         // clean memory reserved for valist
  426.                         }
  427.                     }
  428.                     break;
  429.             default:
  430.                     break;
  431.  
  432.         } // switch( c_choice )
  433.  
  434.     } // while( 1 )
  435.  
  436. } // menu_1()
  437.  
  438.  
  439.  
  440. // Main program
  441. int main(void)
  442. {
  443.     int x=30, y=5;                  // x,y coordinates of upper left angle of meny
  444.     char meny_title[]="MENY TITLE"; // meny title
  445.     int mfc=4, mbc=7,               // meny table foregroud and background color
  446.         tfc=11, tbc=2,              // meny title foregroud and background color
  447.         ifc=10, ibc=6,              // meny items foregroud and background color
  448.         bfc=1, bbc=7,               // meny item under bar foregroud and background color
  449.         bar_item_num=3,             // item under bar
  450.         number_of_meny_items=9;     // number of meny items
  451.  
  452.         switch(
  453.             meny_1( x, y,                   // x,y coordinates of upper left angle of meny
  454.                     meny_title,             // meny title
  455.                     mfc, mbc,               // meny table foregroud and background color
  456.                     tfc, tbc,               // meny title foregroud and background color
  457.                     ifc, ibc,               // meny items foregroud and background color
  458.                     bfc, bbc,               // meny item under bar foregroud and background color
  459.                     bar_item_num,           // ordinar number of meny item under bar
  460.                     number_of_meny_items,   // number of meny items
  461.                     " first item ",         // meny items...
  462.                     " second item ",
  463.                     " third item ",
  464.                     " fourth item ",
  465.                     " fifth item ",
  466.                     " sixth item ",
  467.                     " seventh item ",
  468.                     " eighth item ",
  469.                     " ninth item "  )
  470.               ) {   // based on the choices from meny_1 we call the appropriate function
  471.             case 1 :
  472.                     choice_1();  // choice_1
  473.                     break;
  474.             case 2 :
  475.                     choice_2();  // choice_2
  476.                     break;
  477.             case 3 :
  478.                     choice_3();  // choice_3
  479.                     break;
  480.             case 4 :
  481.                     choice_4();  // choice_4
  482.                     break;
  483.             case 5 :
  484.                     choice_5();  // choice_5
  485.                     break;
  486.             case 6 :
  487.                     choice_6();  // choice_6
  488.                     break;
  489.             case 7 :
  490.                     choice_7();  // choice_7
  491.                     break;
  492.             case 8 :
  493.                     choice_8();  // choice_8
  494.                     break;
  495.             case 9 :
  496.                     choice_9();  // choice_9
  497.                     break;
  498.             case 0 :             // Zero to end work
  499.                     return(0);
  500.                     break;
  501.             default:
  502.                     break;
  503.         } // switch( meny_1() )
  504.  
  505.  
  506.     return 0;
  507.  
  508. } // main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement