[英]MENU selection Using Arrow keys (example : up and down) in C
I have written the below program in C. It is running perfectly (windows 7, turbo c++). 我用C编写了下面的程序。它运行得很好(Windows 7,turbo c ++)。 The program is to select the number using up arrow
and down arrow
key's and to print it in words . 程序是使用up arrow
和down arrow
键选择数字 up arrow
并用文字打印。
#include<stdio.h>
#include<conio.h>
#include<bios.h>
int main(){
int x,i=2;
g:
clrscr();
printf("Print");
printf("\n 1");
printf("\n 2");
printf("\n EXIT");
printf("\nChoose using arrow key");
gotoxy(1,i);
while((x=bioskey(0))!=75) // I took a value so that the loop continues.
{ // no other reason for 75
if(x==20480){
if(i<=3){
gotoxy(1,i+1);
i=i+1;
}else{}
}else if(x==18432){
if(i==2){}else{
gotoxy(1,i-1);
i=i-1;}
}else if(x==7181){
clrscr();
if(i==2){
printf("ONE ");
}else if(i==3){
printf("TWO ");
}else{
printf("thank you");
getch();
return 0;
}
getch();
goto g;
}else{}
}
getch();
return 0;
}
Is there any method easier than this to achieve the same functionality using arrow keys? 有没有比使用箭头键更容易实现相同功能的方法?
Also help me to highlight the selected row ( the row in which cursor blinks). 还帮我突出显示所选行(光标闪烁的行)。
One more question : While " # "
is typed there is a color change displayed on the screen. 还有一个问题:键入" # "
时,屏幕上会显示颜色变化。 How to change the color of alphabets similarly ? 如何同样改变字母的颜色? Please do not tell answer using <GRAPHICS.H>
请不要使用<GRAPHICS.H>
告诉答案
Anyone in need of answer later may refer this also. 以后需要回答的人也可以参考。 A simple example for selection using arrow key, and highlighting. 使用箭头键和突出显示选择的简单示例。
#include<stdio.h>
#include<conio.h>
#include<bios.h>
void disp(int n)
{
int i=0;
char menu[4][20]={"PRINT","1","2","EXIT"};
clrscr();
gotoxy(1,1);
for(i=0;i<4;i++){
if(i==n){
textbackground(WHITE);
textcolor(RED);
gotoxy(1,i+1);
cprintf("%s\n",menu[i]);
textbackground(BLACK);
textcolor(WHITE);
}else{
gotoxy(1,i+1);
textcolor(WHITE);
cprintf("%s\n",menu[i]);
}
}
}
int main()
{
int x,i=2;
disp(1);
gotoxy(1,i);
while((x=bioskey(0))!=75)
{
gotoxy(1,i);
if(x==20480){
if(i<=3){
i=i+1;
disp(i-1);
gotoxy(1,i);
}else{}
}else if(x==18432){
if(i==2){}else{
i=i-1;
disp(i-1);
gotoxy(1,i);
}
}else if(x==7181){
clrscr();
if(i==2){
printf("ONE");
}else if(i==3){
printf("TWO");
}else{
printf("Thank You");
getch();
return 0;
}
}else{
disp(i-1);
gotoxy(1,i);
}
}
return 0;
}
May be this code works. 可能是这段代码有效。
Warning This code is not compiled/tested. 警告此代码未编译/测试。
int highlight_option(int index)
{
int i;
char menu[4][6] = {"Print", "1", "2", "Exit"};
clrscr();
for (i = 0; i < 4; i++) {
if (i == index) {
textbackground(WHITE);
textcolor(BLACK);
cprintf("%s\n", menu[i]);
textbackground(BLACK);
textcolor(WHITE);
} else {
printf("%s\n", menu[i]);
}
}
return 0;
}
Call this function from the arrow handler, pass i + 1
or i - 1
to this function before the gotoxy
从箭头处理程序调用此函数,在gotoxy
之前将i + 1
或i - 1
传递给此函数
Use bioskey to read the UP and DN arrow keys. 使用bioskey读取UP和DN箭头键。 Refer to the following code: 请参阅以下代码:
#include <stdio.h>
#include <graphics.h>
#include <conio.h>
#include <bios.h>
#define LTARROW 0x4B
#define RTARROW 0x4D
#define UPARROW 0x48
#define DNARROW 0x50
#define CR 0x0d
#define ESC 0x1b
#define F1_Key 0x3b00
#define F2_Key 0x3c00
#define F3_Key 0x3d00
#define F4_Key 0x3e00
#define F5_Key 0x3f00
#define F6_Key 0x4000
#define F7_Key 0x4100
#define F8_Key 0x4200
#define F9_Key 0x4300
#define F10_Key 0x4400
int handle_keyevents()
{
int key = bioskey(0);
if (isalnum(key & 0xFF))
{
printf("'%c' key pressed\n", key);
return 0;
}
switch(key)
{
case F1_Key:
printf("F1 Key Pressed");
break;
case F2_Key:
printf("F2 Key Pressed");
break;
case F3_Key:
printf("F3 Key Pressed");
break;
case F4_Key:
printf("F4 Key Pressed");
break;
case F10_Key:
printf("F10 Key Pressed");
return -1;
default:
printf("%#02x\n", key);
break;
}
printf("\n");
return 0;
}
void main()
{
int key;
printf("Press F10 key to Quit\n");
while(1)
{
key = bioskey(1);
if(key > 0)
{
if(handle_keyevents() < 0)
break;
}
}
}
Reference: 参考:
http://www.softwareandfinance.com/Turbo_C/bioskey.html http://www.softwareandfinance.com/Turbo_C/bioskey.html
http://www.softwareandfinance.com/Turbo_C/Graphics_MoveObject.html http://www.softwareandfinance.com/Turbo_C/Graphics_MoveObject.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.