用C编写类Windows菜单
副标题#e#
在开拓措施时,措施的界面、开放性是两个很是重要的方面,今朝,在Unix系统下运行的措施的界面多半较量古板,并且,在举办成果扩充时也不是很利便。那么,能不能设计一个象Windows那样可以或许凭据用户要求随意调解界面,成果扩充利便的措施呢?谜底是必定的。笔者通过实践,设计了一个菜单措施,利用户在对菜单的显式样式不满足时,只需通过对菜单参数文件举办操纵即可完成对菜单元置、宽度、长度、是否有边框等举办调解;在用户需要举办成果扩充时,也无须窜改源措施,只须对参数文件举办操纵就可将新增成果挂到菜单上。
一参数文件说明
本措施需要借肋两个参数文件来实现:
(1)、对菜单中每一项参数举办说明的文件(menu.def),它名目如下所述:
!所属菜单代号!项顺序号!菜单项名称!外挂措施名称!下级菜单代号!
说明:
1、如菜单代号为"0",则暗示此项属于主菜单
2、如外挂措施名称为"0",则暗示此菜单项对应的进程在菜单措施内部或对应于一个子菜单;
3、如下级菜单代号为"0",则暗示此菜单项无下级子菜单;
4、项顺序号同时可作为菜单热键利用。
如果文件menu.def中有下面这一行:
!0!3!名目化磁盘!format /dev/rfd0135ds18!0!
它暗示主菜单的第三项为名目化磁盘,它对应的执行进程为 format /dev/rfd0135ds18,本项无子菜单。
假如用户想把本身编的实现查询成果措施XXX挂到本措施主菜单第4项上,则可在menu.def中增加下面这一行:
!0!4!查询!XXX!0!
(2)、对各菜单参数举办说明文件(menu.conf),其名目如下所述:
!菜单代号!上一级菜单代号!边框符号!菜单宽度!菜单行数!菜单列数!起始横坐标!起始纵坐标!
说明:
1、边框符号为"0"暗示无框,为"1"暗示有边框;
2、上级菜单代号为"-1",暗示无上级菜单;
3、如菜单代号为"0",暗示主菜单。
当用户对菜单显示样式不满足时,可通过调解此文件设计本性化的界面。
#p#副标题#e#
二 编程实现
本措施文件为menu.c,部门代码如下:
#include
#define ESC 27
#define ENT 13
#define REFRESH 12
#define MAX_M 10 /* 菜单最大层数 */
void initial(),nomlastpos(),revcurpos(),disponepage(),dispprevline();
void dispnextline(),domenu(),getmenuconf(),keycont();
void getitem(), get_m_conf(), get_m_item(),clearwin(),execprog();
/* 标识每一菜单项的布局 */
struct menu {
short menu_code; /* 所属菜单代号 */
short item_order; /* 项顺序号 */
char item[20]; /* 菜单项名称 */
char prog[80]; /* 本项菜单执行措施 */
short submenu_code; /* 下一级菜单编号 */
struct menu *next; /* 指向上一项的指针 */
struct menu *prev; /* 指向下一项的指针 */
} m_item,*head,*this,*new,*last,*scrpos,*lastscrpos,*begin,*lastbegin,*lastscr[MAX_M];
/* 标识每一菜单内容的布局 */
struct menuconf {
short menu_code; /* 菜单代号 */
short last_code; /* 上一级菜单代号 */
short bord_flag; /* 边框符号 0--无边框 1--有边框 **/
short m_wight; /* 菜单显示宽度 */
short m_lengh; /* 每一行项数 */
short m_col; /* 菜单列数 */
short m_bx; /* 菜单起始横坐标 */
short m_by; /* 菜单起始纵坐标 */
} m_conf;
WINDOW *menuwin, *boxwin, *curw, *lastw[MAX_M], *workwin;
long curpos, lastcurpos, lastscrcurpos, lastmenucur[MAX_M];
short menu_no = 0, wno = 0;
/* 主函数 */
main()
{
initial();
getmenuconf(0); /* 取第0号菜单参数 */
/* 建设主窗口 */
menuwin=newwin(m_conf.m_lengh, m_conf.m_wight, m_conf.m_bx+1, m_conf.m_by+1);
curw=menuwin; lastw[wno]=menuwin;
getitem(); /* 取当前菜单各项内容 */
domenu(head, 0);
endwin();
}
/* 取菜单各项参数函数 */
void getitem()
{
FILE *fp;
char buff[0x100];
/* 建边框窗口 */
boxwin=newwin(m_conf.m_lengh+2,m_conf.m_wight+2,m_conf.m_bx,m_conf.m_by);
keypad(curw, TRUE);
if (m_conf.bord_flag==1) {
box(boxwin, 0,0 );
wrefresh(boxwin);
}
head=NULL;
if ((fp = fopen("./menu.def","r")) == NULL) {
fprintf(stderr, "\n不能打开菜单界说文件\n");
return;
}
while( fgets(buff, 0x100, fp)!=NULL) {
get_m_item(buff);
if (m_item.menu_code != menu_no)
continue;
new=(struct menu*)malloc(sizeof(struct menu));
if (head == NULL) {
last = head; head = new;
}
else {
this->next = new; last = this;
}
this = new;
this->menu_code=m_item.menu_code;
this->item_order=m_item.item_order;
strcpy(this->item,m_item.item);
strcpy(this->prog,m_item.prog);
this->submenu_code=m_item.submenu_code;
this->next=NULL;
this->prev = last;
}
fclose(fp);
}
/* 菜单处理惩罚函数 */
void domenu(curscrp, curp)
struct menu *curscrp;
int curp;
{
int i, x, y;
struct menu *mpos;
this = head;
disponepage(this);
curpos = curp; scrpos = curscrp;
lastcurpos = lastscrcurpos = 0;
revcurpos();
for(;;) {
switch (wgetch(curw)) {
case ENT:
/* 有下一级菜单 */
if ((!strcmp(scrpos->prog, "0")) && (scrpos->submenu_code != 0)) {
lastbegin = begin->next;
getmenuconf(scrpos->submenu_code);
menu_no = scrpos->submenu_code;
wno++;
lastmenucur[wno]=curpos;
lastscr[wno] = scrpos;
lastw[wno]=curw;
workwin=newwin(m_conf.m_lengh,m_conf.m_wight,m_conf.m_bx+1,m_conf.m_by+1);
curw=workwin;
getitem();
domenu(head, 0);
}
/* 是内部函数 */
/* 是外部可执行措施 */
else {
endwin();
execprog();
}
break;
case ESC:
case 'q':
case 'Q':
case '0':
/* 无上级菜单 */
if (m_conf.last_code == -1) {
clearwin(); endwin(); exit(0);
}
/* 有上级菜单 */
else {
menu_no = m_conf.last_code;
clearwin();
getmenuconf(menu_no);
getitem();
touchwin(lastw[wno]);
curw=lastw[wno];
curpos = lastmenucur[wno];
scrpos = lastscr[wno];
wno--;
wrefresh(curw);
}
break;
case 'r':
case 'R':
case REFRESH: /* 重显屏幕 */
wrefresh(curscr);
break;
case KEY_RIGHT: /* 右光标键 */
if ( scrpos->next != NULL ) {
lastcurpos = curpos; lastscrpos = scrpos;
scrpos=scrpos->next;
getyx(curw, x, y);
if((x==m_conf.m_lengh-1)&&(curpos%m_conf.m_col==m_conf.m_col-1)){
curpos-=(m_conf.m_col-1); lastcurpos = curpos - 1;
/* 实现向上卷屏 */
wmove(curw, 0, 0); wdeleteln(curw); dispnextline("R");
}
else
curpos++;
if ((curpos%m_conf.m_col == 0) && (m_conf.m_lengh == 1)) {
revcurpos(); break;
}
else {
nomlastpos(); revcurpos();
}
}
break;
case KEY_LEFT: /* 左光标键 */
if ( scrpos->prev != NULL ) {
lastcurpos = curpos; lastscrpos = scrpos;
scrpos=scrpos->prev;
getyx(curw, x, y);
if ((x==0) && (curpos%m_conf.m_col ==0)) {
curpos+=m_conf.m_col-1; lastcurpos = curpos + 1;
/* 实现向下卷屏 */
winsertln(curw); dispprevline("L");
}
else
curpos--;
if ((curpos%m_conf.m_col==m_conf.m_col-1)&&(m_conf.m_lengh==1)) {
revcurpos(); break;
}
else {
nomlastpos(); revcurpos();
}
}
break;
case KEY_UP: /* 上光标键 */
lastcurpos = curpos; lastscrpos = scrpos;
mpos = scrpos;
for(i=0; i/td>
if ( mpos->prev != NULL ) mpos=mpos->prev;
else break;
}
if ( i==m_conf.m_col ) {
getyx(curw, x, y);
if (x==0) {
lastcurpos += m_conf.m_col;
/* 实现向下卷屏 */
winsertln(curw); dispprevline("U");
}
else {
curpos-=m_conf.m_col;
}
scrpos = mpos;
if ( m_conf.m_lengh!=1)
nomlastpos();
revcurpos();
}
break;
case KEY_DOWN: /* 下光标键 */
lastcurpos = curpos; lastscrpos = scrpos;
mpos = scrpos;
for(i=0; i/td>
if ( mpos->next != NULL )
mpos=mpos->next;
else
break;
}
if ( i==m_conf.m_col ) {
getyx(curw, x, y);
if (x==m_conf.m_lengh-1) {
lastcurpos -= m_conf.m_col;
/* 实现向上卷屏 */
wmove(curw, 0, 0); wdeleteln(curw); dispnextline("D");
}
else
curpos+=m_conf.m_col;
scrpos = mpos;
if ( m_conf.m_lengh!=1)
nomlastpos();
revcurpos();
}
break;
default:
beep();
break;
}
}
}
/* 反显当前项函数 */
void revcurpos()
{
wattrset(curw, A_STANDOUT);
wmove(curw, curpos/m_conf.m_col,
(curpos%m_conf.m_col)*m_conf.m_wight/m_conf.m_col+m_conf.m_col);
wprintw(curw, "%s", scrpos->item);
wattrset(curw, A_NORMAL);
wrefresh(boxwin);
}
/* 正常显示上一项函数 */
void nomlastpos() {
wmove(curw, lastcurpos/m_conf.m_col, (lastcurpos%m_conf.m_col)
*m_conf.m_wight/m_conf.m_col+m_conf.m_col);
wprintw(curw, "%s", lastscrpos->item);
}
/* 显示一页函数 */
void disponepage(first)
struct menu *first;
{
short col, row;
begin=first; /* begin 为本页首指针 */
for(row=0; row/td>
for(col=0; col/td>
/* m_conf.m_wight/m_col为每一菜单项应占列数*/
wmove(curw,row,col*m_conf.m_wight/m_conf.m_col+m_conf.m_col);
wprintw(curw, "%s", first->item);
wrefresh(curw);
last = first;
first = first->next;
if (first == NULL) {
break;
}
}
}
}
/* 显示上一行函数 */
void dispprevline(flag)
char flag[2]; /* L-左光标引起 U-上光标引起 */
{
struct menu *tmppos;
int tmpcurpos;
tmpcurpos = curpos;
tmppos = scrpos;
if ( flag[0] == 'U') {
while ( tmpcurpos % m_conf.m_col != 0) {
tmppos = tmppos->prev;
tmpcurpos--;
}
tmppos = tmppos->prev;
}
for (tmpcurpos = m_conf.m_col-1; tmpcurpos >= 0; tmpcurpos--) {
wmove(curw, 0, (tmpcurpos%m_conf.m_col)
*m_conf.m_wight/m_conf.m_col+m_conf.m_col);
wprintw(curw, "%s", tmppos->item);
begin = tmppos; /*begin 为本页首指针*/
last = tmppos;
tmppos = tmppos->prev;
if (tmppos == NULL)
break;
}
wrefresh(curw);
}
/* 显示下一行函数 */
void dispnextline(flag)
char flag[2];/* R-右光标引起 D-下光标引起 */
{
struct menu *tmppos;
int tmpcurpos;
tmpcurpos = curpos;
tmppos = scrpos;
if ( flag[0] == 'D') {
while ( tmpcurpos % m_conf.m_col != m_conf.m_col-1) {
tmppos = tmppos->next; tmpcurpos++;
}
tmppos = tmppos->next;
}
for (tmpcurpos = 0; tmpcurpos < m_conf.m_col; tmpcurpos++) {
wmove(curw, m_conf.m_lengh-1, (tmpcurpos%m_conf.m_col)
*m_conf.m_wight/m_conf.m_col+m_conf.m_col);
wprintw(curw, "%s", tmppos->item);
last=tmppos;/* last 为本页最后一个结点指针 */
begin=tmppos; tmppos = tmppos->next;
if (tmppos == NULL)
break;
}
}
/* 取指定菜单参数函数 */
void getmenuconf(menu_code)
short menu_code;
{
FILE *fp;
char menu_buff[0x100];
if ((fp = fopen("menu.conf", "r"))==NULL) {
fprintf(stderr, "can not open menu config file");
return;
}
while( fgets(menu_buff, 0x100, fp)!=NULL ) {
get_m_conf(menu_buff);
if (m_conf.menu_code == menu_code)
break;
}
return ;
}
/* 取指定菜单参数处理惩罚函数 */
void get_m_conf(menu_conf)
char *menu_conf;
{
register i, j, k;
char buff[20];
j = k = 0;
for (i = 0; i < strlen(menu_conf); i++) {
if ( menu_conf[i] == '!' ) {
j++;
if ( j == 1) {
k = i+1;
continue;
}
switch(j) {
case 2:
memcpy(buff, &menu_conf[k], i-k);
buff[i-k]=0;
m_conf.menu_code = atoi(buff);
k=i+1;
break;
case 3:
memcpy(buff, &menu_conf[k], i-k);
buff[i-k]=0;
m_conf.last_code = atoi(buff);
k=i+1;
break;
case 4:
memcpy(buff, &menu_conf[k], i-k);
buff[i-k]=0;
m_conf.bord_flag = atoi(buff);
k=i+1;
break;
case 5:
memcpy(buff, &menu_conf[k], i-k);
buff[i-k]=0;
m_conf.m_wight = atoi(buff);
k=i+1;
break;
case 6:
memcpy(buff, &menu_conf[k], i-k);
buff[i-k]=0;
m_conf.m_lengh = atoi(buff);
k=i+1;
break;
case 7:
memcpy(buff, &menu_conf[k], i-k);
buff[i-k]=0;
m_conf.m_col = atoi(buff);
k=i+1;
break;
case 8:
memcpy(buff, &menu_conf[k], i-k);
buff[i-k]=0;
m_conf.m_bx = atoi(buff);
k=i+1;
break;
case 9:
memcpy(buff, &menu_conf[k], i-k);
buff[i-k]=0;
m_conf.m_by = atoi(buff);
k=i+1;
break;
default:
break;
}
}
}
}
/* 取指定项参数处理惩罚函数 */
void get_m_item(menu_item)
char *menu_item;
{
register i, j, k;
char buff[80];
j = k = 0;
for (i = 0; i < strlen(menu_item); i++) {
if ( menu_item[i] == '!' ) {
j++;
if ( j == 1) {
k = i+1;
continue;
}
switch(j) {
case 2:
memcpy(buff, &menu_item[k], i-k);
buff[i-k] = 0;
m_item.menu_code = atoi(buff);
k=i+1;
break;
case 3:
memcpy(buff, &menu_item[k], i-k);
buff[i-k] = 0;
m_item.item_order = atoi(buff);
k=i+1;
break;
case 4:
memcpy(buff, &menu_item[k], i-k);
buff[i-k] = 0;
strcpy(m_item.item,buff);
k=i+1;
break;
case 5:
memcpy(buff, &menu_item[k], i-k);
buff[i-k] = 0;
strcpy(m_item.prog,buff);
k=i+1;
break;
case 6:
memcpy(buff, &menu_item[k], i-k);
buff[i-k] = 0;
m_item.submenu_code = atoi(buff);
k=i+1;
break;
default:
break;
}
}
}
}
void initial() /* 自定开启 curses 函式 */
{
initscr();
cbreak(); nonl(); noecho();
intrflush(stdscr,FALSE);
keypad(stdscr,TRUE);
refresh();
}
/* 按键期待函数 */
void keycont()
{
fprintf(stderr, "按键继承..."); getchar();
}
/* 运行可执行措施函数 */
void execprog()
{
system("clear");
fprintf(stderr, "%s:\n", scrpos->item);
system(scrpos->prog);
keycont(); initial();
touchwin(boxwin); touchwin(curw); keypad(curw, TRUE);
wrefresh(boxwin); wrefresh(curw);
}
/* 排除窗口函数 */
void clearwin()
{
wmove(boxwin, 0, 0);
wclrtobot(boxwin); wrefresh(boxwin); delwin(curw); delwin(boxwin);
}
三 编译说明
该措施可用如下呼吁编译(在SCO OpenServer 5.05下编译通过):
cc -o menu menu.c -lcurses