/*
@Author:Mr.Suppakit Thongdee
@Website: www.sourcecode.in.th
*/
#include <stdio.h>
#include <conio.h>
void display_menu(int selected){
const xpos =20;
const ypos =1;
const txtcolor =15; //white
const bgcolor = 9; //blue
const bgcolor_select =4; //red
textcolor(txtcolor);
textbackground(bgcolor);
clrscr();
char menu[4] = " ";
menu[selected] = '>';
gotoxy(xpos,ypos);
cprintf("Main Menu [ENTER to select, ESC to exit]");
gotoxy(xpos,ypos+1);
cprintf("----------------------------------------");
if(selected==0){
textbackground(bgcolor_select);
}else{
textbackground(bgcolor);
};
gotoxy(xpos,ypos+2);
cprintf(" First menu ");
if(selected==1){
textbackground(bgcolor_select);
}else{
textbackground(bgcolor);
};
gotoxy(xpos,ypos+3);
cprintf(" Second menu ");
if(selected==2){
textbackground(bgcolor_select);
}else{
textbackground(bgcolor);
};
gotoxy(xpos,ypos+4);
cprintf(" Third menu ");
if(selected==3){
textbackground(bgcolor_select);
}else{
textbackground(bgcolor);
};
gotoxy(xpos,ypos+5);
cprintf(" Forth menu ");
textcolor(txtcolor); //white
textbackground(bgcolor); //blue
gotoxy(xpos,ypos+6);
cprintf("----------------------------------------");
}
void menu1(){
clrscr();
printf("This is First program \n\t press any key to exit");
getch();
}
void menu2(){
clrscr();
printf("This is Second program \n\t press any key to exit");
getch();
}
void menu3(){
clrscr();
printf("This is Third program \n\t press any key to exit");
getch();
}
void menu4(){
clrscr();
printf("This is Forth program \n\t press any key to exit");
getch();
}
void main(){
int ch;
int selected = 0; //0-3 for 4 menu
clrscr();
//ESC=27, Spacebar=32, Enter=13
//arrow key UP=0,72
//arrow key Down=0,80
do{
//display menu
display_menu(selected);
//get keypress
ch = getch();
if(ch==0){ //check function key or not
ch = getch();
if(ch==72){ //check arrow key UP
selected--;
}else if(ch==80){ //check arrow key DOWN
selected++;
}
//update select menu value 0 to 3
if(selected<0){
selected = 3;
}else if(selected>3){
selected = 0;
}
display_menu(selected);
}else if(ch==13){ //ENTER
//Call select program
switch(selected){
case 0: menu1(); break;
case 1: menu2(); break;
case 2: menu3(); break;
case 3: menu4(); break;
}
}
}while(ch!=27); //exit when press ESC on keyboard
}