| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2564 人关注过本帖
标题:C控制台菜单
只看楼主 加入收藏
永夜的极光
Rank: 6Rank: 6
等 级:贵宾
威 望:27
帖 子:2721
专家分:1
注 册:2007-10-9
收藏
 问题点数:0 回复次数:7 
C控制台菜单
前几天看某个作业贴,有个要求是菜单必须能用光标选择,一时无事,就把这个功能给做了,算是给各位写作业的同学锦上添花吧

a.h
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>

#define DefaultWidth 50
typedef void (*MenuFun)(void);
struct _Menu;
typedef struct _Selection
{
  char MenuInfor[100];
  MenuFun fun;
  _Menu* SubMenu;
}Selection, *PSelection;
typedef struct _Menu
{
  PSelection SelectionArray;
  int nSelectionCount;
}Menu, *PMenu;

void PrintSame(char c, int n)
{
  int i;
  for (i = 0; i < n; i++)
  {
    printf("%c", c);
  }
}

void ShowMenu(PMenu m,int select, int width)
{
  int i, l = m->nSelectionCount;
  width = width < DefaultWidth ? DefaultWidth : width;
  system("cls");
  PrintSame('*', width);
  printf("\n");
  for (i = 0; i < l; i++)
  {
    if (i == select)
      printf("**  ==> ");
    else
      printf("**      ");
    printf("%-*s**\n", width - 10, m->SelectionArray[i].MenuInfor);
  }
  PrintSame('*', width);
  printf("\n");
  printf("数字键:直接选择  方向键:移动光标  回车:确定  ESC:取消\n");
}

void DynamicMenu(PMenu m, int width)
{
  char c = 0;
  int select = 0, l = m->nSelectionCount;
  while (c != 27)
  {
    ShowMenu(m, select, width);
    c = getch();
    if (c == -32)
    {
      c = getch();
      if (c == 72 && select > 0)
        select--;
      else if (c == 80 && select < l - 1)
        select++;
    }
    else if (c == 13)
      if (m->SelectionArray[select].SubMenu == NULL)
        m->SelectionArray[select].fun();
      else
        DynamicMenu(m->SelectionArray[select].SubMenu, width);
    else if (c >= 1 + '0' && c <= m->nSelectionCount + '0')  
      if (m->SelectionArray[c - '1'].SubMenu == NULL)
        m->SelectionArray[c - '1'].fun();
      else
        DynamicMenu(m->SelectionArray[c - '1'].SubMenu, width);
  }
}


下面的a.cpp是演示如何使用
#include "a.h"

#define p(x)\
  printf("%s 被调用\n按任意键继续..", x);\
  getch()

void f1()
{
  p("f1");
}

void f2()
{
  p("f2");
}

void f3()
{
  p("f3");
}

void f4()
{
  p("f4");
}

int main()
{
  char c = 0;
  Selection s1[4] = {"1.子菜单11", f1, NULL, "2.子菜单12", f2, NULL, "3.子菜单13", f3, NULL, "4.子菜单14", f4, NULL};
  Selection s3[2] = {"1.子菜单31", f1, NULL, "2.子菜单32", f2, NULL};
  Menu m1 = {s1, 4};
  Menu m3 = {s3, 2};
  Selection s[3] = {"1.菜单1..", f1, &m1, "2.菜单2", f2, NULL, "3.菜单3..", f3, &m3};
  Menu m = {s, 3};
  DynamicMenu(&m, 60);
  return 0;
}


以上程序WinXP(SP2)+VC6测试通过

顺便为了照顾不得不使用TC的同学,特别修改了一个TC版,TC2测试通过

附件中包含VC和TC两种版本

C控制台菜单.rar (9.61 KB)
搜索更多相关主题的帖子: 控制台 菜单 
2008-06-26 11:16
永夜的极光
Rank: 6Rank: 6
等 级:贵宾
威 望:27
帖 子:2721
专家分:1
注 册:2007-10-9
收藏
得分:0 
没人提点意见建议?

