用C语言实现仿TC界面

#include <stdio.h>
#include <conio.h>
#include <graphics.h>
#define upkey  0x4800
#define downkey 0x5000
#define leftkey 0x4b00
#define rightkey 0x4d00
#define f10 0x4400
#define esckey 0x011b
#define enterkey 0x1c0d
#define NULL 0
void mainmenudraw();
void submenudraw();
void drawfram();
void setunactive();
void setactive(int ,int );
void change(int ,int );
int selectmenu();
void drawline();
int func(int );
 
typedef struct menu{
   int id;
   int x;
   int y;
   int active;
   char content[15];
   struct menu *psubmenu;
   int numsub;
 }MENU;
 
MENU system[2]={
    {11,1,1,0,"clean",NULL,0},
    {12,1,2,0,"exit",NULL,0}
               };
 
MENU set[3]={
    {21,26,1,0,"bond",NULL,0},
    {22,26,2,0,"color",NULL,0},
    {23,26,3,0,"recover",NULL,0}
            };
            
            
MENU draw[4]={
    {31,51,1,0,"arc",NULL,0},
    {32,51,2,0,"bar",NULL,0},
    {33,51,3,0,"line",NULL,0},
    {34,51,4,0,"circle",NULL,0}
             };
   
MENU mainmenu[3]={
   {1,1,1,0,"system",system,2},
   {2,26,1,0,"set",set,3},
   {3,51,1,0,"draw",draw,4},
                 };
                 
         
char colorarry[4][10]={
   "WHITE","BLUE","RED","YELLOW"
                      };
    
                      
                               
char buff[4096];
int actx,acty,linex,bondnum,colornum;
 
 
void main()
{
  char ch;
  int i,j;
  linex=1;
  
  
  bondnum=1;
  colornum=0;
  drawfram();
  i=0;
  
  
  while(!i)
  {
    switch(bioskey(0))
      {
         case f10:
                 j=selectmenu();
                 i=func(j);
                 break;
         case esckey:
                 i=1;
                 break;          
      }
  }
}
 

void mainmenudraw(MENU *pmenu,int n)
{
   int i;
   for(;n>0;n--)
   {
   textcolor(pmenu->active?YELLOW:WHITE);
   gotoxy(pmenu->x,pmenu->y);
   cprintf("%s",pmenu->content);
   pmenu++;
   }
}

