In this tutorial, I will show a source code that creates a border and a taskbar with three tasks assigned to keys F5, F6, and F7. This source code has two main areas: win and taskbar_win. Both pointers are good and each of these is defined for each window. The source code is commented on for each step.
Let’s see the source code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 | #include <ncurses.h> int main() { // Initialize ncurses initscr(); // Initialize the color pairs start_color(); init_pair(1, COLOR_BLUE, COLOR_WHITE); // Turn off input echoing and enable special keys cbreak(); noecho(); keypad(stdscr, TRUE); // Create a window that takes up the entire screen WINDOW *win = newwin(LINES, COLS, 0, 0); // Add a border to the window using ASCII characters box(win, ACS_VLINE, ACS_HLINE); // Define the taskbar WINDOW* taskbar_win = newwin(1, COLS-2, LINES-2, 1); // Enable scrolling for the taskbar scrollok(taskbar_win, TRUE); // Set the taskbar color wbkgd(taskbar_win, COLOR_PAIR(1)); // Draw the taskbar wmove(taskbar_win, -1, 0); wprintw(taskbar_win, "[F5] Task 1 | [F6] Task 2 | [F7] Task 3"); // Refresh the screen to show changes refresh(); // Refresg the win wrefresh(win); // Refresh the taskbar wrefresh(taskbar_win); // Wait for user input getch(); // Wait for user input int ch; do { ch = getch(); switch(ch) { case KEY_F(5): // Do something for Task 1 break; case KEY_F(6): // Do something for Task 2 break; case KEY_F(7): // Do something for Task 3 break; default: // Handle other key presses break; } } // If 'q' key then quit while (ch != 'q'); // Clean up delwin(win); endwin(); return 0; } |
The result is this: