c++ 菜单动态效果

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <stdarg.h> 
#include <time.h>
#include "hello.h"
#include <share.h>
#include <Windows.h>
//#define  var 333;
//移动光标位置
void gotoxy(int x,int y)
{
    COORD xy;
    HANDLE hstdout;
    hstdout=GetStdHandle(STD_OUTPUT_HANDLE);
    xy.X=x;
    xy.Y=y;
    SetConsoleCursorPosition(hstdout,xy);
    //CloseHandle(hstdout);
}
void fillchar(char a,int charlen)
{
    COORD xy={0};
    HANDLE hstdout;
    long byw;
    
    hstdout=GetStdHandle(STD_OUTPUT_HANDLE);
    FillConsoleOutputCharacter(hstdout,a,charlen,xy,&byw);
    CloseHandle(hstdout);
}
#define  X 20
#define  Y 6
//绘制边框
void drawM(void)
{
    int i;
    //上边
    gotoxy(X,Y);
    printf("");
     for (i=1;i<=13;i++)
     {
         printf("");
     }
    printf("");
    //左边
    for (i=1;i<=16;i++)
    {
      gotoxy(X,Y+i);
      printf("");
    }
    //右边
    for (i=1;i<=16;i++)
    {
        gotoxy(X+28,Y+i);
        printf("");
    }
    //下边
    gotoxy(X,Y+17);
    printf("");
    for (i=1;i<=13;i++)
    {
         printf("");
    }
    printf("");
    return ;
    
}
//drawmenu
void drawmenu(void)
{
    gotoxy(X+3,Y+3);
    printf("1->菜单选项一");
    gotoxy(X+3,Y+3+2);
    printf("   菜单选项二");
     gotoxy(X+3,Y+3+2+2);
    printf("   退出");
    gotoxy(0,0);
}
int selmenu_Num=0;
int selmenu(void)
{ //0,1,2
  //如果按下↓selmenu_Num++; 如果>2  selmenu_Num=0;
  //如果按下↑selmenu_Num--; 如果<0  selmenu_Num=2;
    int ch=0;
    while(1)
    {
        ch=getch();
        //gotoxy(0,0);
        //printf("%d ",ch);
        if (ch==13)
        {
            return selmenu_Num;
        }
        switch(ch)
        {
        case 72://上光标
            selmenu_Num--;
            if (selmenu_Num<0)
            {
                selmenu_Num=2;
            }
            break;
        case 80: //下光标
            selmenu_Num++;
            if (selmenu_Num>2)
            {
                selmenu_Num=0;
            }
            break;
        }
     gotoxy(0,0);
    // printf("selmenu_num=%d",selmenu_Num);
       //动态菜单
       switch(selmenu_Num)
       {
       case 0:
           gotoxy(X+3,Y+3);
           printf("1->");
           gotoxy(X+3,Y+3+2);
           printf("   ");
           gotoxy(X+3,Y+3+2+2);
           printf("   ");
           gotoxy(0,0);
       break;
       case 1:
           gotoxy(X+3,Y+3);
           printf("   ");
           gotoxy(X+3,Y+3+2);
           printf("2->");
           gotoxy(X+3,Y+3+2+2);
           printf("   ");
           gotoxy(0,0);
       break;
       case 2:
           gotoxy(X+3,Y+3);
           printf("   ");
           gotoxy(X+3,Y+3+2);
           printf("   ");
           gotoxy(X+3,Y+3+2+2);
           printf("3->");
           gotoxy(0,0);
       break;
       }
    } //end while
     
    return selmenu_Num;
}
//doMenu
void doMenu(void)
{ int sel;
   sel=    selmenu();
   switch(sel)
   {
   case 0: //菜单一
        gotoxy(0,0);
        printf("\n菜单一功能\n");
       break;
   case 1:       
       gotoxy(0,0);
       printf("\n菜单二功能\n");
       break;
   case 2: //退出
       exit(1);
       break;
   }
}

int main(void)
{  //Y行 X列
  // fillchar(' ',3000);
    system("color 0a");
    system("title QQ150330575");
    drawM();
    drawmenu();
    doMenu();
    getchar();
    getchar();
    return 0;
}

 

posted @ 2016-12-12 17:07  whzym111  阅读(554)  评论(0编辑  收藏  举报