⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 825.c

📁 curses界面编程
💻 C
📖 第 1 页 / 共 2 页
字号:
/*函数说明:界面*/
/*运行环境:redhat7.1*/
/*编译要求:包含头文件curses.h*/
/*Datech AnWin*/
/*2004-08-23*/
/******************************************************
*8-25下午
*
*
*****************************************************/
#include <curses.h>
#include <stdlib.h>
#include <string.h>

#define ENTER 10
#define ESC 27
#define WHI_BLU 1
#define BLU_WHI 2
#define RED_WHI 3
#define BLK_BLU 4
#define YEL_BLU 5

#define OK 3
#define CANCEL 4
#define OK_CANCEL 5

#define MENU_OK_ONLY 1000
#define MENU_OK_CANCEL 1001
#define MENU_INPUT 1002
#define MENU_SAVE_AS 1003
#define MENU_QUIT 1004
#define MENU_ABOUT 2000
#define MENU_HELP 2001
#define MENU_VERSION 2002

#define MENU_WIDTH 15 /*menu width*/
#define INPUT_SIZE 30 /*Max input string size*/
#define MNCANCEL 5000

char *menu1[]={"OK_ONLY","OK_CANCEL","INPUT","SAVE_AS","QUIT"};
char *menu2[]={"ABOUT","HELP","VERSION"};


void init_curses()
{
initscr();
start_color();/**初始化颜色**/
init_pair(WHI_BLU,COLOR_WHITE,COLOR_BLUE);/**set前景色和背景色model**/
init_pair(BLU_WHI,COLOR_BLUE,COLOR_WHITE);
init_pair(RED_WHI,COLOR_RED,COLOR_WHITE);
init_pair(BLK_BLU,COLOR_BLACK,COLOR_BLUE);
init_pair(YEL_BLU,COLOR_YELLOW,COLOR_BLUE);
curs_set(0);/***设置光标不可见**/
noecho();/**关闭回显**/
keypad(stdscr,TRUE);/**使用curses.h所定义的特殊键必须将keypad设定为TRUE**/
}

void draw_menubar(WINDOW *menubar)
{
wbkgd(menubar,COLOR_PAIR(BLU_WHI));

/*设置窗口或者屏幕的背景字符和属性*/
/*COLOR OF MAIN WINDOW font:BLUE,bak:WHITE*/
/**COLOR_PAIR的属性将和窗口中所有非空格的字符的属性进行OR操作**/
/**COLOR_PAIR为init_pair(1,COLOR_WHITE,COLOR_BLUE)定义的属性**/

/***move(1,0);
hline(ACS_HLINE,80);**/  /**draw horizion line TOP**/
move(24,0);
hline(ACS_HLINE,80);/**draw horizion line BOTTOM**/
move(0,0);
vline(ACS_VLINE,25);/**LEFT vertiral line**/
move(0,79);
vline(ACS_VLINE,25);/**RIGHT v LINE**/
wmove(menubar,0,1);
waddstr(menubar,"MENU_TEST");
wattron(menubar,COLOR_PAIR(RED_WHI));/*COLOR::front:RED  bak:WHITE */
waddstr(menubar,"(F1)");
wattroff(menubar,COLOR_PAIR(RED_WHI));/**close FONT**/
wmove(menubar,0,20);
waddstr(menubar,"MENU_HELP");
wattron(menubar,COLOR_PAIR(RED_WHI));
waddstr(menubar,"(F2)");
wattroff(menubar,COLOR_PAIR(RED_WHI));
}

WINDOW **draw_menu(int start_col,char **menu,int itemCount,int width)
/**扩展接口:菜单数目及内容**/
{
int i;
WINDOW **items;
items=(WINDOW **)malloc((itemCount+1)*sizeof(WINDOW *));/*menu+box=count+1*/
items[0]=newwin(itemCount+2,width+2,1,start_col);
wbkgd(items[0],COLOR_PAIR(BLU_WHI));
box(items[0],ACS_VLINE,ACS_HLINE);
for(i=1;i<=itemCount;i++)
items[i]=subwin(items[0],1,width,i+1,start_col+1);
for (i=0;i<itemCount;i++)
  wprintw(items[i+1],"%s",menu[i]);
wbkgd(items[1],COLOR_PAIR(WHI_BLU));/**childItem:front:WHITE,bak:BLUE**/
wrefresh(items[0]);
return items;
}

void delete_menu(WINDOW **items,int count)
{
int i;
for (i=0;i<count;i++)
  delwin(items[i]);
free(items);
}

int scroll_menu(WINDOW **items,int count,int menu_start_col)
{
int key;
int selected=0;
while (1) {
  key=getch();
  if (key==KEY_DOWN || key==KEY_UP)
 {
    wbkgd(items[selected+1],COLOR_PAIR(BLU_WHI));
    /**selected item:back:WHITE,**/

    wnoutrefresh(items[selected+1]);
if (key==KEY_DOWN)
        {
        selected=(selected+1) % count;
        }
else
        {
        selected=(selected+count-1) % count;
        }
   wbkgd(items[selected+1],COLOR_PAIR(WHI_BLU));
   wnoutrefresh(items[selected+1]);
   doupdate();
  }/**KEY_DOWN or KEY_UP**/
/*************agebar,**********************/
else if (key==KEY_LEFT || key==KEY_RIGHT)
 {
        touchwin(stdscr);
        refresh();
        if(menu_start_col == 20)/*download  menu1*/
         {      delete_menu(items,4);
                items = draw_menu(1,menu1,5,MENU_WIDTH);
                return scroll_menu(items,5,1);
         }
        if(menu_start_col == 1)/*download menu2*/
         {      delete_menu(items,6);
                items = draw_menu(20,menu2,3,MENU_WIDTH);
                return scroll_menu(items,3,20);
         }

}
/************************************/
else if (key==ESC)
 {
    return -1;
 }
else if (key==ENTER)
 {
    selected += (menu_start_col/20 + 1)*1000;/*menu NO.*/
    return selected;
  }
}/**while(1)**/
}
/*显示消息函数*/
/*char * topic :标题  char * message:信息*/
/*type:OK(=1),OK_CANCEL(=2)*/
WINDOW **messagebox(char * topic , char * message,int type)
{
int width,height;
int i,j;
int y, x;/*position of message box*/
char *tmp;
WINDOW **msgbox;
width = strlen(message);
if (width<20 )
 width = 20;
height=width/40+1;/*count of message line*/
x = (80-width)/2;
y = (20-height)/2;

/****************
if (width >40)
 {width=40;
 i=0;
 memset(
 while(message[i])
  tmp[i]=
  tmp[0]=
****************/
/*
* ______________________________________
*|TOPIC (1)                             |(0)
*|             MESSAGE (2)              |
*|             MESSAGE                  |
*|             MESSAGE                  |
*|         [OK](3) [CANCEL] (4)         |
*|______________________________________|
*/
msgbox=(WINDOW **)malloc((5)*sizeof(WINDOW *));/*box+topic+msgbox+OK_CANCEL*/
msgbox[0]=newwin(height+4,width+2,y,x);/**lines,rows,y,x**/
wbkgd(msgbox[0],COLOR_PAIR(BLU_WHI));/**front:BLUE,bak:WHITE**/
box(msgbox[0],ACS_VLINE,ACS_HLINE);/**draw box**/

msgbox[1]=subwin(msgbox[0],1,width,y+1,x+1);
msgbox[2]=subwin(msgbox[0],height,width,y+2,x+1);
wbkgd(msgbox[1],COLOR_PAIR(YEL_BLU));/**topic::backcolor=BLUE,front=YELLOW**/
wprintw(msgbox[1],"%s",topic);
wprintw(msgbox[2],"%s",message);
if (type == OK)
{msgbox[3]=subwin(msgbox[0],1,6,y+3,x+(width-4)/2+1);
wprintw(msgbox[3]," [OK] ");//OK button
msgbox[4]=subwin(msgbox[0],1,1,y+3,x+1);//CANCEL button,here can't see
}
if (type == OK_CANCEL)
{msgbox[3]=subwin(msgbox[0],1,6,y+3,x+3);
wprintw(msgbox[3]," [OK] ");/*length=1,so +1 :)*/
msgbox[4]=subwin(msgbox[0],1,10,y+3,x+width-10);
wprintw(msgbox[4]," [CANCEL] ");
}
wbkgd(msgbox[OK],COLOR_PAIR(WHI_BLU));/*default choose OK*/
wrefresh(msgbox[0]);
return msgbox;
}

