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

📄 ct.c

📁 curses界面编程
💻 C
字号:
/*函数说明:界面*/
/*运行环境:redhat7.1*/
/*编译要求:包含头文件curses.h*/
/*Datech AnWin*/
/*2004-08-23*/
/******************************************************
*8-24下午
*
*
*****************************************************/
#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_FILE 1000
#define MENU_OPEN 1001
#define MENU_SAVE 1002
#define MENU_SAVE_AS 1003
#define MENU_QUIT 1004
#define MENU_ABOUT 2000
#define MENU_HELP 2001
#define MENU_VERSION 2002

char *menu1[]={"FILE","OPEN","SAVE","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_1");
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_2");
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);/**lines,rows,y:1,x:start**/
wbkgd(items[0],COLOR_PAIR(BLU_WHI));/**front:BLUE,bak:WHITE**/
box(items[0],ACS_VLINE,ACS_HLINE);/**draw box**/
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]);/*Write Item_name to item*/
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);
        /**通知ncurses整个win窗口已经被改动过**/
        refresh();
        if(menu_start_col == 20)/*download  menu1*/
         {      delete_menu(items,4);
                items = draw_menu(1,menu1,5,8);
		return scroll_menu(items,5,1);
         }
        if(menu_start_col == 1)/*download menu2*/
         {      delete_menu(items,6);
                items = draw_menu(20,menu2,3,8);
                return scroll_menu(items,3,20);
         }

}
/************************************/
else if (key==ESC)
 {
    return -1;
 }
else if (key==ENTER)
 {
    selected += (menu_start_col/20 + 1)*1000;
    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;
 }
}/**while(1)**/
}

/*结束消息显示*/
/*功能:结束显示消息框,回收内存*/
void del_box(WINDOW **box)
{int i;
for(i=0;i<=4;i++)
delwin(box[i]);
free(box);
}

/*输入框函数inputbox()*/
/*char * topic :标题  char * inputStr:返回的输入信息*/
WINDOW **inpbox(char * topic , char * inputStr)
{
int width,height;
int i,j; 
int y, x;/*position of message box*/
char *tmp;
WINDOW **inputbox;
width = 25;
height=2;
x = (80-width)/2;
y = (20-height)/2;

/*
* ______________________________________
*|TOPIC (1)                             |(0)
*|    INPUTBOX:||||||||||||||||| (2)    |
*|                                      |
*|         [OK](3) [CANCEL] (4)         |
*|______________________________________|
*/
inputbox=(WINDOW **)malloc((5)*sizeof(WINDOW *));/*box+topic+inputbox+OK_CANCEL*/
inputbox[0]=newwin(height+4,width+2,y,x);/**lines,rows,y,x**/
wbkgd(inputbox[0],COLOR_PAIR(BLU_WHI));/**front:BLUE,bak:WHITE**/
box(inputbox[0],ACS_VLINE,ACS_HLINE);/**draw box**/

inputbox[1]=subwin(inputbox[0],1,width,y+1,x+1);
inputbox[2]=subwin(inputbox[0],height,width,y+2,x+1);

wprintw(inputbox[1],"%s",topic);
inputbox[3]=subwin(inputbox[0],1,6,y+3,x+3);
wprintw(inputbox[3]," [OK] ");/*length=1,so +1 :)*/
inputbox[4]=subwin(inputbox[0],1,10,y+3,x+width-10);
wprintw(inputbox[4]," [CANCEL] ");

//wbkgd(inputbox[OK],COLOR_PAIR(WHI_BLU));/*default choose OK*/
//wrefresh(inputbox[0]);
///////////////////////////////////////////当输入后按TAB有效/////////////////////

wmove(inputbox[0],2,1);
curs_set(true);/***设置光标可见**/
echo();/**开启回显功能**/
mvwgetstr(inputbox[0],2,1,inputStr);
return inputbox;
}


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

	

