⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ncurses-programming-howto.html

📁 ncurses 库 可能有用酒用 没用就算了 我觉得还可以用
💻 HTML
📖 第 1 页 / 共 5 页
字号:
</div><div class="SECT2"><hr><h3 class="SECT2"><a name="SCANWCLASS" id="SCANWCLASS">7.2. scanw()class of functions</a></h3><p>These functions are similar to <var class="LITERAL">scanf()</var> with the added capability of getting theinput from any location on the screen.</p><div class="SECT3"><hr><h4 class="SECT3"><a name="SCANWMVSCANW" id="SCANWMVSCANW">7.2.1.scanw() and mvscanw</a></h4><p>The usage of these functions is similar to that of <var class="LITERAL">sscanf()</var>, where the line to be scanned is providedby <var class="LITERAL">wgetstr()</var> function. That is, thesefunctions call to <var class="LITERAL">wgetstr()</var>function(explained below) and uses the resulting line for ascan.</p></div><div class="SECT3"><hr><h4 class="SECT3"><a name="WSCANWMVWSCANW" id="WSCANWMVWSCANW">7.2.2. wscanw() and mvwscanw()</a></h4><p>These are similar to above two functions except that they readfrom a window, which is supplied as one of the arguments to thesefunctions.</p></div><div class="SECT3"><hr><h4 class="SECT3"><a name="VWSCANW" id="VWSCANW">7.2.3.vwscanw()</a></h4><p>This function is similar to <var class="LITERAL">vscanf()</var>.This can be used when a variable number of arguments are to bescanned.</p></div></div><div class="SECT2"><hr><h3 class="SECT2"><a name="GETSTRCLASS" id="GETSTRCLASS">7.3.getstr() class of functions</a></h3><p>These functions are used to get strings from the terminal. Inessence, this function performs the same task as would be achievedby a series of calls to <var class="LITERAL">getch()</var> until anewline, carriage return, or end-of-file is received. The resultingstring of characters are pointed to by <var class="LITERAL">str</var>, which is a character pointer provided by theuser.</p></div><div class="SECT2"><hr><h3 class="SECT2"><a name="GETSTREX" id="GETSTREX">7.4. Someexamples</a></h3><div class="EXAMPLE"><a name="BSCEX" id="BSCEX"></a><p><b>Example 4. A Simple scanw example</b></p><table border="0" bgcolor="#E0E0E0" width="100%"><tr><td><pre class="PROGRAMLISTING"><font color="#000000"><span class="INLINEMEDIAOBJECT">#include &lt;ncurses.h&gt;                   /* ncurses.h includes stdio.h */  #include &lt;string.h&gt;  int main(){ char mesg[]="Enter a string: ";                /* message to be appeared on the screen */ char str[80]; 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 */ getstr(str); mvprintw(LINES - 2, 0, "You Entered: %s", str); getch(); endwin(); return 0;}</span></font></pre></td></tr></table></div></div></div><div class="SECT1"><hr><h2 class="SECT1"><a name="ATTRIB" id="ATTRIB">8.Attributes</a></h2><p>We have seen an example of how attributes can be used to printcharacters with some special effects. Attributes, when setprudently, can present information in an easy, understandablemanner. The following program takes a C file as input and printsthe file with comments in bold. Scan through the code.</p><div class="EXAMPLE"><a name="BSIAT" id="BSIAT"></a><p><b>Example 5. A Simple Attributes example</b></p><table border="0" bgcolor="#E0E0E0" width="100%"><tr><td><pre class="PROGRAMLISTING"><font color="#000000"><span class="INLINEMEDIAOBJECT">/* pager functionality by Joseph Spainhour" &lt;spainhou@bellsouth.net&gt; */#include &lt;ncurses.h&gt;#include &lt;stdlib.h&gt;int main(int argc, char *argv[]){   int ch, prev, row, col;  prev = EOF;  FILE *fp;  int y, x;  if(argc != 2)  {    printf("Usage: %s &lt;a c file name&gt;\n", argv[0]);    exit(1);  }  fp = fopen(argv[1], "r");  if(fp == NULL)  {    perror("Cannot open input file");    exit(1);  }  initscr();                            /* Start curses mode */  getmaxyx(stdscr, row, col);           /* find the boundaries of the screeen */  while((ch = fgetc(fp)) != EOF)        /* read the file till we reach the end */  {    getyx(stdscr, y, x);                /* get the current curser position */    if(y == (row - 1))                  /* are we are at the end of the screen */    {      printw("&lt;-Press Any Key-&gt;");      /* tell the user to press a key */      getch();      clear();                          /* clear the screen */      move(0, 0);                       /* start at the beginning of the screen */    }    if(prev == '/' &amp;&amp; ch == '*')        /* If it is / and * then only                                         * switch bold on */        {      attron(A_BOLD);                   /* cut bold on */      getyx(stdscr, y, x);              /* get the current curser position */      move(y, x - 1);                   /* back up one space */      printw("%c%c", '/', ch);          /* The actual printing is done here */    }    else      printw("%c", ch);    refresh();    if(prev == '*' &amp;&amp; ch == '/')      attroff(A_BOLD);                  /* Switch it off once we got *                                         * and then / */    prev = ch;  }  endwin();                             /* End curses mode */  fclose(fp);  return 0;}</span></font></pre></td></tr></table></div><p>Don't worry about all those initialization and other crap.Concentrate on the while loop. It reads each character in the fileand searches for the pattern /*. Once it spots the pattern, itswitches the BOLD attribute on with <var class="LITERAL">attron()</var> . When we get the pattern */ it isswitched off by <var class="LITERAL">attroff()</var> .</p><p>The above program also introduces us to two useful functions<var class="LITERAL">getyx()</var> and <var class="LITERAL">move()</var>. The first function gets the co-ordinates ofthe present cursor into the variables y, x. Since getyx() is amacro we don't have to pass pointers to variables. The function<var class="LITERAL">move()</var> moves the cursor to theco-ordinates given to it.</p><p>The above program is really a simple one which doesn't do much.On these lines one could write a more useful program which reads aC file, parses it and prints it in different colors. One could evenextend it to other languages as well.</p><div class="SECT2"><hr><h3 class="SECT2"><a name="ATTRIBDETAILS" id="ATTRIBDETAILS">8.1.The details</a></h3><p>Let's get into more details of attributes. The functions<var class="LITERAL">attron(), attroff(), attrset()</var> , andtheir sister functions <var class="LITERAL">attr_get()</var> etc..can be used to switch attributes on/off , get attributes andproduce a colorful display.</p><p>The functions attron and attroff take a bit-mask of attributesand switch them on or off, respectively. The following videoattributes, which are defined in &lt;curses.h&gt; can be passed tothese functions.</p><table border="0" bgcolor="#E0E0E0" width="100%"><tr><td><pre class="PROGRAMLISTING"><font color="#000000">        A_NORMAL        Normal display (no highlight)    A_STANDOUT      Best highlighting mode of the terminal.    A_UNDERLINE     Underlining    A_REVERSE       Reverse video    A_BLINK         Blinking    A_DIM           Half bright    A_BOLD          Extra bright or bold    A_PROTECT       Protected mode    A_INVIS         Invisible or blank mode    A_ALTCHARSET    Alternate character set    A_CHARTEXT      Bit-mask to extract a character    COLOR_PAIR(n)   Color-pair number n     </font></pre></td></tr></table><p>The last one is the most colorful one :-) Colors are explainedin the <a href="#color" target="_top">next sections</a>.</p><p>We can OR(|) any number of above attributes to get a combinedeffect. If you wanted reverse video with blinking characters youcan use</p><table border="0" bgcolor="#E0E0E0" width="100%"><tr><td><pre class="PROGRAMLISTING"><font color="#000000">    attron(A_REVERSE | A_BLINK);</font></pre></td></tr></table></div><div class="SECT2"><hr><h3 class="SECT2"><a name="ATTRONVSATTRSET" id="ATTRONVSATTRSET">8.2. attron() vs attrset()</a></h3><p>Then what is the difference between attron() and attrset()?attrset sets the attributes of window whereas attron just switcheson the attribute given to it. So attrset() fully overrides whateverattributes the window previously had and sets it to the newattribute(s). Similarly attroff() just switches off theattribute(s) given to it as an argument. This gives us theflexibility of managing attributes easily.But if you use themcarelessly you may loose track of what attributes the window hasand garble the display. This is especially true while managingmenus with colors and highlighting. So decide on a consistentpolicy and stick to it. You can always use <var class="LITERAL">standend()</var> which is equivalent to <var class="LITERAL">attrset(A_NORMAL)</var> which turns off all attributesand brings you to normal mode.</p></div><div class="SECT2"><hr><h3 class="SECT2"><a name="ATTR_GET" id="ATTR_GET">8.3.attr_get()</a></h3><p>The function attr_get() gets the current attributes and colorpair of the window. Though we might not use this as often as theabove functions, this is useful in scanning areas of screen. Say wewanted to do some complex update on screen and we are not sure whatattribute each character is associated with. Then this function canbe used with either attrset or attron to produce the desiredeffect.</p></div><div class="SECT2"><hr><h3 class="SECT2"><a name="ATTR_FUNCS" id="ATTR_FUNCS">8.4. attr_functions</a></h3><p>There are series of functions like attr_set(), attr_on etc..These are similar to above functions except that they takeparameters of type <var class="LITERAL">attr_t</var>.</p></div><div class="SECT2"><hr><h3 class="SECT2"><a name="WATTRFUNCS" id="WATTRFUNCS">8.5. wattrfunctions</a></h3><p>For each of the above functions we have a corresponding functionwith 'w' which operates on a particular window. The above functionsoperate on stdscr.</p></div><div class="SECT2"><hr><h3 class="SECT2"><a name="CHGAT" id="CHGAT">8.6. chgat()functions</a></h3><p>The function chgat() is listed in the end of the man pagecurs_attr. It actually is a useful one. This function can be usedto set attributes for a group of characters without moving. I meanit !!! without moving the cursor :-) It changes the attributes of agiven number of characters starting at the current cursorlocation.</p><p>We can give -1 as the character count to update till end ofline. If you want to change attributes of characters from currentposition to end of line, just use this.</p><table border="0" bgcolor="#E0E0E0" width="100%"><tr><td><pre class="PROGRAMLISTING"><font color="#000000">    chgat(-1, A_REVERSE, 0, NULL);</font></pre></td></tr></table><p>This function is useful when changing attributes for charactersthat are already on the screen. Move to the character from whichyou want to change and change the attribute.</p><p>Other functions wchgat(), mvchgat(), wchgat() behave similarlyexcept that the w functions operate on the particular window. Themv functions first move the cursor then perform the work given tothem. Actually chgat is a macro which is replaced by a wchgat()with stdscr as the window. Most of the "w-less" functions aremacros.</p><div class="EXAMPLE"><a name="BWICH" id="BWICH"></a><p><b>Example 6. Chgat() Usage example</b></p><table border="0" bgcolor="#E0E0E0" width="100%"><tr><td><pre class="PROGRAMLISTING"><font color="#000000"><span class="INLINEMEDIAOBJECT">#include &lt;ncurses.h&gt;int main(int argc, char *argv[]){       initscr();                      /* Start curses mode            */        start_color();                  /* Start color functionality    */                init_pair(1, COLOR_CYAN, COLOR_BLACK);        printw("A Big string which i didn't care to type fully ");        mvchgat(0, 0, -1, A_BLINK, 1, NULL);            /*          * First two parameters specify the position at which to start          * Third parameter number of characters to update. -1 means till          * end of line         * Forth parameter is the normal attribute you wanted to give          * to the charcter         * Fifth is the color index. It is the index given during init_pair()         * use 0 if you didn't want color         * Sixth one is always NULL          */        refresh();        getch();        endwin();                       /* End curses mode                */        return 0;}</span></font></pre></td></tr></table></d

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -