怎么使用text color和text background函数啊?求教!!
好多年前的东西了。。。
在程序开头打上uses crt;表示使用crt单元,在crt单元里才有textcolor和textbackground函数。
用函数textcolor(x),textbackground(x)可以分别改变字体的颜色,背景颜色。当你输出时就可以看到了。
x表示颜色值(貌似0-15,大于7的会自动mod 8,颜色值会循环使用)。可以为red,blue,green等常量。
从0-15颜色分别为蓝、绿、蓝绿、红、紫、褐、白、黑、亮蓝、亮绿、亮蓝紫、亮红、亮紫、亮黄、白。
textbackground问题
你用的tc?如果是,那么就是对应lib没设置对
如果你用的其他编译器
TextBackground这个函数是没有的,所以当然会出错
如果用的是其他编译器,等价的效果可以用system函数调用color指令实现
#include
#include
void main()
{
int color=0;
char a[16];
for(color=0;color<8;color++)
{
sprintf(a, "color %d", color);
system(a);
printf("This is color %d\r\n",color);
printf("Press any key to continue\r\n");
}
}
C语言中如何实现选项菜单的功能?
#include#include void hello(){printf("hello world\n");}int main(){ int x; while (1) { printf("-------------操作选项-----------\n"); printf("1:返回菜单 \n"); printf("2:退出程序 \n"); printf("3:执行操作 \n"); printf("--------------------------------\n"); printf("按数字键选择要执行的操作: "); scanf("%d",&x); printf("\n"); //输入2跳出循环,退出程序 if(x==2) break; switch(x) { case 1: break; //输入1,跳出switch语句,进入下一次循环 case 3: hello();break; default: //数字输入错误,跳出siwtch语句,进入下一次循环 printf("输入的数字不正确\n"); break; } } return 0;}说明:有3个选项,1是用返回菜单,2是用于退出程序,3是用于执行相关的操作,这里只是一个示例,所以将要执行的操作,编写成了一个输出hello world的函数。
C语言中如何实现选项菜单的功能?
#include
#include
#include
#define Key_DOWN 0x5100
#define Key_UP 0x4900
#define Key_ESC 0x011b
#define Key_ALT_F 0x2100
#define Key_ALT_X 0x2d00
#define Key_ENTER 0x1c0d
void box(int startx,int starty,int high,int width);
main()
{
int i,key,x,y,l;
char *menu[] = {"File","Edit","Run","Option","Help","Setup","Zoom","Menu"};
char *red[] = { "F","E","R","O","H","S","Z","M" };
char *f[] = {"Load file", "Save file", "Print", "Modify ", "Quit A1t_x"};
char buf[16*10*2],buf1[16*2];
while(1)
{
textbackground(BLUE);
clrscr();
textmode(C80);
window(1,1,80,1);
textbackground(LIGHTGRAY);
textcolor(BLACK);
clrscr();
gotoxy(5,1);
for(i=0,l=0;i<8;i++)
{
x=wherex();
y=wherey();
cprintf("%s",menu[i]);
l=strlen(menu[i]);
gotoxy(x,y);
textcolor(RED);
cprintf("%s",red[i]);
x=x+l+5;
gotoxy(x,y);
textcolor(BLACK);
}
gotoxy(5,1);
key=bioskey(0);
switch (key){
case Key_ALT_X:
exit(0);
case Key_ALT_F:
{
textbackground(BLACK);
textcolor(WHITE);
gotoxy(5,1);
cprintf("%s",menu[0]);
gettext(5,2,20,12,buf);
window(5,2,20,9);
textbackground(LIGHTGRAY);
textcolor(BLACK);
clrscr();
box(1,1,7,16);
for(i=2;i<7;i++)
{ gotoxy(2,i);
cprintf("%s",f[i-2]);
}
gettext(2,2,18,3,buf1);
textbackground(BLACK);
textcolor(WHITE);
gotoxy(2,2);
cprintf("%s",f[0]);
gotoxy(2,2);
y=2;
while ((key=bioskey(0))!=Key_ALT_X)
{
if ((key==Key_UP)||(key==Key_DOWN))
{
puttext(2,y,18,y+1,buf1);
if (key==Key_UP)
y=y==2?6:y-1;
else
y=y==6?2:y+1;
gettext(2,y,18,y+1,buf1);
textbackground(BLACK);
textcolor(WHITE);
gotoxy(2,y);
cprintf("%s",f[y-2]);
gotoxy(2,y);
}
else
if (key==Key_ENTER)
{
switch ( y-1 ){
case 1:
break;
case 2:
break;
case 3:
break;
case 4:
break;
case 5:
exit(0);
default:
break;
}
break;
}
else
if (key==Key_ESC)
break;
}
if (key==Key_ALT_X) exit(0);
break;
}
}
}
}
void box(int startx,int starty,int high,int width)
{ int i;
gotoxy(startx,starty);
putch(0xda);
for (i=startx+1;i<width;i++) putch(0xc4);
putch(0xbf);
for( i=starty+1;i<high;i++)
{
gotoxy(startx,i);putch(0xb3);
gotoxy(width,i);putch(0xb3);
}
gotoxy(startx,high);
putch(0xc0);
for (i=startx+1;i<width;i++) putch(0xc4);
putch(0xd9);
return ;
}
用c语言制作一个小菜单
#include
#include
#include
#include
using namespace std;
void xuanzepaixu() //选择排序函数
{
printf("待添加,请按任意键继续...");
getchar();
}
void erfenpaixu() //二分查找函数
{
printf("待添加,请按任意键继续...");
getchar();
}
void menu() //主菜单
{
system("cls"); //清屏
printf("\n\t\t\t 菜单\n");
printf("\t\t\t#***********************#\n");
printf("\t\t\t# 1选择排序 #\n");
printf("\t\t\t# 2二分查找 #\n");
printf("\t\t\t# 3返回 #\n");
printf("\t\t\t# 4---关闭 #\n");
printf("\t\t\t#***********************#\n");
printf("\t\t\t 请选择(1-4) =:");
}
void main()
{
char select;
while(1)
{
menu();
system("COLOR 9f");
scanf("%c",&select);
if(select=='3')
break; //返回上级菜单
else
{
getchar(); //读入回车符
if(!isdigit(select)) //如果不是数字字符
{
printf("\n\7Your select may be wrong, must enter the digit!\n");_getch();
}
else
{
switch (select)
{
case '1': xuanzepaixu();break;
case '2': erfenpaixu();break;
case '4':exit(0); //直接退出
default:
{
printf("\n\7\7Your selected digit may be wrong, select again!\n");
_getch();
break;
}
}
}
}
};
}
怎样用C语言编写菜单
对于窗口组件菜单,需要根据不同平台,通过图形编程接口,进行菜单的编制。例程:#include #include #include void main() { char str; int i,k,choice=1; int gd=DETECT,gm; initgraph(&gd,&gm," "); setbkcolor(2); settextstyle(3,0,3); outtextxy(140,120,"A. The Mock Clock."); outtextxy(140,150,"B. The Digital Clock."); outtextxy(140,180,"C. Exit."); setlinestyle(0,0,3); rectangle(170,115,370,145); /*按上下键选择所需选项*/ for(i=1;i=100)exit(0);/*如果按键超过100次退出*/ switch(choice)/*这里引用函数,实现所要的功能*/ { case 1: cleardevice(); setbkcolor(4); settextstyle(3,0,4); outtextxy(160,120,"No.1 have not built."); break; case 2: cleardevice(); setbkcolor(4); settextstyle(3,0,4); outtextxy(160,150,"No.2 have not built."); break; case 3: exit(0); } getch(); closegraph(); }对于命令行菜单,直接通过不断刷新输出来模拟菜单行为。例程:#include #include #include int n,t,k;int m;char s1[20],s2[20],c;char **l;char *num[]={"one","two","three","four","five","six","seven","eight","nine","ten"};void menu(){ printf("\n\n\t\t*******************************************************\n"); printf("\t\t** 1.查找字符串S1中S2出现的次数 **\n"); printf("\t\t** 2.统计字符串中大小写字母,数字出现的次数 **\n"); printf("\t\t** 3.将数字翻译成英语 **\n"); printf("\t\t** 4.结束 **\n"); printf("\t\t*******************************************************\n"); printf("\t\t 您的输入:"); fflush(stdin); scanf("%d",&n);}void check(){ char a[20],b[20]; int j=0,k,m,l=0; int t=0,n=0; printf("请输入主字符串:\n"); scanf("%s",a); k=strlen(a); printf("请输入子字符串:\n"); scanf("%s",b); m=strlen(b); for(n=0;n='a'&&c='A'&&c='0'&&c<='9') k++; } printf("有大写字母:\n%d\n",t); printf("有小写字母:\n%d\n",n); printf("有数字:\n%d\n",k);}void number(){ l=num; printf("请输入一个数字:(0-10)\n"); fflush(stdin); scanf("%d",&m); printf("%d对应的英文是:\n%s\n",m,*(l+m-1));}void main(){ while(1) { system("cls"); menu(); switch(n) { case 1:system("cls");check();system("pause");break; case 2:system("cls");cout();system("pause");break; case 3:system("cls");number();system("pause");break; case 4:system("cls");break; default:system("cls");break; } if(n==4) break; } printf("感谢使用\n");}