/*msgrtn()*/
/*消息框返回值:OK or CANCEL*/
int msgrtn(WINDOW **msgbox)
{
int key;
int selected=3;
while (1) {
  key=getch();
  if (key==KEY_LEFT || key==KEY_RIGHT)
 {    /******[OK]***********[CANCEL]**/
    wbkgd(msgbox[selected],COLOR_PAIR(BLU_WHI));
    wnoutrefresh(msgbox[selected]);
   if (key==KEY_LEFT)
        {
        selected=OK;
        }
   else
        {
        selected=CANCEL;
        }
   wbkgd(msgbox[selected],COLOR_PAIR(WHI_BLU));
   wnoutrefresh(msgbox[selected]);
   doupdate();
  }/**KEY_LEFT/RIGHT**/
else if (key==ESC)
 {
    return CANCEL;
 }
else if (key==ENTER)
 {
    return selected;
 }
else if (key=='\t')/*TAB KEY*/
 {
        {wbkgd(msgbox[selected],COLOR_PAIR(BLU_WHI));
        wnoutrefresh(msgbox[selected]);
        selected = (selected == CANCEL) ? OK : CANCEL;
        wbkgd(msgbox[selected],COLOR_PAIR(WHI_BLU));
        wnoutrefresh(msgbox[selected]);
        doupdate();
        }
 }
}/**while(1)**/
}

/*结束消息显示*/
/*功能:结束显示消息框,回收内存*/
void del_box(WINDOW **box)
{int i;
for(i=0;i<=4;i++)
{
delwin(box[i]);
}
free(box);
beep();
}
////////////////////////////////////delete or free error!!!!!!
//void del_box(WINDOW **box)
//{int i;
//for(i=0;i<=4;i++)
//{
//delwin(box[i]);
//beep();
//mvprintw(10+i,10,"delwin[%d]",i);
//}
//free(box);
//}


/*function:inputs()*/
/*win:belonged window;;y,x:curser position*/
/**str:returned input string*/

void inputs(WINDOW **inpbox,int y,int x,char *str)
{
int ch;
//int x=1,y=2;
char tmpstr[INPUT_SIZE];
int count=-1,flag=1;
x=0;
memset(tmpstr,'\0',INPUT_SIZE);
memset(str,'\0',INPUT_SIZE);
noecho();
wmove(inpbox[2],0,1);
curs_set(TRUE);/***设置光标可见**/
intrflush(inpbox[2],TRUE);
wmove(inpbox[0],2,1);/* 移至输入位置 */
wrefresh(inpbox[2]);
wrefresh(inpbox[0]);
do {/* 等待输入 */
        ch=getch();
        switch(ch) {/* 判断 */
        case KEY_UP:/* 判断是否"↑"键被按下 */
        case KEY_DOWN:           /* 判断是否"↓"键被按下 */
        case KEY_RIGHT:           /* 判断是否"→"键被按下*/
        case KEY_LEFT:           /* 判断是否"←"键被按下 */
                beep();
                break;/*屏蔽方向键*/
        case ENTER:
        case '\t':                    /* 判断是否 TAB 键被按下 */
                flag = 0;
                curs_set(FALSE);  /*close curser*/
                wbkgd(inpbox[3],COLOR_PAIR(WHI_BLU));
                wnoutrefresh(inpbox[3]);/*[OK] button choosed*/
                break;
        case KEY_BACKSPACE:            /* 判断是否 BACKSPACE 键被按下 */
                if(count < 0 )
                {
                beep();
                break;
                }
                else
                {count--;
                mvwaddch(inpbox[2],0,--x,' ');/* delete 一个字符 */
                tmpstr[count] == '\0';
                break;
                }
        case ESC:            /* 判断是否[ESC]键被按下 */
                flag = 0;
                memset(tmpstr,'\0',INPUT_SIZE);/*ESC取消所有输入*/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -