📄 ncurses-programming-howto.html
字号:
} refresh(); /* Print it on to the real screen */ getch(); /* Wait for user input */ endwin(); /* End curses mode */ return 0;}</span></font></pre></td></tr></table></div><p>This program is self-explanatory. But I used functions whicharen't explained yet. The function <var class="LITERAL">getch()</var> is used to get a character from user. It isequivalent to normal <var class="LITERAL">getchar()</var> exceptthat we can disable the line buffering to avoid <enter> afterinput. Look for more about <var class="LITERAL">getch()</var>andreading keys in the <a href="#KEYS">key management section</a> .The functions attron and attroff are used to switch some attributeson and off respectively. In the example I used them to print thecharacter in bold. These functions are explained in detaillater.</p></div></div><div class="SECT1"><hr><h2 class="SECT1"><a name="AWORDWINDOWS" id="AWORDWINDOWS">5. AWord about Windows</a></h2><p>Before we plunge into the myriad ncurses functions, let me clearfew things about windows. Windows are explained in detail infollowing <a href="#WINDOWS">sections</a></p><p>A Window is an imaginary screen defined by curses system. Awindow does not mean a bordered window which you usually see onWin9X platforms. When curses is initialized, it creates a defaultwindow named <var class="LITERAL">stdscr</var> which representsyour 80x25 (or the size of window in which you are running) screen.If you are doing simple tasks like printing few strings, readinginput etc., you can safely use this single window for all of yourpurposes. You can also create windows and call functions whichexplicitly work on the specified window.</p><p>For example, if you call</p><table border="0" bgcolor="#E0E0E0" width="100%"><tr><td><pre class="PROGRAMLISTING"><font color="#000000"> printw("Hi There !!!"); refresh();</font></pre></td></tr></table><p>It prints the string on stdscr at the present cursor position.Similarly the call to refresh(), works on stdscr only.</p><p>Say you have created <a href="#WINDOWS">windows</a> then youhave to call a function with a 'w' added to the usual function.</p><table border="0" bgcolor="#E0E0E0" width="100%"><tr><td><pre class="PROGRAMLISTING"><font color="#000000"> wprintw(win, "Hi There !!!"); wrefresh(win);</font></pre></td></tr></table><p>As you will see in the rest of the document, naming of functionsfollow the same convention. For each function there usually arethree more functions.</p><table border="0" bgcolor="#E0E0E0" width="100%"><tr><td><pre class="PROGRAMLISTING"><font color="#000000"> printw(string); /* Print on stdscr at present cursor position */ mvprintw(y, x, string);/* Move to (y, x) then print string */ wprintw(win, string); /* Print on window win at present cursor position */ /* in the window */ mvwprintw(win, y, x, string); /* Move to (y, x) relative to window */ /* co-ordinates and then print */</font></pre></td></tr></table><p>Usually the w-less functions are macros which expand tocorresponding w-function with stdscr as the window parameter.</p></div><div class="SECT1"><hr><h2 class="SECT1"><a name="PRINTW" id="PRINTW">6. Outputfunctions</a></h2><p>I guess you can't wait any more to see some action. Back to ourodyssey of curses functions. Now that curses is initialized, let'sinteract with world.</p><p>There are three classes of functions which you can use to dooutput on screen.</p><ol type="1"><li><p>addch() class: Print single character with attributes</p></li><li><p>printw() class: Print formatted output similar to printf()</p></li><li><p>addstr() class: Print strings</p></li></ol><p>These functions can be used interchangeably and it's a matter ofstyle as to which class is used. Let's see each one in detail.</p><div class="SECT2"><hr><h3 class="SECT2"><a name="ADDCHCLASS" id="ADDCHCLASS">6.1. addch()class of functions</a></h3><p>These functions put a single character into the current cursorlocation and advance the position of the cursor. You can give thecharacter to be printed but they usually are used to print acharacter with some attributes. Attributes are explained in detailin later <a href="#ATTRIB">sections</a> of the document. If acharacter is associated with an attribute(bold, reverse videoetc.), when curses prints the character, it is printed in thatattribute.</p><p>In order to combine a character with some attributes, you havetwo options:</p><ul><li><p>By OR'ing a single character with the desired attribute macros.These attribute macros could be found in the header file<var class="LITERAL">ncurses.h</var>. For example, you want toprint a character ch(of type char) bold and underlined, you wouldcall addch() as below.</p><table border="0" bgcolor="#E0E0E0" width="90%"><tr><td><pre class="PROGRAMLISTING"><font color="#000000"> addch(ch | A_BOLD | A_UNDERLINE);</font></pre></td></tr></table></li><li><p>By using functions like <var class="LITERAL">attrset(),attron(),attroff()</var>. These functions areexplained in the <a href="#ATTRIB">Attributes</a> section. Briefly,they manipulate the current attributes of the given window. Onceset, the character printed in the window are associated with theattributes until it is turned off.</p></li></ul><p>Additionally, <var class="LITERAL">curses</var> provides somespecial characters for character-based graphics. You can drawtables, horizontal or vertical lines, etc. You can find allavaliable characters in the header file <var class="LITERAL">ncurses.h</var>. Try looking for macros beginning with<var class="LITERAL">ACS_</var> in this file.</p></div><div class="SECT2"><hr><h3 class="SECT2"><a name="AEN298" id="AEN298">6.2. mvaddch(),waddch() and mvwaddch()</a></h3><p><var class="LITERAL">mvaddch()</var> is used to move the cursorto a given point, and then print. Thus, the calls:</p><table border="0" bgcolor="#E0E0E0" width="100%"><tr><td><pre class="PROGRAMLISTING"><font color="#000000"> move(row,col); /* moves the cursor to row<em>th</em> row and col<em>th</em> column */ addch(ch);</font></pre></td></tr></table>can be replaced by<table border="0" bgcolor="#E0E0E0" width="100%"><tr><td><pre class="PROGRAMLISTING"><font color="#000000"> mvaddch(row,col,ch);</font></pre></td></tr></table><p><var class="LITERAL">waddch()</var> is similar to <var class="LITERAL">addch()</var>, except that it adds a character into thegiven window. (Note that <var class="LITERAL">addch()</var> adds acharacter into the window <var class="LITERAL">stdscr</var>.)</p><p>In a similar fashion <var class="LITERAL">mvwaddch()</var>function is used to add a character into the given window at thegiven coordinates.</p><p>Now, we are familiar with the basic output function <var class="LITERAL">addch()</var>. But, if we want to print a string, itwould be very annoying to print it character by character.Fortunately, <var class="LITERAL">ncurses</var> provides<var class="LITERAL">printf</var><em>-like</em> or <var class="LITERAL">puts</var><em>-like</em> functions.</p></div><div class="SECT2"><hr><h3 class="SECT2"><a name="PRINTWCLASS" id="PRINTWCLASS">6.3.printw() class of functions</a></h3><p>These functions are similar to <var class="LITERAL">printf()</var> with the added capability of printing atany position on the screen.</p><div class="SECT3"><hr><h4 class="SECT3"><a name="PRINTWMVPRINTW" id="PRINTWMVPRINTW">6.3.1. printw() and mvprintw</a></h4><p>These two functions work much like <var class="LITERAL">printf()</var>. <var class="LITERAL">mvprintw()</var> canbe used to move the cursor to a position and then print. If youwant to move the cursor first and then print using <var class="LITERAL">printw()</var> function, use <var class="LITERAL">move()</var> first and then use <var class="LITERAL">printw()</var> though I see no point why one should avoidusing <var class="LITERAL">mvprintw()</var>, you have theflexibility to manipulate.</p></div><div class="SECT3"><hr><h4 class="SECT3"><a name="WPRINTWMVWPRINTW" id="WPRINTWMVWPRINTW">6.3.2. wprintw() and mvwprintw</a></h4><p>These two functions are similar to above two except that theyprint in the corresponding window given as argument.</p></div><div class="SECT3"><hr><h4 class="SECT3"><a name="VWPRINTW" id="VWPRINTW">6.3.3.vwprintw()</a></h4><p>This function is similar to <var class="LITERAL">vprintf()</var>. This can be used when variable number ofarguments are to be printed.</p></div><div class="SECT3"><hr><h4 class="SECT3"><a name="SIMPLEPRINTWEX" id="SIMPLEPRINTWEX">6.3.4. A Simple printw example</a></h4><div class="EXAMPLE"><a name="BPREX" id="BPREX"></a><p><b>Example 3. A Simple printw example</b></p><table border="0" bgcolor="#E0E0E0" width="100%"><tr><td><pre class="PROGRAMLISTING"><font color="#000000"><span class="INLINEMEDIAOBJECT">#include <ncurses.h> /* ncurses.h includes stdio.h */ #include <string.h> int main(){ char mesg[]="Just a string"; /* message to be appeared on the screen */ int row,col; /* to store the number of rows and * * the number of colums of the screen */ initscr(); /* start the curses mode */ getmaxyx(stdscr,row,col); /* get the number of rows and columns */ mvprintw(row/2,(col-strlen(mesg))/2,"%s",mesg); /* print the message at the center of the screen */ mvprintw(row-2,0,"This screen has %d rows and %d columns\n",row,col); printw("Try resizing your window(if possible) and then run this program again"); refresh(); getch(); endwin(); return 0;}</span></font></pre></td></tr></table></div><p>Above program demonstrates how easy it is to use <var class="LITERAL">printw</var>. You just feed the coordinates and themessage to be appeared on the screen, then it does what youwant.</p><p>The above program introduces us to a new function <var class="LITERAL">getmaxyx()</var>, a macro defined in <var class="LITERAL">ncurses.h</var>. It gives the number of columns and thenumber of rows in a given window. <var class="LITERAL">getmaxyx()</var> does this by updating the variablesgiven to it. Since <var class="LITERAL">getmaxyx()</var> is not afunction we don't pass pointers to it, we just give two integervariables.</p></div></div><div class="SECT2"><hr><h3 class="SECT2"><a name="ADDSTRCLASS" id="ADDSTRCLASS">6.4.addstr() class of functions</a></h3><p><var class="LITERAL">addstr()</var> is used to put a characterstring into a given window. This function is similar to calling<var class="LITERAL">addch()</var> once for each character in agiven string. This is true for all output functions. There areother functions from this family such as <var class="LITERAL">mvaddstr(),mvwaddstr()</var> and <var class="LITERAL">waddstr()</var>, which obey the naming convention ofcurses.(e.g. mvaddstr() is similar to the respective calls move()and then addstr().) Another function of this family is addnstr(),which takes an integer parameter(say n) additionally. This functionputs at most n characters into the screen. If n is negative, thenthe entire string will be added.</p></div><div class="SECT2"><hr><h3 class="SECT2"><a name="ACAUTION" id="ACAUTION">6.5. A word ofcaution</a></h3><p>All these functions take y co-ordinate first and then x in theirarguments. A common mistake by beginners is to pass x,y in thatorder. If you are doing too many manipulations of (y,x)co-ordinates, think of dividing the screen into windows andmanipulate each one separately. Windows are explained in the<a href="#WINDOWS">windows</a> section.</p></div></div><div class="SECT1"><hr><h2 class="SECT1"><a name="SCANW" id="SCANW">7. Inputfunctions</a></h2><p>Well, printing without taking input, is boring. Let's seefunctions which allow us to get input from user. These functionsalso can be divided into three categories.</p><ol type="1"><li><p>getch() class: Get a character</p></li><li><p>scanw() class: Get formatted input</p></li><li><p>getstr() class: Get strings</p></li></ol><div class="SECT2"><hr><h3 class="SECT2"><a name="GETCHCLASS" id="GETCHCLASS">7.1. getch()class of functions</a></h3><p>These functions read a single character from the terminal. Butthere are several subtle facts to consider. For example if youdon't use the function cbreak(), curses will not read your inputcharacters contiguously but will begin read them only after a newline or an EOF is encountered. In order to avoid this, the cbreak()function must used so that characters are immediately available toyour program. Another widely used function is noecho(). As the namesuggests, when this function is set (used), the characters that arekeyed in by the user will not show up on the screen. The twofunctions cbreak() and noecho() are typical examples of keymanagement. Functions of this genre are explained in the <a href="#KEYS">key management section</a> .</p>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -