1 #include<stdio.h> 2 #include<curses.h> 3 #include<stdlib.h> 4 #include<unistd.h> 5 #include<string.h> 6 #define PW_LEN 256 7 #define NAME_LEN 256 8 int main() 9 { 10 initscr(); 11 move(5,10); 12 attron(A_BLINK); 13 printw("%s","hello linux"); 14 attroff(A_BLINK); 15 refresh(); 16 sleep(1); 17 move(8,10); 18 attron(A_BOLD); 19 addstr("hello c/c++"); 20 attroff(A_BOLD); 21 refresh(); 22 sleep(1); 23 move(10,15); 24 attron(A_STANDOUT); 25 printw("%s","test and for fun"); 26 attroff(A_STANDOUT); 27 refresh(); 28 sleep(1); 29 30 int i=0; 31 const char *read_password="test"; 32 char name [NAME_LEN]; 33 char password[PW_LEN]; 34 move(15,15); 35 printw("%s","login:"); 36 while(true) 37 { 38 i=0; 39 move(16,15); 40 printw("%s","user name:"); 41 getstr(name); 42 43 move(17,15); 44 printw("%s","user password:"); 45 refresh(); 46 cbreak(); //非默认输入模式 47 noecho(); //关闭回显功能 48 memset(password,‘\0‘,sizeof(password)); 49 50 while(i<PW_LEN) 51 { 52 password[i]=getch(); 53 if(password[i]==‘\n‘) 54 break; 55 move(17,30+i); 56 addch(‘*‘); 57 refresh(); 58 i++; 59 } 60 61 echo(); 62 nocbreak(); 63 move(18,15); 64 65 if(strncmp(read_password,password,strlen(read_password))==0) 66 { 67 printw("%s","correct"); 68 break; 69 } 70 else 71 { 72 addstr("wroing"); 73 sleep(2); 74 clear(); 75 } 76 refresh(); 77 } 78 79 clear(); 80 //创建新的窗口 81 82 int loop_x; 83 int loop_y; 84 WINDOW *new_window_ptr; 85 WINDOW *popup_window_ptr; 86 char a_letter=‘a‘; 87 mvprintw(5,5,"%s","the multiple windows"); 88 refresh(); 89 for(loop_x=0;loop_x<LINES-1;loop_x++) 90 { 91 for(loop_y=0;loop_y<COLS-1;loop_y++) 92 { 93 94 mvwaddch(stdscr,loop_x,loop_y,a_letter); 95 a_letter++; 96 if(a_letter>‘z‘) 97 a_letter=‘a‘; 98 } 99 } 100 refresh(); 101 sleep(2); 102 103 new_window_ptr=newwin(30,20,5,5); 104 mvwprintw(new_window_ptr,2,2,"%s","hello my gril"); 105 wrefresh(new_window_ptr); //对新建窗口进行刷新 106 sleep(2); 107 108 a_letter=‘0‘; 109 for(loop_x=0;loop_x<LINES-1;loop_x++) 110 { 111 for(loop_y=0;loop_y<COLS-1;loop_y++) 112 { 113 114 mvwaddch(stdscr,loop_x,loop_y,a_letter); 115 a_letter++; 116 if(a_letter>‘9‘) 117 a_letter=‘0‘; 118 } 119 } 120 refresh(); 121 sleep(2); 122 wrefresh(new_window_ptr); //对新建窗口进行刷新 123 sleep(2); 124 125 touchwin(new_window_ptr); 126 wrefresh(new_window_ptr); 127 sleep(2); 128 129 popup_window_ptr=newwin(20,30,8,8); //再创建一个新的窗口 130 box(popup_window_ptr,‘|‘,‘-‘); 131 mvwprintw(popup_window_ptr,5,2,"%s","hello i am the pop_window"); 132 wrefresh(popup_window_ptr); 133 sleep(2); 134 135 //对两新的窗口不断的进行输出 136 touchwin(new_window_ptr); 137 wrefresh(new_window_ptr); 138 sleep(2); 139 wclear(new_window_ptr); 140 wrefresh(new_window_ptr); 141 sleep(2); 142 delwin(new_window_ptr); 143 touchwin(popup_window_ptr); 144 wrefresh(popup_window_ptr); 145 sleep(2); 146 wclear(popup_window_ptr); 147 wrefresh(popup_window_ptr); 148 sleep(2); 149 delwin(popup_window_ptr); 150 touchwin(stdscr); 151 refresh(); 152 sleep(2); 153 154 endwin(); 155 exit(EXIT_SUCCESS); 156 }
这个程序是对键盘输入和对创建多窗口的一个例子。
时间: 2024-10-10 18:34:14