欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C指針原理教程之Ncurses介紹

 更新時(shí)間:2019年02月07日 12:03:12   作者:myhaspl  
Ncurses 提供字符終端處理庫(kù),包括面板和菜單。為了能夠使用ncurses庫(kù),您必須在您的源程序中將curses.h包括(include)進(jìn)來(lái),而且在編譯的需要與它連接起來(lái). 在gcc中您可以使用參數(shù)-lcurses進(jìn)行編譯.

1、安裝Ncurses

Ncurses是一個(gè)能提供功能鍵定義(快捷鍵),屏幕繪制以及基于文本終端的圖形互動(dòng)功能的動(dòng)態(tài)庫(kù)。

Ncurses是一個(gè)能提供基于文本終端窗口功能的動(dòng)態(tài)庫(kù). Ncurses可以:

· 只要您喜歡,您可以使用整個(gè)屏幕

· 創(chuàng)建和管理一個(gè)窗口

· 使用8種不同的彩色

· 為您的程序提供鼠標(biāo)支持

· 使用鍵盤上的功能鍵

Ubuntu下

mysea@mysea-desktop:~$ sudo apt-get install libncurses5-dbg libncurses5-dev

mysea@mysea-desktop:~/test$ gcc -lncurses -o cursestest cursestest.c

Freebsd下

cd /usr/ports/devel/ncurses-devel

make install clean

2、hello,world

#include <curses.h>

int main(void){

  initscr();//初始化

  box(stdscr,ACS_VLINE,ACS_HLINE);//畫(huà)邊框

  mvaddstr(15,2,"hello,world");//在15,2顯示字符串

  refresh();//刷新屏幕

  getch();//等待按鍵

  endwin();//結(jié)束

  return 0;  

}

編譯及運(yùn)行

dp@dp:~/cursestest % gcc -lncurses 1.c -o mytest

dp@dp:~/cursestest % ./mytest

 3、色彩

然后編寫下面代碼:

#include <ncurses.h>

#include <locale.h>

#include <stdio.h>

int main(void){

//init_pair(short index,short foreground,short background)初始化顏色索引

//attron(COLOR_PAIR(索引號(hào))|屬性)

  setlocale(LC_ALL,"");

  initscr();//初始化

  box(stdscr,ACS_VLINE,ACS_HLINE);//畫(huà)邊框

  if (!has_colors()||start_color()==ERR){

    endwin();

    printf("終端不支持顏色\n");

    return 0;

  }

  init_pair(1,COLOR_GREEN,COLOR_BLACK);

  init_pair(2,COLOR_RED,COLOR_BLACK);

  init_pair(3,COLOR_WHITE,COLOR_BLUE);

  int i=0;

  for (i=1;i<=3;i++){

     attron(COLOR_PAIR(i));

     move(i,10);

     printw("hello,world:%d",i);

  }

  for (i=1;i<=3;i++){

     attron(COLOR_PAIR(i)|A_UNDERLINE);

     move(i+5,10);

     printw("hello,world:%d",i);

  }

  refresh();//刷新屏幕

  getch();//等待按鍵

  endwin();//結(jié)束
  

執(zhí)行

4、對(duì)中文的支持

dp@dp:~/cursestest % cat 1.c


#include <ncurses.h>

#include <locale.h>

#include <stdio.h>

int main(void){

//init_pair(short index,short foreground,short background)初始化顏色索引

//attron(COLOR_PAIR(索引號(hào))|屬性)

  setlocale(LC_ALL,"");

  initscr();//初始化

  box(stdscr,ACS_VLINE,ACS_HLINE);//畫(huà)邊框

  if (!has_colors()||start_color()==ERR){

    endwin();

    printf("終端不支持顏色\n");

    return 0;

  }

  init_pair(1,COLOR_GREEN,COLOR_BLACK);

  init_pair(2,COLOR_RED,COLOR_BLACK);

  init_pair(3,COLOR_WHITE,COLOR_BLUE);

  int i=0;

  for (i=1;i<=3;i++){

     attron(COLOR_PAIR(i));

     move(i,10);

     printw("hello,世界%d",i);

  }

  for (i=1;i<=3;i++){

     attron(COLOR_PAIR(i)|A_UNDERLINE);

     move(i+5,10);

     printw("hello,世界:%d",i);

  }

  refresh();//刷新屏幕

  getch();//等待按鍵

  endwin();//結(jié)束

  return 0;  

}

編譯時(shí)注意要使用ncursesw庫(kù),不使用ncurses庫(kù)

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

dp@dp:~/cursestest % ./mytest

5、窗口與子窗口

dp@dp:~/cursestest % cat 1.c

#include <ncurses.h>
#include <locale.h>
int main(){
//init_pair(short index,short foreground,short background)初始化顏色索引
//attron(COLOR_PAIR(索引號(hào))|屬性)
//newwin建立窗口,derwin建立窗口的子窗口(相對(duì)于父窗口相對(duì)位置),subwin建立窗口的子窗口(相對(duì)于根窗口絕對(duì)位置)
  setlocale(LC_ALL,"");
  WINDOW *win1,*win2,*subwin;
  initscr();//初始化
  win1=newwin(15,50,1,1);//新窗口(行,列,begin_y,begin_x)
  box(win1,ACS_VLINE,ACS_HLINE);
  mvwprintw(win1,1,1,"WIN1");
  mvwprintw(win1,2,1,"您好,很高興認(rèn)識(shí)您");
  win2=newwin(10,40,10,30);//新窗口(行,列,begin_y,begin_x)
  box(win2,ACS_VLINE,ACS_HLINE);
  mvwprintw(win2,1,1,"WIN2");
  mvwprintw(win2,2,1,"您好,很高興認(rèn)識(shí)您");
  subwin=derwin(win2,3,20,3,5); //子窗口
  box(subwin,ACS_VLINE,ACS_HLINE);
  mvwprintw(subwin,1,5,"按任意鍵退出");//(窗口,y,x,字符串)
  refresh();//刷新整個(gè)大窗口stdscr
  wrefresh(win1);
  wrefresh(win2);
  touchwin(win1);//轉(zhuǎn)換當(dāng)前窗口為win1
  wrefresh(win1);
  getch();//win1顯示完,等待按鍵顯示win2
  touchwin(win2);//轉(zhuǎn)換當(dāng)前窗口為win2 
  //使用doupdate,可以事先定義要刷新的部分,然后刷新
  wnoutrefresh(win2); 
  wnoutrefresh(subwin);
  doupdate();
  getch();//等待按鍵
  delwin(win1);
  delwin(subwin);
  delwin(win2);
  endwin();//結(jié)束
  return 0;  
}

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

6、自動(dòng)滾屏

dp@dp:~/cursestest % cat 2.c

#include <ncurses.h> 

#include <locale.h> 

int main(void){ 

  int y,x,i,j,h,w;

  setlocale(LC_ALL,""); 

  WINDOW *pad; 

  initscr();//初始化 

  

  getmaxyx(stdscr,h,w);//獲得屏幕尺寸 

  //畫(huà)背景

  for(i=0;i<h;i++){

     for(j=0;j<w;j++){

       mvaddch(i,j,ACS_CKBOARD);

     }

  }

  refresh();

  

  //建立窗口

  pad=newpad(80,90);
  for (i=0;i<80;i++){

    char line[90];

    sprintf(line,"line %d\n",i);

    mvwprintw(pad,i,1,line);

  }

  refresh();

  prefresh(pad,0,1,5,10,20,25);//刷新pad。0,1 為基墊需要顯示區(qū)域的左上角置(行列對(duì),以下同此)。5,10,20,45為屏幕顯示區(qū)域的左上角和右下角位置

 

  for(i=0;i<65;i++){

    prefresh(pad,i+1,1,5,10,20,25);//刷新pad,實(shí)現(xiàn)流屏;

    usleep(30000); 

  }

  getch();//等待按鍵

  delwin(pad); 

  endwin();//結(jié)束 

  return 0;  

}

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

dp@dp:~/cursestest % ./mytest

7、在窗口中移動(dòng)光標(biāo)

dp@dp:~/cursestest % cat 2.c

#include <ncurses.h> 

#include <locale.h> 

int main(void){ 

//init_pair(short index,short foreground,short background)初始化顏色索引 

//attron(COLOR_PAIR(索引號(hào))|屬性) 

//newwin建立窗口,derwin建立窗口的子窗口(相對(duì)于父窗口相對(duì)位置),subwin建立窗的子窗口(相對(duì)于根窗口絕對(duì)位置) 

  int x,y;

  setlocale(LC_ALL,""); 

  WINDOW *win1,*win2,*subwin; 

  initscr();//初始化 

  win1=newwin(15,50,1,1);//新窗口(行,列,begin_y,begin_x) 

  box(win1,ACS_VLINE,ACS_HLINE); 

  mvwprintw(win1,1,1,"WIN1"); 

  mvwprintw(win1,2,1,"myhaspl@myhaspl.com"); 

  win2=newwin(10,40,10,30);//新窗口(行,列,begin_y,begin_x) 

  box(win2,ACS_VLINE,ACS_HLINE); 

  wmove(win2,1,1);//移動(dòng)某窗口的光標(biāo)

  printw("WIN2");

  wmove(win2,2,1);//移動(dòng)某窗口的光標(biāo)。(窗口,y,x)

  printw("myhaspl@myhaspl.com"); 

  subwin=derwin(win2,3,20,4,5); //子窗口 

  box(subwin,ACS_VLINE,ACS_HLINE); 

  mvwprintw(subwin,1,5,"按任意鍵退出");//(窗口,y,x,字符串) 

    

  refresh();//刷新整個(gè)大窗口stdscr 

  wrefresh(win1); 

  wrefresh(win2); 

  

  move(5,60);//在stdscr移動(dòng)光標(biāo)

  printw("hello.........");

  touchwin(win1);//轉(zhuǎn)換當(dāng)前窗口為win1 

  wrefresh(win1); 

  getch();//win1顯示完,等待按鍵顯示win2 

  touchwin(win2);//轉(zhuǎn)換當(dāng)前窗口為win2 

  //使用doupdate,可以事先定義要刷新的部分,然后刷新 

  wnoutrefresh(win2);  

  wnoutrefresh(subwin); 

  doupdate(); 

  getyx(subwin,y,x);//獲得當(dāng)前邏輯光標(biāo)位置

  mvwprintw(subwin,y+1,x,"................");//在“按任意鍵退出"下一行輸出"..............."

  getch();//等待按鍵 

  delwin(win1); 

  delwin(subwin); 

  delwin(win2); 

  endwin();//結(jié)束 

  return 0;  

} 

編譯后運(yùn)行

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

dp@dp:~/cursestest % ./mytest

8、菜單

dp@dp:~/cursestest % cat 2.c

#include <locale.h>

#include <menu.h> 

#include <stdio.h>

#include <ctype.h>

//定義菜單項(xiàng)

static const char *menus[]={

  "1-1","1-2","1-3","2-1","2-2","2-3"

};

#define CITEM sizeof(menus)/sizeof(menus[0])//菜單項(xiàng)數(shù)

ITEM *items[CITEM];

int main(int argc,char *argv[]){

  int i;

  int ch;

  int mrows,mcols;

  WINDOW *win,*subwin;

  MENU *mymenu;

  

  //初始化屏幕

  initscr();

  //不用等待回車鍵

  cbreak();

  //不回顯

  noecho();

  //可以處理功能鍵 

  keypad(stdscr,TRUE); 

  

  //建立菜單項(xiàng)

  for(i=0;i<CITEM;i++){

    items[i]=new_item(menus[i],menus[i]);//第二個(gè)參數(shù)為菜單項(xiàng)的描述

  } 

  //建立菜單

  mymenu=new_menu(items);

  set_menu_format(mymenu,CITEM,1);  //設(shè)置CITEM行1列的菜單

  set_menu_mark(mymenu,">");//菜單選中的MARK

  //獲得菜單的行列數(shù)

  scale_menu(mymenu,&mrows,&mcols);

  //建立窗口和子窗口

  win=newwin(mrows+2,mcols+2,3,30);

  keypad(win,TRUE);

  box(win,0,0);

  subwin=derwin(win,0,0,1,1);

  //設(shè)置菜單的窗口

  set_menu_sub(mymenu,subwin);  

  //在子窗口上放置菜單

  post_menu(mymenu);

  

  refresh();

  wrefresh(win);

  //獲得輸入,并移動(dòng)選擇到相應(yīng)的菜單項(xiàng) 

  while(toupper(ch=wgetch(win))!='\n'){

     if(ch==KEY_DOWN)

       menu_driver(mymenu,REQ_DOWN_ITEM);//移動(dòng)菜單選擇

     else if(ch==KEY_RIGHT)

       menu_driver(mymenu,REQ_RIGHT_ITEM);

     else if (ch==KEY_UP)

       menu_driver(mymenu,REQ_UP_ITEM); 

     else if (ch==KEY_LEFT)

       menu_driver(mymenu,REQ_LEFT_ITEM);

  }

  //輸出當(dāng)前項(xiàng)

  mvprintw(LINES-2,0,"you select the item :%s\n",item_name(current_item(mymenu)));

  refresh();

  unpost_menu(mymenu);

  getch();

  //釋放內(nèi)存

  free_menu(mymenu);

  for(i=0;i<CITEM;i++) free_item(items[i]);

  endwin();

  return 1;

}

編譯并運(yùn)行

dp@dp:~/cursestest % gcc -lncursesw -lmenu 2.c -o mytest

dp@dp:~/cursestest % ./mytest

相關(guān)文章

  • c++ 移動(dòng)構(gòu)造相關(guān)總結(jié)

    c++ 移動(dòng)構(gòu)造相關(guān)總結(jié)

    這篇文章主要介紹了c++ 移動(dòng)構(gòu)造的相關(guān)資料,幫助大家更好的理解和學(xué)習(xí)使用c++,感興趣的朋友可以了解下
    2021-02-02
  • C語(yǔ)言switch使用之詭異用法詳解

    C語(yǔ)言switch使用之詭異用法詳解

    今天小編就為大家分享一篇C語(yǔ)言switch使用之詭異用法詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2019-12-12
  • C語(yǔ)言 詳細(xì)解析時(shí)間復(fù)雜度與空間復(fù)雜度

    C語(yǔ)言 詳細(xì)解析時(shí)間復(fù)雜度與空間復(fù)雜度

    算法復(fù)雜度分為時(shí)間復(fù)雜度和空間復(fù)雜度。其作用: 時(shí)間復(fù)雜度是度量算法執(zhí)行的時(shí)間長(zhǎng)短;而空間復(fù)雜度是度量算法所需存儲(chǔ)空間的大小
    2022-04-04
  • C++實(shí)現(xiàn)LeetCode(37.求解數(shù)獨(dú))

    C++實(shí)現(xiàn)LeetCode(37.求解數(shù)獨(dú))

    這篇文章主要介紹了C++實(shí)現(xiàn)LeetCode(37.求解數(shù)獨(dú)),本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-07-07
  • c++實(shí)現(xiàn)發(fā)送http請(qǐng)求通過(guò)get方式獲取網(wǎng)頁(yè)源代碼

    c++實(shí)現(xiàn)發(fā)送http請(qǐng)求通過(guò)get方式獲取網(wǎng)頁(yè)源代碼

    這篇文章主要介紹了c++實(shí)現(xiàn)發(fā)送http請(qǐng)求,通過(guò)get方式獲取網(wǎng)頁(yè)源代碼的示例,需要的朋友可以參考下
    2014-02-02
  • C++內(nèi)存管理詳細(xì)解析

    C++內(nèi)存管理詳細(xì)解析

    這篇文章主要給大家分享的是C++內(nèi)存管理的詳細(xì)內(nèi)容學(xué)習(xí),下面文章圍繞C++內(nèi)存管理的相關(guān)資料展開(kāi)具體學(xué)習(xí)內(nèi)容,需要的朋友可以參考一下,希望對(duì)你有所幫助
    2021-11-11
  • 淺析Boost智能指針:scoped_ptr shared_ptr weak_ptr

    淺析Boost智能指針:scoped_ptr shared_ptr weak_ptr

    雖然通過(guò)弱引用指針可以有效的解除循環(huán)引用,但這種方式必須在程序員能預(yù)見(jiàn)會(huì)出現(xiàn)循環(huán)引用的情況下才能使用,也可以是說(shuō)這個(gè)僅僅是一種編譯期的解決方案,如果程序在運(yùn)行過(guò)程中出現(xiàn)了循環(huán)引用,還是會(huì)造成內(nèi)存泄漏的
    2013-09-09
  • MFC對(duì)話框中添加狀態(tài)欄的方法

    MFC對(duì)話框中添加狀態(tài)欄的方法

    這篇文章主要介紹了MFC對(duì)話框中添加狀態(tài)欄的方法,實(shí)例分析了MFC對(duì)話框添加狀態(tài)欄所涉及的相關(guān)成員變量與事件實(shí)現(xiàn)技巧,需要的朋友可以參考下
    2015-07-07
  • C語(yǔ)言結(jié)課設(shè)計(jì)之計(jì)算器功能

    C語(yǔ)言結(jié)課設(shè)計(jì)之計(jì)算器功能

    這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言結(jié)課設(shè)計(jì)之計(jì)算器功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-02-02
  • C++實(shí)現(xiàn)走迷宮小游戲

    C++實(shí)現(xiàn)走迷宮小游戲

    這篇文章主要為大家詳細(xì)介紹了C++實(shí)現(xiàn)走迷宮小游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-03-03

最新評(píng)論