这儿把窗口分成了4个区域,欢迎词条头部,输出聊天记录,好友列表,输入框。由于输出聊天记录与好友列表需用到其它目录(内存池)下的内容,我这儿是先把它单独编译,所以还无法实现其功能,之后整合后会补充。
此外,这儿还用到了另一开源库ncurses,可自行下载。
chat_window.h
1 #include<iostream> 2 #include<string> 3 #include<string.h> 4 #include<ncurses.h> 5 6 #define SIZE 1024 7 8 class chat_window 9 { 10 public: 11 chat_window(); 12 ~chat_window(); 13 void create_header(); 14 void create_output(); 15 void create_friendList(); 16 void create_input(); 17 WINDOW* create_newwin(int height,int width,int starty,int startx); 18 WINDOW* getHeader(); 19 WINDOW* getOutput(); 20 WINDOW* getFriendList(); 21 WINDOW* getInput(); 22 void fflush_win(WINDOW* p); 23 void clear_win_line(WINDOW* p,int begin,int lines); 24 void put_str_to_win(WINDOW* win,int starty,int startx,const std::string& msg); 25 std::string get_str_from_win(WINDOW* win); 26 void* drawHeader(void* arg); 27 void* drawOutput(void* arg); 28 void* drawFriendList(void* arg); 29 void* drawInput(void* arg); 30 //public: 31 private: 32 WINDOW* header; 33 WINDOW* output; 34 WINDOW* friendList; 35 WINDOW* input; 36 };
chat_window.cpp
1 #include "chat_window.h" 2 3 chat_window::chat_window() 4 { 5 initscr(); 6 } 7 chat_window::~chat_window() 8 { 9 // destory_win(getHeader());//本来是打算封装一个函数来清除窗口,后来直接调用删除窗口的函数,没有封装 10 // destory_win(getOutput()); 11 // destory_win(getFriendList()); 12 // destory_win(getInput); 13 14 delwin(getHeader()); 15 delwin(getOutput()); 16 delwin(getFriendList()); 17 delwin(getInput()); 18 endwin(); 19 } 20 void chat_window::create_header() 21 { 22 int _h=LINES/5; 23 int _w=COLS; 24 int _y=0; 25 int _x=0; 26 27 header=create_newwin(_h,_w,_y,_x); 28 fflush_win(header); 29 } 30 void chat_window::create_output() 31 { 32 int _h=(LINES*3)/5; 33 int _w=(COLS*3)/4; 34 int _y=LINES/5; 35 int _x=0; 36 output=create_newwin(_h,_w,_y,_x); 37 fflush_win(getOutput()); 38 } 39 void chat_window::create_friendList() 40 { 41 int _h=(LINES*3)/5; 42 int _w=COLS/4; 43 int _y=LINES/5; 44 int _x=(COLS*3)/4; 45 friendList=create_newwin(_h,_w,_y,_x); 46 fflush_win(getFriendList()); 47 } 48 void chat_window::create_input() 49 { 50 int _h=LINES/5; 51 int _w=COLS; 52 int _y=(LINES*4)/5; 53 int _x=0; 54 input=create_newwin(_h,_w,_y,_x); 55 fflush_win(getInput()); 56 } 57 WINDOW* chat_window:: create_newwin(int height,int width,int starty,int star tx) 58 { 59 WINDOW* local_win=newwin(height,width,starty,startx); 60 box(local_win,0,0); 61 return local_win; 62 } 63 WINDOW* chat_window::getHeader() 64 { 65 return header; 66 } 67 WINDOW* chat_window::getOutput() 68 { 69 return output; 70 } 71 WINDOW* chat_window::getFriendList() 72 { 73 return friendList; 74 } 75 WINDOW* chat_window::getInput() 76 { 77 return input; 78 } 79 void chat_window::put_str_to_win(WINDOW* win,int starty,int startx,const std ::string& msg) 80 { 81 mvwaddstr(win,starty,startx,msg.c_str()); 82 } 83 std::string chat_window::get_str_from_win(WINDOW* win) 84 { 85 char msg[SIZE]; 86 memset(msg,‘\0‘,sizeof(msg)); 87 wgetnstr(win,msg,SIZE); 88 return msg; 89 } 90 void chat_window::fflush_win(WINDOW* p) 91 { 92 box(p,0,0); 93 wrefresh(p); 94 } 95 void chat_window::clear_win_line(WINDOW* p,int begin,int lines) 96 { 97 while(lines--) 98 { 99 wmove(p,begin++,0); 100 wclrtoeol(p); 101 } 102 } 103 void* drawHeader(void* arg) 104 { 105 chat_window* winp=(chat_window*)arg; 106 std::string _msg="Welcome!"; 107 winp->create_header(); 108 109 int _max_y,_max_x; 110 getmaxyx(winp->getHeader(),_max_y,_max_x); 111 int _y=_max_y/2; 112 int _x=0; 113 114 while(1) 115 { 116 winp->put_str_to_win(winp->getHeader(),_y,_x,_msg); 117 winp->fflush_win(winp->getHeader()); 118 usleep(100000); 119 winp->clear_win_line(winp->getHeader(),_y,2); 120 _x++; 121 _x%=_max_x; 122 } 123 } 124 void* drawOutput(void* arg) 125 { 126 chat_window* winp=(chat_window*)arg; 127 winp->create_output(); 128 while(1) 129 { 130 winp->fflush_win(winp->getOutput()); 131 usleep(1000000); 132 } 133 } 134 void* drawFriendList(void* arg) 135 { 136 chat_window* winp=(chat_window*)arg; 137 winp->create_friendList(); 138 while(1) 139 { 140 winp->fflush_win(winp->getFriendList()); 141 usleep(1000000); 142 } 143 } 144 void* drawInput(void* arg) 145 { 146 chat_window* winp=(chat_window*)arg; 147 winp->create_input(); 148 std::string _msg="Please input# "; 149 150 int _max_y,_max_x; 151 getmaxyx(winp->getInput(),_max_y,_max_x); 152 153 while(1) 154 { 155 winp->put_str_to_win(winp->getInput(),1,1,_msg); 156 winp->fflush_win(winp->getInput()); 157 winp->get_str_from_win(winp->getInput()); 158 usleep(1000000); 159 winp->clear_win_line(winp->getInput(),1,_max_y-2); 160 } 161 } 162 163 164 int main() 165 { 166 chat_window _win; 167 pthread_t th,to,tf,ti; 168 169 pthread_create(&th,NULL,drawHeader,(void*)&_win); 170 pthread_create(&to,NULL,drawOutput,(void*)&_win); 171 pthread_create(&tf,NULL,drawFriendList,(void*)&_win); 172 pthread_create(&ti,NULL,drawInput,(void*)&_win); 173 174 pthread_join(th,NULL); 175 pthread_join(to,NULL); 176 pthread_join(tf,NULL); 177 pthread_join(ti,NULL); 178 179 return 0; 180 }
makefile
1 chat_window:chat_window.cpp 2 g++ -o $@ $^ -lpthread -lncurses 3 .PHONY:clean 4 clean: 5 rm -f chat_window
输出结果:
原文地址:http://10707460.blog.51cto.com/10697460/1825642