从BFS(Breadth First Study)到DFS(Depth First Study)
2008-06-26 17:38
StarWing83
Rank: 8Rank: 8
来 自:仙女座大星云
等 级:贵宾
威 望:19
帖 子:3951
专家分:748
注 册:2007-11-16
收藏
得分:0 
为什么不做个彩色的呢,嘿嘿~~~

专心编程………
飞燕算法初级群:3996098
我的Blog
2008-06-26 18:33
永夜的极光
Rank: 6Rank: 6
等 级:贵宾
威 望:27
帖 子:2721
专家分:1
注 册:2007-10-9
收藏
得分:0 
/*****************************************************************
** HighlightCodeV3.1 software by yzfy(雨中飞燕) http:// **
*****************************************************************/
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>

#define DefaultWidth 50
typedef void (*MenuFun)(void);
struct _Menu;
typedef struct _Selection
{
   
char MenuInfor[100];
    MenuFun fun;
    _Menu* SubMenu;
}Selection, *PSelection;
typedef struct _Menu
{
   
PSelection SelectionArray;
    int nSelectionCount;
}Menu, *PMenu;

void PrintSame(char c, int n)
{
   
int i;
    for (i = 0; i < n; i++)
    {
   
printf("%c", c);
    }
}

void ShowMenu(PMenu m,int select, int width)
{
   
int i, l = m->nSelectionCount;
    width = width < DefaultWidth ? DefaultWidth : width;
    system("cls");
    PrintSame('*', width);
    printf("\n");
    for (i = 0; i < l; i++)
    {
   
if (i == select)
        printf("**  ==> ");
    else
        
printf("**      ");
    printf("%-*s**\n", width - 10, m->SelectionArray[i].MenuInfor);
    }
   
PrintSame('*', width);
    printf("\n");
    printf("数字键:直接选择  方向键:移动光标  回车:确定  ESC:取消\n");
}

void DynamicMenu(PMenu m, int width)
{
   
char c = 0;
    int select = 0, l = m->nSelectionCount;
    while (c != 27)
    {
   
ShowMenu(m, select, width);
    c = getch();
    if (c == -32)
    {
        
c = getch();
        if (c == 72 && select > 0)
        select--;
        else if (c == 80 && select < l - 1)
        select++;
    }
   
else if (c == 13)
        if (m->SelectionArray[select].SubMenu == NULL)
        m->SelectionArray[select].fun();
        else
        
DynamicMenu(m->SelectionArray[select].SubMenu, width);
    else if (c >= 1 + '0' && c <= m->nSelectionCount + '0')  
        if (m->SelectionArray[c - '1'].SubMenu == NULL)
        m->SelectionArray[c - '1'].fun();
        else
        
DynamicMenu(m->SelectionArray[c - '1'].SubMenu, width);
    }
}

我试试看,不过好像有不少可以省略的标签哦,难道我下的这个不是最新版的?

从BFS(Breadth First Study)到DFS(Depth First Study)
2008-06-27 13:05
zhuwei168
Rank: 1
来 自:东软信息学院
等 级:新手上路
帖 子:180
专家分:0
注 册:2008-2-13
收藏
得分:0 
楼主
下了
学习~~
大概的看了一下
果然我是菜鸟
水平就是不够
要写出这个水平还得有一定时间

做一个自由的人,飞到蔚蓝的天空里。
2008-06-28 22:30
yzt7805882
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2008-7-13
收藏
得分:0 
ddddd
2008-07-13 22:23
junjin456963
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2011-4-9
收藏
得分:0 
哇哇哇哇。我在网上整整找了20个小时的控制台界面设计。网上的好多都TC的。我郁闷死了。当我注册完下载,这正是我要找的东西。谢谢楼主分享。百度我都搜的都累坏了、赶紧研究下。谢谢谢谢,再谢谢。
2011-04-09 13:31
矢戮
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2012-6-7
收藏
得分:0 
我找了2天,终于找到我想要的,谢谢你们
2012-10-16 20:35
快速回复:C控制台菜单
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.018369 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved