C程序设计实例一百六十三:彩色弹出菜单

- 编辑:去找发布 - 阅读:- 发布时间:

C程序设计实例一百六十三:彩色弹出菜单

C程序设计实例一百六十三:彩色弹出菜单

#include <stdio.h>
#include <dos.h>
#include <stdlib.h>
#include <bios.h>
#include <conio.h>
#include <string.h>
#include <ctype.h>
 
#define BORDER  1
#define ESC 27
#define REV_VID 0x1e
#define NORM_VID 0x40
 
char *main_menu[] =
{
         "F --- 文件管理",
         "D --- 磁盘管理",
         "H --- 打印服务",
         "X --- 退出菜单",
};
 
char* sub_menu1[] =
{
         "S --- 文件显示",
         "P --- 文件打印", 欢迎访问www.7zhao.net
         "C --- 文件拷贝",
         "D --- 文件删除",
         "X --- 返回上级",
};
 
char* sub_menu2[] =
{
         "B --- BOOT区备份",
         "R --- BOOT区恢复",
         "X --- 返回上级",
};
 
char* sub_menu3[] =
{
         "O --- 打印图形",
         "F --- 打印文本",
         "X --- 返回上级",
};
 
void goto_xy(int x, int y) /*将光标移动到指定位置*/
{
         union REGS regs; www.7zhao.net
         regs.h.ah = 2;
         regs.h.dl = x;
         regs.h.dh = y;
         regs.h.bh = 0;
         int86( 0x10, &regs, &regs );
}
 
void save_video( int startx, int endx, int starty, int endy, unsigned int* buf_ptr )
/*将指定区域的视频模式存储到指定的内存中去*/
{
         union REGS r;
         register int i, j;
         for( i = starty; i < endy; i++)
                   for( j = startx; j < endx; j++ )
                   {

欢迎访问www.7zhao.net

                            goto_xy(j, i);
                            r.h.ah = 8;
                            r.h.bh = 0;
                            *buf_ptr++ = int86( 0x10, &r, &r );
                            putchar(' ');
                   } 欢迎访问www.7zhao.net
}
 
void chineseputs( int x, int y, char *p, int attrib ) /*在指定的位置显示中文字符*/
{
         union REGS r;
         while(*p)
         {
                   r.h.ah = 2;
                   r.h.dl = x++;
                   r.h.dh = y;
                   int86(0x10, &r, &r);
                   r.h.ah = 9;
                   r.h.bh = 0;

copyright www.7zhao.net


                   r.x.cx = 1;
                   r.h.al = *p++;
                   r.h.bl = attrib;
                   int86( 0x10, &r, &r );
                  
         }
}
 
void disp_box( int x1, int y1, int x2, int y2, int attrib) /*显示边框*/
{
         unsigned char* boxchar[6] = {"┏","━","┓","┃","┗","┛"};
         register int i; copyright www.7zhao.net
         for( i = x1; i <= x2; i++ )
         {
                   chineseputs( i, y1, boxchar[1], attrib );
                   chineseputs( i, y2, boxchar[1], attrib );
         }
         for( i = y1; i <= y2; i++ )
         {
                   chineseputs( x1, i, boxchar[3], attrib );
                   chineseputs( x2, i, boxchar[3], attrib );
         } 去找(www.7zhao.net欢迎您
         chineseputs( x1, y1, boxchar[0], attrib );
         chineseputs( x2, y1, boxchar[2], attrib );
         chineseputs( x2, y2, boxchar[5], attrib );
         chineseputs( x1, y2, boxchar[4], attrib );
}
 
void display_menu( char* menu[], int x, int y, int count ) /*显示菜单项*/
{
         register int i;
         for( i = 0; i < count; i++, x++)
         {
                   goto_xy( x, y );
                   cprintf( menu[i]);
         }

去找(www.7zhao.net欢迎您


}
 
int is_in( char* s, char c) /*返回字符串中热键的位置*/
{
         register int i;
         for( i = 0; *s; i++ )
                   if( *s++ == c ) return i + 1;
         return 0;
}
 
int get_resp( int x, int y, int count, char* menu[], char* keys ) /*获得用户的响应*/
{
         union inkey
         {
                   char ch[2];
                   int i ;
         } c ; copyright www.7zhao.net
         int arrow_choice = 0, key_choice;
         y++;
         goto_xy(x, y);
         chineseputs( y, x, menu[0], REV_VID);
         for(;;)
         {
                   while(!bioskey(1));
                   c.i = bioskey(0);
                   goto_xy( x + arrow_choice, y);
                   chineseputs( y, x+arrow_choice, menu[arrow_choice], NORM_VID ); 本文来自去找www.7zhao.net
                   if( c.ch[0] )
                   {
                            key_choice = is_in( keys, tolower( c.ch[0] ));
                            if( key_choice ) return key_choice - 1;
                            switch( c.ch[0] )
                            {
copyright www.7zhao.net

                            case '\r': return arrow_choice;
                            case ' ':  arrow_choice++;
                                        break;
                            case ESC : return -1;
                            } 欢迎访问www.7zhao.net
                   }
                   else
                   {
                            switch( c.ch[1] )
                            {
                            case 72: arrow_choice--;
                                      break;

去找(www.7zhao.net欢迎您


                            case 80: arrow_choice++;
                                      break;
                            }
                   }
                   if( arrow_choice == count ) arrow_choice = 0;
                   if( arrow_choice < 0 ) arrow_choice = count - 1; 去找(www.7zhao.net欢迎您
                   goto_xy( x + arrow_choice, y );
                   chineseputs( y, x+arrow_choice, menu[ arrow_choice ], REV_VID );
         }
}
 
void restore_video( int startx, int endx, int starty, int endy, unsigned char* buf_ptr )
/*恢复原有的视频方式*/
{
         union REGS r;
         register int i, j;
         for( j = startx; j < endx; j++)
                   for( j = starty; j < endy; j++ )
                   {
copyright www.7zhao.net

                            goto_xy(i, j);
                            r.h.ah = 9;
                            r.h.bh = 0;
                            r.x.cx = 1;
                            r.h.al = *buf_ptr++;
                            r.h.bl = *buf_ptr++;
www.7zhao.net

                            int86( 0x10, &r, &r );
                   }
}
 
int popup( char* menu[], char* keys, int count, int x, int y, int border )
/*弹出式菜单的显示*/
{
         register int i, len;
         int endx, endy, choice;
         unsigned int *p;
         if((x>24)||(x<0)||(y>79)||(y<0))
         {
                   printf("范围错");
                   return -2; 内容来自www.7zhao.net
         }
         len = 0;
         for( i = 0; i < count; i++ )
                   if( strlen( menu[i]) > len ) len = strlen( menu[i] );
         endy = len + 2 + y;
         endx = count + 1 + x;
         if((endx+1>24)||(endy+1>79))
         {
                   printf("菜单不匹配");
                   return -2;
         } copyright www.7zhao.net
         p = (unsigned int *) malloc((endx-x+1)*(endy-y+1));
         if( !p ) exit(1);
         save_video( x, endx + 1, y, endy+1, p );
         if( border ) disp_box( y, x, endy, endx, YELLOW );
         display_menu( menu, x + 1, y + 1, count );
         choice = get_resp( x + 1, y, count, menu, keys );
         restore_video( x, endx + 1, y, endy + 2, (unsigned char* ) p);
         free( p );
         return choice;
}
 
void main()
{
         int i, j;
         directvideo = 0; www.7zhao.net
         for(;;)
         {
                   clrscr();
                   goto_xy(0, 0);
                   textcolor( BLACK  );
                   textbackground( RED );
                   switch( popup( main_menu, "fdhx", 4,5, 16, BORDER ))
                   {
                   case 0:
内容来自www.7zhao.net

                            clrscr();
                            switch( popup( sub_menu1, "spcdx", 5, 5, 16, BORDER ))
                            {
                            case 0:
                                     clrscr();
                                     goto_xy( 24, 20 );

欢迎访问www.7zhao.net


                                     cprintf("你选择了文件显示功能");
                                     getch();
                                     break;
                            case 1:
                                     clrscr();

www.7zhao.net


                                     goto_xy( 24, 20 );
                                     cprintf("你选择了文件打印功能");
                                     getch();
                                     break;
                            case 2:

去找(www.7zhao.net欢迎您


                                     clrscr();
                                     goto_xy( 24, 20 );
                                     cprintf("你选择了文件拷贝功能");
                                     getch();
                                     break; 去找(www.7zhao.net欢迎您
                            case 3:
                                     clrscr();
                                     goto_xy(24,20);
                                     cprintf("你选择了文件删除功能");
                                     getch();
欢迎访问www.7zhao.net

                                     break;
                            case 4:
                                     break;
                            default:
                                     break; 内容来自www.7zhao.net
                            }
                            break;
                   case 1:
                            clrscr();
                            switch( popup( sub_menu2, "brx", 3, 5, 16, BORDER ))
                            {
内容来自www.7zhao.net

                            case 0:
                                     clrscr();
                                     goto_xy(24,20);
                                     cprintf("你选择了BOOT区备份功能");
                                     getch();
本文来自去找www.7zhao.net

                                     break;
                            case 1:
                                     clrscr();
                                     goto_xy(24,20);
                                     cprintf("你选择了BOOT区恢复功能"); 内容来自www.7zhao.net
                                     getch();
                                     break;
                            case 2:
                                     break;
                            default:
本文来自去找www.7zhao.net

                                     break;
                            }
                            break;
                   case 2:
                            clrscr();
                            switch( popup( sub_menu3, "of", 3, 5, 16, BORDER )) 去找(www.7zhao.net欢迎您
                            {
                            case 0:
                                     clrscr();
                                     goto_xy( 24, 20 );
                                     cprintf("你选择了打印图形功能"); 本文来自去找www.7zhao.net
                                     getch();
                                     break;
                            case 1:
                                     clrscr();
                                     goto_xy(24,20);
去找(www.7zhao.net欢迎您

                                     cprintf("你选择了打印文本功能");
                                     getch();
                                     break;
                            case 2:
                                     break; 本文来自去找www.7zhao.net
                            default:
                                     break;
                            }
                            break;
                   case 3:
                   case -1:
                            return 0;

www.7zhao.net


                   default:
                            break;
                   }
         }
}