include stdio include conio include bios define total_pos define color

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include <stdio.h>
#include <conio.h>
#include <bios.h>
#define total_pos 9
#define color1 0
#define color2 7
int menu_draw(int x,int y)
{
textbackground(color1);
textcolor(color2);
for(int i=1;i<=total_pos;i++)
{
gotoxy(x, y+i-1);
cprintf("menu item %d\n\r",i);
}
return 0;
}
int pos_en(int x, int y, int pos)
{
textbackground(color2);
textcolor(color1);
gotoxy(x,y+pos-1);
cprintf("menu item %d",pos);
return 0;
}
int pos_dis(int x, int y, int pos)
{
textbackground(color1);
textcolor(color2);
gotoxy(x,y+pos-1);
cprintf("menu item %d",pos);
return 0;
}
int main()
{
clrscr();
printf("\n Enter - show current pos\n Esc - exit");
int x=5,y=5;
menu_draw(x,y);
int key=0;
int cur_pos=1;
pos_en(x,y,cur_pos);
while(key != 283)
{
key = bioskey(0);
pos_dis(x,y,cur_pos);
switch(key)
{
//up
case 18432:
{
if (cur_pos-1<1) cur_pos=total_pos;
else cur_pos--;
break;
}
//down
case 20480:
{
if (cur_pos+1>total_pos) cur_pos=1;
else cur_pos++;
break;
}
//enter
case 7181:
{
gotoxy(20,5);
printf("Current position: %d",cur_pos);
}
}
pos_en(x,y,cur_pos);
}
return 0;
}