码迷,mamicode.com
首页 > 其他好文 > 详细

C指针原理(33)-Ncurses-文本终端的图形

时间:2019-01-07 17:51:36      阅读:139      评论:0      收藏:0      [点我收藏+]

标签:cat   default   star   delete   return   ||   \n   ack   回车   

键盘管理

我们打造一个简单的单屏编辑器

首先构造一个仅带输入功能的编辑器,使用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?机制/

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;

}

运行

dp@dp:~/cursestest?%?gcc?-lncursesw?a.c?-o?mytest

dp@dp:~/cursestest?%?./mytest

接着继续完善,为它加上方向键的支持,移动方向键,可移动光标,并编辑光标处的内容。

#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?机制/

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>

//code?by?myhaspl@myhaspl.com

//date:2014/1/17

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?机制/

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;

}else

mvprintw(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;

}

运行

dp@dp:~/cursestest?%?gcc?-lncursesw?a.c?-o?mytest

dp@dp:~/cursestest?%?./mytest

C指针原理(33)-Ncurses-文本终端的图形

标签:cat   default   star   delete   return   ||   \n   ack   回车   

原文地址:http://blog.51cto.com/13959448/2339593

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!