在这里贴给你吧,看思路(留意如何写出方便调用者使用的界面,菜单项用填表方式很容易修改维护扩充,扩充部分如增加函数指针则可避用switch,而且菜单函数是可以反复使用的),不要死抄代码。
这里是使用点:
程序代码:
头文件my_tools.h:
程序代码:
头文件接口的实现部分my_tools.cpp:
程序代码:
这里是使用点:
程序代码:
VOID wmain(VOID)
{
screen.SetTitle(L"学生管理系统");
screen.SetTextAttribute(FOREGROUND_WHITE | BACKGROUND_BLUE);
screen.Cls();
const MENU main_menu[] = {
{L'1', L"1.录入学生信息"},
{L'2', L"2.添加学生信息"},
{L'3', L"3.删除学生信息"},
{L'4', L"4.显示学生信息"},
{L'0', L"0.结束程序返回"},
{L'\0'}
};
int choice;
do
{
choice = Menu(main_menu, 2, 2, FOREGROUND_WHITE | BACKGROUND_GREEN, FOREGROUND_YELLOW | BACKGROUND_RED);
switch (choice)
{
case 1:
Input_Data();
break;
}
} while (choice != 0);
}
头文件my_tools.h:
程序代码:
#ifndef MYTOOLS_H
#define MYTOOLS_H
#include <string>
// 统计宽字符串中字符数
size_t CountByAnsi(const WCHAR* string);
// 暂停等待,按Esc键继续
VOID Pause(VOID);
// 检测是否ASCII字符
BOOL IsAscii(const WCHAR character);
// 检测是否汉字
BOOL IsHz(const WCHAR character);
// 菜单
typedef struct _MENU
{
WCHAR Key;
std::wstring Bar;
} MENU;
int Menu(const MENU menu[], const SHORT x, const SHORT y, const WORD text_attr, const WORD get_attr);
#endif
头文件接口的实现部分my_tools.cpp:
程序代码:
int Menu(const MENU menu[], const SHORT x, const SHORT y, const WORD text_attr, const WORD get_attr)
{
size_t bar_width(0);
size_t bar_number(0);
while (menu[bar_number].Key)
{
size_t number(CountByAnsi(menu[bar_number].Bar.c_str()));
if ( number > bar_width)
{
bar_width = number;
}
++bar_number;
}
WORD old_attr(screen.GetTextAttribute());
screen.SetTextAttribute(text_attr).Box(x, y, x + bar_width + 1, y + bar_number + 3, text_attr);
for (size_t index = 0; index != bar_number; ++index)
{
screen.MoveCursor(x + 2, y + index + 1).WriteText(menu[index].Bar);
}
screen.MoveCursor(x + 2, y + bar_number + 2).WriteText(L"选择:");
screen.SetTextAttribute(get_attr);
COORD pos(screen.GetCursorPosition());
WCHAR choice;
BOOL finish(false);
while (!finish)
{
screen.MoveCursor(pos).WriteText(L' ');
choice = screen.MoveCursor(pos).ReadKey();
for (size_t index = 0; index != bar_number; ++index)
{
if (choice == menu[index].Key)
{
screen.MoveCursor(pos).WriteText(choice);
finish = true;
break;
}
}
}
screen.SetTextAttribute(old_attr);
return choice - L'0';
}

授人以渔,不授人以鱼。





