C指针原理(33)-Ncurses-文本终端的图形
生活随笔
收集整理的這篇文章主要介紹了
C指针原理(33)-Ncurses-文本终端的图形
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
鍵盤管理
我們打造一個簡單的單屏編輯器
首先構(gòu)造一個僅帶輸入功能的編輯器,使用wgetch來捕捉輸入。
#include <locale.h>#include <stdio.h>#include <ncurses.h>int main(int argc, char *argv[]){setlocale(LC_ALL,"");initscr();clear();noecho();cbreak();if(has_colors() == FALSE){ endwin();printf("你的終端不支持色彩!\n");return (1);}start_color(); /啟動color 機(jī)制/init_pair(1, COLOR_GREEN, COLOR_BLACK);WINDOW *win1; int width=COLS-14;int height=LINES-14;int x,y;win1=newwin(height,width,7,7);//新窗口(行,列,begin_y,begin_x)keypad(win1,TRUE);box(win1,ACS_VLINE,ACS_HLINE);wattron(win1,COLOR_PAIR(1));wrefresh(win1);getyx(win1,y,x);++y;++x;while(1){int c=mvwgetch(win1,y,x);++x;if (x>=width-1){++y;x=1;}if (y>=height-1){y=1;}mvwprintw(win1,y,x,"%c",c);wrefresh(win1);}wattroff(win1,COLOR_PAIR(1));endwin();return 0;}運(yùn)行
dp@dp:~/cursestest % gcc -lncursesw a.c -o mytest
dp@dp:~/cursestest % ./mytest
接著繼續(xù)完善,為它加上方向鍵的支持,移動方向鍵,可移動光標(biāo),并編輯光標(biāo)處的內(nèi)容。
#include <locale.h>#include <stdio.h>#include <ncurses.h>int main(int argc, char *argv[]){setlocale(LC_ALL,"");initscr();clear();noecho();cbreak();if(has_colors() == FALSE){ endwin();printf("你的終端不支持色彩!\n");return (1);}start_color(); /啟動color 機(jī)制/mvprintw(5,COLS/2-10,"簡單編輯器-僅限于單個屏幕的編輯");refresh();init_pair(1, COLOR_GREEN, COLOR_BLACK);WINDOW *win1; int width=COLS-14;int height=LINES-14;int x,y;win1=newwin(height,width,7,7);//新窗口(行,列,begin_y,begin_x)keypad(win1,TRUE);box(win1,ACS_VLINE,ACS_HLINE);wattron(win1,COLOR_PAIR(1));wrefresh(win1);getyx(win1,y,x);++y;++x;while(1){int c=mvwgetch(win1,y,x);switch(c){case KEY_RIGHT:++x;if (x>=width-1) {++y;x=1;}break;case KEY_LEFT:--x;if (x<1){--y;x=width-2;}break;case KEY_UP:--y;if (y<1){y=height-2;}break;case KEY_DOWN:++y;if (y>=height-1){y=1;}break;default:mvwprintw(win1,y,x,"%c",c);++x;if (x>=width-1){++y;x=1;}if (y>=height-1){y=1;}wrefresh(win1);}}wattroff(win1,COLOR_PAIR(1));endwin();return 0;}dp@dp:~/cursestest % gcc -lncursesw a.c -o mytest
dp@dp:~/cursestest % ./mytest
我們定義delete鍵為刪除某個字符,回車符表示換行,同時定義F12為刪除整行,F1為退出。
dp@dp:~/cursestest % cat a.c
#include <locale.h>#include <stdio.h>#include <ncurses.h>int main(int argc, char *argv[]){setlocale(LC_ALL,"");initscr();clear();noecho();cbreak();if(has_colors() == FALSE){ endwin();printf("你的終端不支持色彩!\n");return (1);}start_color(); /啟動color 機(jī)制/mvprintw(5,COLS/2-10,"簡單編輯器-僅限于單個屏幕的編輯");refresh();init_pair(1, COLOR_GREEN, COLOR_BLACK);WINDOW *win1; int width=COLS-14;int height=LINES-14;int x,y;win1=newwin(height,width,7,7);//新窗口(行,列,begin_y,begin_x)keypad(win1,TRUE);wattron(win1,COLOR_PAIR(1));box(win1,ACS_VLINE,ACS_HLINE);wrefresh(win1);getyx(win1,y,x);++y;++x;while(1){int c=mvwgetch(win1,y,x);switch(c){case KEY_RIGHT:++x;if (x>=width-1) {++y;x=1;}break;case KEY_LEFT: --x;if (x<1){--y;x=width-2;}break;case KEY_UP:--y;if (y<1){y=height-2;}break;case KEY_DOWN:++y;if (y>=height-1){y=1;}break;case 10:++y;if (y>=height-1){y=1;}break;case KEY_F(1)://退出mvprintw(LINES-3,2,"退出編輯器嗎?");mvprintw(LINES-2,2," ");refresh();int ans=getch();if (ans=='Y' ||ans=='y'){mvprintw(LINES-2,2,"是\n");refresh();return 0;}elsemvprintw(LINES-2,2,"否\n");refresh();break;case KEY_F(12)://刪除某行wdeleteln(win1);winsertln(win1);box(win1,ACS_VLINE,ACS_HLINE);case KEY_DC://刪除某字符mvwprintw(win1,y,x," ");break;default:mvwprintw(win1,y,x,"%c",c);++x;if (x>=width-1){++y;x=1;}if (y>=height-1){y=1;}wrefresh(win1);}}wattroff(win1,COLOR_PAIR(1));endwin();return 0;}運(yùn)行
dp@dp:~/cursestest % gcc -lncursesw a.c -o mytest
dp@dp:~/cursestest % ./mytest
總結(jié)
以上是生活随笔為你收集整理的C指针原理(33)-Ncurses-文本终端的图形的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C指针原理(34)-Ncurses-文本
- 下一篇: Mybatis的xml配置实例