• Ncurses-窗口


    前面介绍过标准屏幕 stdscr, stdscr 只是 WINDOW 结构的一个特例。

    我们可以使用函数 newwin 和 delwin 来创建和销毁窗口

    WINDOW *newwin(int num_of_lines, int num_of_cols, int start_y, int start_x);
    int delwin(WINDOW *window_to_delete);

    newwin 函数的作用是创建一个新窗口,该窗口从屏幕位置(start_y,start_x)开始,行数和列数分别由参数 num_of_lines 和 num_of_cols 指定。返回一个指向新窗口的指针,如果新窗口创建失败则返回 null。所有的窗口范围必须在当前屏幕范围之内,如果新窗口的任何部分落在当前屏幕范围之外,则 newwin 函数调用失败。通过 newwin 函数创建的新窗口完全独立于所有已存在的窗口。默认情况下,它被放置在任何已有窗口之上,覆盖(但不是改变)它们的内容。
    delwin 函数的作用是删除之前通过 newwin 函数创建的窗口。因为调用 newwin 函数可能会给新窗口分配内存,所以当不再需要这些窗口时,不要忘记通过 delwin 函数将其删除。

    注:不要尝试删除 curses 自己的窗口 stdscr 和curscr!!!

    通用函数

    前缀 w 用于窗口,mv 用于光标移动,mvw 用于在窗口中移动光标

    int addch(const chtype char);
    int waddch(WINDOW *window_pointer, const chtype char);
    int mvaddch(int y, int x, const chtype char);
    int mvwaddch(WINDOW *window_pointer, int y, int x, const chtype char);
    
    int printw(char *format, ...);
    int wprintw(WINDOW *window_pointer, char *format, ...);
    int mvprintw(int y, int x, char *format, ...);
    int mvwprintw(WINDOW *window_pointer, int y, int x, char *format, ...);
    
    int mvwin(WINDOW *window_to_move, int new_y, int new_x);
    int wrefresh(WINDOW *window_pointer);
    int wclear(WINDOW *window_pointer);
    int werase(WINDOW *window_pointer);
    int touchwin(WINDOW *window_pointer);
    int scrollok(WINDOW *window_pointer, bool scroll_flag);
    int scroll(WINDOW *window_pointer);


    示例:

     1 #include <ncurses.h>
     2 
     3 WINDOW* create_newwin(int height, int width, int starty, int startx)
     4 {
     5     WINDOW* local_win = NULL;
     6     local_win = newwin(height, width, starty, startx);
     7     box(local_win, 0, 0);
     8     wrefresh(local_win);
     9     return local_win;
    10 }
    11 
    12 void move_win(WINDOW* local_win, int y, int x)
    13 {
    14     wborder(local_win, ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '); // clear border for old win
    15     wrefresh(local_win);
    16 
    17     mvwin(local_win, y, x);
    18     box(local_win, 0, 0); // reset border
    19     wrefresh(local_win);
    20 }
    21 
    22 int main()
    23 {
    24     WINDOW* my_win = NULL;
    25     int startx = 0, starty = 0, width = 10, height = 3;
    26     int ch = 0;
    27     initscr();
    28     cbreak();
    29     keypad(stdscr, TRUE);
    30     starty = (LINES - height) / 2;
    31     startx = (COLS - width) / 2;
    32     printw("Press F1 to exit");
    33     refresh();
    34     my_win = create_newwin(height, width, starty, startx);
    35 
    36     while ((ch = getch()) != KEY_F(1)) {
    37         switch(ch) {
    38             case KEY_LEFT:
    39                 move_win(my_win, starty, --startx);
    40                 break;
    41             case KEY_RIGHT:
    42                 move_win(my_win, starty, ++startx);
    43                 break;
    44             case KEY_UP:
    45                 move_win(my_win, --starty, startx);
    46                 break;
    47             case KEY_DOWN:
    48                 move_win(my_win, ++starty, startx);
    49                 break;
    50         }
    51     }
    52 
    53     if (my_win) {
    54         delwin(my_win);
    55     }
    56     endwin();
    57     return 0;
    58 }
  • 相关阅读:
    IOS 网络编程 + 后台保持连接
    关于 UIWindow
    IOS 企业级应用无线部署 详解
    Appium+python自动化(二十四)- 白素贞千年等一回许仙
    Appium+python自动化(二十三)- 真假美猴王Monkeyrunner与Monkey傻傻的分不清楚(超详解)
    Appium+python自动化(二十二)- 三个臭皮匠助你成就一番霸业-控件坐标获取(超详解)
    Appium+python自动化(二十)- 猴哥失散多年的混血弟弟还是妹妹- Monkey(猴子)日志(超详解)
    java接口自动化(一)
    Appium+python自动化(十九)- 猴哥失散多年的混血弟弟还是妹妹- Monkey(猴子)参数(超详解)
    Appium+python自动化(十八)- 你难道是猴哥失散多年的混血弟弟还是妹妹???- Monkey事件(超详解)
  • 原文地址:https://www.cnblogs.com/jingyg/p/5731967.html
Copyright © 2020-2023  润新知