Basic window operations

This program introduces the window operations ncurses makes possible. It simply shows a window in the centre of the screen, and allows the user to move it around using the cursor keys, and the HOME and END keys.

Programming Issues

After initializing ncurses, switching off echo and enabling the keypad, we create a new window using subwin(), specifying our main window as the parent. We give this window a border using the box() function, and add some text to it using mvwaddstr(). The coordinates passed to mvwaddstr()> are now relative to the origin of our new window, and not to the screen.

We then use getch() to get some keypresses from the user. We catch the cursor keys, HOME and END, and modify the coordinates of our window accordingly. Then, we call mvwin() to move it. Note that we do not need to call refresh() after moving our window.

Finally we clean up. Notice that we call delwin() on our subwindow before calling it on the parent.

Usage

Link with the ncurses library. The ncurses "Hello, World!" example includes details on how to do this.

Source and Downloads