int main()
{
  int key,tmpkey;
  int menuNo,itemNo;
  WINDOW *menubar,*messagebar;
/*  char *menu1[]={"FILE","OPEN","SAVE","SAVE_AS","QUIT"};*/
/*  char *menu2[]={"ABOUT","HELP","VERSION"};*/

  init_curses();
  bkgd(COLOR_PAIR(WHI_BLU));
  menubar=subwin(stdscr,1,80,0,0);
  messagebar=subwin(stdscr,1,79,23,1);
  draw_menubar(menubar);
/************************************************/
  move(2,1);
  printw("Press F1 or F2 to open the menus. ");
  printw("ESC quits.");
  refresh();
do
{
int selected_item,selected_msg;
WINDOW **menu_items;
WINDOW **msgbox;
WINDOW **inputbox;
char inputStr[40];
key=getch();
werase(messagebar);
wrefresh(messagebar);
if (key==KEY_F(1))
        {
        menu_items=draw_menu(1,menu1,5,8);

/*WINDOW **draw_menu(int start_col,char **menu,int count,int width)*/
/**接口:start_col开始的列,**menu内容,count:菜单数目,width菜单宽度**/
        selected_item=scroll_menu(menu_items,5,1);
        delete_menu(menu_items,6);
        }
else if (key==KEY_F(2))
        {
        menu_items=draw_menu(20,menu2,3,8);
        selected_item=scroll_menu(menu_items,3,20);
        delete_menu(menu_items,4);
        }
if (selected_item<0)
        wprintw(messagebar,"You haven't selected any child item");
else
        {
        /**menuNo = selected_item/1000;
        *itemNo = selected_item%100;
        */
        switch(selected_item)
        {
/********************************
*#define MENU_FILE 1000
*#define MENU_OPEN 1001
*#define MENU_SAVE 1002
*#define MENU_SAVE_AS 1003
*#define MENU_QUIT 1004
*#define MENU_ABOUT 2000
*#define MENU_HELP 2001
*#define MENU_VERSION 2002
****************************/
        case 1000:
        case 1001:
        case 1002:
	case 1003:
		inputStr = malloc(40*sizeof(char));
		inputbox = inpbox("TOPIC",&inputStr);
        	selected_msg = inpboxrtn(inputbox);
        	del_box(inputbox);
        	curs_set(0);/***设置光标不可见**/
		noecho();/**关回显**/
        	if(selected_msg == OK)
        	{
        	wprintw(messagebar,"Input string is::[%s]",inputStr);
        	free(inputStr);
		}
 		else if(selected_msg == CANCEL)
 		{
 			wprintw(messagebar,"User CANCELED!");
 			free(inputStr);
 		}
 		selected_msg = CANCEL;
 		selected_item = 0;
		break;
	case MENU_QUIT:
        	msgbox = messagebox("TOPIC","  Are You Sure To Quit?",OK_CANCEL);
        	selected_msg = msgrtn(msgbox);
        	del_box(msgbox);
        	if(selected_msg == OK)
        	{
 			delwin(messagebar);
			delwin(menubar);
			clear();
			endwin();
			return 0;
		}
 		else if(selected_msg == CANCEL)
 		wprintw(messagebar,"User CANCELED!");
 		selected_msg = CANCEL;
 		selected_item = 0;
		 break;
        case MENU_ABOUT://2000
                msgbox = messagebox("ABOUT","DaTech(C)AnWin 2004-08-23",OK);/*only OK button*/
        	tmpkey=getch();
        	del_box(msgbox);
                selected_item = 0;
                break;
        case MENU_HELP://2001
        	msgbox = messagebox("HELP","Use F1~FX and UP/DOWN/LEFT/RIGHT key choose menu",OK);/*only OK button*/
        	tmpkey=getch();
        	del_box(msgbox);
                selected_item = 0;
                break;
        case MENU_VERSION://2002
                msgbox = messagebox("VERSION","DaTech(C)AnWin Version 1.0",OK);/*only OK button*/
        	tmpkey=getch();
        	del_box(msgbox);
                selected_item = 0;
                break;
        default:
/*              wprintw(messagebar,
*       "An Error Detected!!Selected will not %d",selected_item);
*/

        }/*switch()*/
        }
        touchwin(stdscr);
        refresh();
} while (key!=ESC);
delwin(messagebar);
delwin(menubar);
clear();
endwin();
return 0;
}

⌨️ 快捷键说明

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