void drawfram()
{
  textmode(C80);
  window(1,1,80,25);
  textbackground(RED);
  clrscr();
  mainmenudraw(mainmenu,3);
  textcolor(YELLOW);
  gotoxy(1,25);
  cprintf("menu-F10");
  gotoxy(12,25);
  cprintf("quit-Esc");
  gotoxy(24,25);
  cprintf("left-<--");
  gotoxy(36,25);
  cprintf("right--->");
  gotoxy(48,25);
  cprintf("left-^1");
  gotoxy(60,25);
  cprintf("left-V1");
  window(1,2,80,24);
  textbackground(WHITE);
  clrscr();
}
int selectmenu()
{
  int i,j;
  j=1;
  actx=0;
  acty=0;
  setactive(actx,acty);
  gettext(1,2,80,25,buff);
  drawfram();
  puttext(1,2,80,25,buff);
  submenudraw(mainmenu[0].psubmenu,mainmenu[0].numsub,1);
  
  while(j)
  {
    switch(bioskey(0))
    {
      case esckey:
                  setunactive();
                  drawfram();
                  puttext(1,2,80,25,buff);
                  j=0;
                  break;
                  
      case upkey:
                 setunactive();
                 change(0,-1);
                 setactive(actx,acty);
                 drawfram();
                 puttext(1,2,80,25,buff);
                 submenudraw(mainmenu[actx].psubmenu,mainmenu[actx].numsub,1+actx*25);
                 break;
      
      case  leftkey:
                 setunactive();
                 change(-1,0);  
                 setactive(actx,acty);
                 drawfram();
                 puttext(1,2,80,25,buff); 
                 submenudraw(mainmenu[actx].psubmenu,mainmenu[actx].numsub,1+actx*25);
                 break;
      
      case   downkey:
                 setunactive();
                 change(0,1);  
                 setactive(actx,acty);
                 drawfram();
                 puttext(1,2,80,25,buff); 
                 submenudraw(mainmenu[actx].psubmenu,mainmenu[actx].numsub,1+actx*25);
                 break;           
                 
      case   rightkey:
                 setunactive();
                 change(1,0);
                 setactive(actx,acty);
                 drawfram();
                 puttext(1,2,80,25,buff); 
                 submenudraw(mainmenu[actx].psubmenu,mainmenu[actx].numsub,1+actx*25);
                 break;                          
    
      case   enterkey:
                 i=actx*10+acty;
                 setunactive();
                 drawfram();
                 puttext(1,2,80,25,buff);
                 j=0;
                 break;
      }
  }
  return i;
}
int func(int i)
{
  int j,a1,a2,a3,a4,a5;
  j=0;
  textcolor(BLACK);
  switch(i)
  {
    case 0:
           drawfram();
           linex=1;
           break;
    case 1:
           j=1;
           break;
    case 10:
            gotoxy(1,linex);
            cprintf("Please input your bond number:");
            linex=linex+1;
            break;
    case 11:
            gotoxy(1,linex);
            cprintf("Plese input your color chioce (WHITE(0),BULE(1),RED(2),YELLOW(3)):");
            linex=linex+1;
            break;
    case 12:
            gotoxy(1,linex);
            cprintf("you have recoved the set(bond=1,color=WHITE).");
            linex=linex+1;
            break;
    case 20:
            gotoxy(1,linex);
            cprintf("arc(midx,midy,stangle,endangle,radius):");
            linex=linex+1;
            break;
    case 21:
            gotoxy(1,linex);
            cprintf("bar(left,top,right,bottom):");
            scanf("%d",&a1);  
            scanf("%d",&a2);
            scanf("%d",&a3);
            scanf("%d",&a4);
            drawline();
            bar(a1,a2,a3,a4);
            getch();
            closegraph();
            linex=linex+1;
            break;      
    case 22:
            gotoxy(1,linex);
            cprintf("line(x1,y1,x2,y2):");
            scanf("%d",&a1);  
            scanf("%d",&a2);
            scanf("%d",&a3);
            scanf("%d",&a4);
            drawline();
            line(a1,a2,a3,a4);
            getch();
            
            linex=linex+1;
            break;
    case 23:
            gotoxy(1,linex);
            cprintf("circle (midx,midy,radius):");
            drawline();
            circle(100,100,53);  
            getch();
            linex=linex+1;
            break;
  }
   
  return j;
}
void submenudraw(MENU *pmenu,int n,int m)
{
  int i;
  window(m,2,15,2+n);
  textbackground(RED);
  for(;n>0;n--)
  {
     textcolor(pmenu->active?YELLOW:WHITE);
     gotoxy(pmenu->x,pmenu->y);
     cprintf("%s",pmenu->content);
     pmenu++;
  }
}
void setunactive()
{
  mainmenu[actx].active=0;
  mainmenu[actx].psubmenu[acty].active=0;
}
void setactive(int i,int j)
{
  mainmenu[i].active=1;
  mainmenu[i].psubmenu[j].active=1;
}
void change(int i,int j)
{
  if(i==0)
  {
    acty=acty+j;
    if(acty<0)
        acty=mainmenu[actx].numsub-1;
    if(acty>=(mainmenu[actx].numsub))
        acty=0;
   }
    
   else
   {
     actx=actx+i;
     if(acty<0)
         actx=2;
     if(actx==3)
         actx=0;
     if(acty>=(mainmenu[actx].numsub))
          acty=0;   
   }
}

void drawline()
{
  int gdriver=DETECT,gmode,errorcode;
  initgraph(&gdriver,&gmode,"");
  errorcode=graphresult();
  if(errorcode!=grOk)
  {
    printf("Graphics error:%s",grapherrormsg(errorcode));
    printf("Press anykey to halt:");
    getch();
    exit(1);
  }
}


你可能感兴趣的:(c,struct,null,System,语言,menu)