There are three classes of functions which you can use to do output on screen.
These functions put a single character into the current cursor location and advance the position of the cursor. You can give the character to be printed but they usually are used to print a character with some attributes. Attributes are explained in detail in later sections of the document. If a character is associated with an attribute(bold, reverse video etc.), when curses prints the character, it is printed in that attribute.
In order to combine a character with some attributes, you have two options:
By OR'ing a single character with the desired attribute macros. These attribute macros could be found in the header file ncurses.h. For example, you want to print a character ch(of type char) bold and underlined, you would call addch() as below.
addch(ch | A_BOLD | A_UNDERLINE); |
By using functions like attrset(),attron(),attroff(). These functions are explained in the Attributes section. Briefly, they manipulate the current attributes of the given window. Once set, the character printed in the window are associated with the attributes until it is turned off.
Additionally, curses provides some special characters for character-based graphics. You can draw tables, horizontal or vertical lines, etc. You can find all avaliable characters in the header file ncurses.h. Try looking for macros beginning with ACS_ in this file.
mvaddch() is used to move the cursor to a given point, and then print. Thus, the calls:
move(row,col); /* moves the cursor to rowth row and colth column */ addch(ch); |
mvaddch(row,col,ch); |
Example 3. A Simple printw example
#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;
} |
All these functions take y co-ordinate first and then x in their arguments. A common mistake by beginners is to pass x,y in that order. If you are doing too many manipulations of (y,x) co-ordinates, think of dividing the screen into windows and manipulate each one separately. Windows are explained in the windows section.
Закладки на сайте Проследить за страницей |
Created 1996-2025 by Maxim Chirkov Добавить, Поддержать, Вебмастеру |