1 / 18

UNIX 螢幕導向程式的發展利器 - curses

UNIX 螢幕導向程式的發展利器 - curses. Gwan-Hwan Hwang Dept. CSIE National Taiwan Normal University 2009.11.24. 前言. 為了因應網路上各式各樣的終端機形態 (terminal), UNIX 上特別發展出一套函式庫 , 專門用來處理 UNIX 上游標移動及螢幕的顯示 . curses.h 函式庫. curses 的歷史與版本. cureses 最早是由柏克萊大學的 Bill Joy 及 Ken Arnold 所發展出來的 .

cormac
Download Presentation

UNIX 螢幕導向程式的發展利器 - curses

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. UNIX 螢幕導向程式的發展利器 - curses Gwan-Hwan Hwang Dept. CSIE National Taiwan Normal University 2009.11.24

  2. 前言 • 為了因應網路上各式各樣的終端機形態 (terminal), UNIX 上特別發展出一套函式庫, 專門用來處理 UNIX 上游標移動及螢幕的顯示. • curses.h 函式庫

  3. curses 的歷史與版本 • cureses 最早是由柏克萊大學的 Bill Joy 及 Ken Arnold 所發展出來的. • 當時發展此一函式庫主要原因是為了提高程式對不同終端機的相容性而設計的. 因此, 利用 curses 發展出來的程式將和您所使用的終端機無關. • 本文的介紹 以 System V 的 curses 版本為主.

  4. 如何在您的程式使用 curses ? • 在您的 C 程式的檔頭將 <curses.h> include 進來.當您引進 curses.h這個函式庫後, 系統會自動將 <stdio.h> 和 <unctl.h>一併 include 進 來. 另外, 在 System V 版本中, <terminfo.h> 這個函式庫也將一併include進來. #include <curses.h> main() { : : : : }

  5. 如何編譯(compile) • 編輯好程式, 在 UNIX 提示符號下鍵入: % /usr/5bin/cc [file.c] -lcurses ^^^^^^^ 引進 curses.h 這個 library 或 % /usr/5bin/cc [file.c] -lcurses -ltermlib

  6. 如何開始我的第一個 curses 程式? • 在開始使用 curses 的一切命令之前, 必須先利用 initscr()這個函式來開啟 curses 模式. • 在結束 curses 模式前 ( 通常在您結束程式前 ) 也必須以endwin()來關閉 curses 模式. #include <curses.h> main() { initscr(); : : endwin(); }

  7. 將平常較常用的一些設定放在一個叫 initial()的函式內 void initial() { initscr(); cbreak(); nonl(); noecho(); intrflush(stdscr,FALSE); keypad(stdscr,TRUE); refresh(); }

  8. 游標的控制 • move(y,x) • 將游標移動至 x,y 的位置 • getyx(win,y,x) • 得到目前游標的位置 • (請注意! 是 y,x 而不是 &y,&x )

  9. 有關清除螢幕的函式 • clear() • erase() • 將整個螢幕清除 • (請注意配合 refresh() 使用)

  10. 如何在螢幕上顯示字元 • echochar(ch):顯示某個字元 • addch(ch):顯示某個字元 • mvaddch(y,x,ch:在(x,y) 上顯示某個字元相當於呼叫 move(y,x);addch(ch); • addstr(str):顯示一串字串 • mvaddstr(y,x,str):在(x,y) 上顯示一串字串相當於呼叫 move(y,x);addstr(str); • printw(format,str):類似 printf() , 以一定的格式輸出至螢幕 • mvprintw(y,x,format,str) 在(x,y) 位置上做 printw 的工作. 相當於呼叫 move(y,x);printw(format,str);

  11. 如何從鍵盤上讀取字元 • getch():從鍵盤讀取一個字元 (注意! 傳回的是整數值) • getstr():從鍵盤讀取一串字元 • scanw(format,&arg1,&arg2...):如同 scanf, 從鍵盤讀取一串字元

  12. 如何利用方向鍵 • curses 將一些如方向鍵等特殊控制字元, 以 KEY_ 為開頭定義在 curses.h • KEY_UP 0403 ↑ • KEY_DOWN 0402 ↓ • KEY_LEFT 0404 ← • KEY_RIGHT 0405 → • KEY_HOME 0406 Home key • KEY_BACKSPACE 0407 backspace • KEY_F0 0410 Function keys. • KEY_F(n) (KEY_F0+(n)) Formula for f . • KEY_NPAGE 0522 Next page • KEY_PPAGE 0523 Previous page • [TAB] /t • [ENTER] /r • [ESC] 27 • [BACKSPACE] 127

  13. 如何改變螢幕顯示字元的屬性 • 為了使輸出的螢幕畫面更為生動美麗, 我們常須要在螢幕上做一些如反白,閃爍等變化. curses 定義了一些特殊的屬性, 透過這些定義, 我們也可以在 curses 程式裏控制螢幕的輸出變化.

  14. 其他常用的一些函式 • beep() 發出一聲嗶聲 • box(win,ch1,ch2) 自動畫方框 • ch1: 畫方框時垂直方向所用字元 • ch2: 畫方框時水平方向所用字元

  15. 視窗的建立 • 視窗的建立, 以 newwin() 這個函式來完成. 同時, 需宣告此視窗為 WINDOW結構變數. • WINDOW *newwin(lines,colums,start_y,start_x); WINDOW *win; win=newwin(10,20,0,0);

  16. 多視窗處理函式的格式 • 這一類函式和一般的基本函式極為類似, 幾乎每一個基本函式都有一個對應的視窗處理函式. 一般將 'w' 加在函式的裡頭作為區別, 'w' 乃 'window' 之意. • wmove(win,y,x) 即對 win 這個視窗做 move() 動作. • wmove(stdscr,y,x) 相當於 move(y,x)

  17. 介紹一些較重要的函式 • wmove(win,y,x) • touchwin(win) • wrefresh(win) • mvwaddstr(win,y,x,str) • wattron(attr) • delwin(win) • subwin(win,ny,nx,y,x)

  18. 視窗的捲動 • scrollok(win,TRUE); 開啟 • scrollok(win,FALSE); 關閉

More Related