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 75
|
//My first program in Curses
//Create a program, which allows you to move window using arrows
//After each movement window shows coordinates of its 4 corners in itself.
#include <curses.h>
WINDOW* create_newwin(int height, int width, int starty, int startx);
void destroy_window(WINDOW* targetwin);
int main() {
WINDOW* okno;
int input;
int y,x;
int hei, wid;
int winx, winy;
bool doloop = true;
initscr();
noecho();
cbreak();
keypad(stdscr, TRUE);
hei = 7;
wid = 45;
y = (LINES - hei)/2;
x = (COLS - wid)/2;
refresh();
while(doloop) {
refresh();
okno = create_newwin(hei, wid, y, x);
getbegyx(okno, winy, winx);
attron(A_REVERSE);
mvwprintw(okno, 1, 2, "My top left corner coordinates: %d, %d!", winy, winx);
mvwprintw(okno, 2, 2, "My top right corner coordinates: %d, %d!", winy, winx+wid);
mvwprintw(okno, 3, 2, "My bottom left corner coordinates: %d, %d!", winy+hei, winx);
mvwprintw(okno, 4, 2, "My bottom right corner coordinates: %d, %d!", winy+hei, winx+wid);
attroff(A_REVERSE);
input = getch();
switch(input) {
case KEY_LEFT:
x-=1;
break;
case KEY_RIGHT:
x+=1;
break;
case KEY_UP:
y-=1;
break;
case KEY_DOWN:
y+=1;
break;
case 10:
doloop = false;
break;
}
destroy_window(okno);
}
endwin();
return 0;
}
WINDOW* create_newwin(int height, int width, int starty,int startx) {
WINDOW* local_win;
local_win = newwin(height, width, starty, startx);
box(local_win, 0, 0);
wrefresh(local_win);
return local_win;
}
void destroy_window(WINDOW* targetwin){
wborder(targetwin, ' ', ' ', ' ',' ',' ',' ',' ',' ');//clear space after window
wrefresh(targetwin);//refresh it
delwin(targetwin);
}
|