From 13bbb29c90bc3c7a26f4b1b55ec46f3898818e99 Mon Sep 17 00:00:00 2001 From: xianya Date: Sun, 29 Dec 2024 13:20:38 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90=E6=AC=A2=E8=BF=8E=E7=95=8C?= =?UTF-8?q?=E9=9D=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- include/function.h | 2 +- include/ui.h | 11 +- main.c | 64 +++++++- ui.c | 367 +++++++++++++++++++++++---------------------- 4 files changed, 253 insertions(+), 191 deletions(-) diff --git a/include/function.h b/include/function.h index d8b1be0..49a7c76 100644 --- a/include/function.h +++ b/include/function.h @@ -11,5 +11,5 @@ int Login(); - +void myexit(); #endif //FUNCTION_H diff --git a/include/ui.h b/include/ui.h index e5f550a..3295fc8 100644 --- a/include/ui.h +++ b/include/ui.h @@ -4,14 +4,15 @@ #ifndef O_UI_H #define O_UI_H -void Login_ui(); +int Login_ui(); -void UserSignin_ui(); +int Signin_ui(); -void StudentsDesktop_ui(); +int StudentsDesktop_ui(); -void TeachersDesktop_ui(); +int TeachersDesktop_ui(); + +int Welcome_ui(); -void Welcome_ui(); #endif //O_UI_H diff --git a/main.c b/main.c index 5843d45..cceb357 100644 --- a/main.c +++ b/main.c @@ -8,6 +8,66 @@ #include "./include/ui.h" #include "./include/myerror.h" #include "./include/extern.h" -int main() { - Welcome_ui(); +#include "./include/curses.h" +#define WELCOME_UI 1 +#define LOGIN_UI 2 +#define SIGNIN_UI 3 +void myexit() { + endwin(); + exit(0); } + +//void test() { +// // 初始化 ncurses +// initscr(); +// cbreak(); // 禁用行缓冲 +// noecho(); // 不显示输入字符 +// curs_set(0); // 隐藏光标 +// +// // 启用所有鼠标事件 +// mousemask(ALL_MOUSE_EVENTS, NULL); +// +// // 创建一个新的窗口 +// WINDOW *win = newwin(10, 30, 5, 5); +// box(win, 0, 0); // 为窗口添加边框 +// mvwprintw(win, 1, 1, "Click inside this window."); +// keypad(win,TRUE); +// wrefresh(win); // 刷新窗口以显示内容 +// +// MEVENT event; // 用于存储鼠标事件 +// +// while (1) { +// int ch = wgetch(win); // 从指定窗口获取输入 +// +// if (ch == KEY_MOUSE) { +// if (nc_getmouse(&event) == OK) { +// // 打印鼠标事件的信息 +// mvwprintw(win, 3, 1, "Mouse at (%d, %d), button %d", event.x, event.y, event.bstate); +// wrefresh(win); // 刷新窗口以显示鼠标点击位置 +// } +// } else { +// mvwprintw(win, 4, 1, "You pressed a key: %c", ch); +// wrefresh(win); // 刷新窗口以显示按键信息 +// break; // 按键退出循环 +// } +// } +// +// // 等待用户按键后退出 +// getch(); +// +// // 清理和结束程序 +// endwin(); +// return; +//} + +int main() { + int code=Welcome_ui(); + while(1){ + switch(code){ + case WELCOME_UI:code=Welcome_ui();break; + case LOGIN_UI:code=Login_ui();break; + case SIGNIN_UI:code=Signin_ui();break; + case 0:myexit(); + } + } +} \ No newline at end of file diff --git a/ui.c b/ui.c index 27804fb..0236497 100644 --- a/ui.c +++ b/ui.c @@ -8,70 +8,144 @@ #include "./include/ui.h" #include -#define BUTTON_WIDTH 12 -#define BUTTON_HEIGHT 3 -#define LOGIN_BTN_X 10 -#define LOGIN_BTN_Y 10 -#define REGISTER_BTN_X 20 -#define REGISTER_BTN_Y 10 -#define USERNAME_X 15 -#define USERNAME_Y 5 -#define PASSWORD_X 15 -#define PASSWORD_Y 7 -#define MAX_INPUT_LEN 30 -void draw_button(WINDOW *win, int x, int y, const char *label, bool is_active) { - if (is_active) { - wattron(win, A_REVERSE); // 激活按钮时反显 - } - mvwprintw(win, y, x + (BUTTON_WIDTH - strlen(label)) / 2, "%s", label); - if (is_active) { - wattroff(win, A_REVERSE); // 反显结束 - } - box(win, 0, 0); // 画按钮的边框 - wrefresh(win); + +// 定义窗口宽度和高度 +#define WIN_HEIGHT 15 +#define WIN_WIDTH 40 + +struct bottom{ + int x,y; + int width,height; + char *test[10]; +}; + +typedef void (*ButtonCallback)(); + +struct bottom *creat_bottom(WINDOW *win,int x,int y,int width,int height,char *test[]) { + struct bottom *bot=malloc(sizeof(struct bottom)); + bot->y=x; + bot->x=y; + bot->width=width; + bot->height=height; + strcpy(bot->test,test); + mvwprintw(win,x,y,"%s",test); + return bot; } - -void draw_login_window() { - // 绘制登录窗口 - clear(); - box(stdscr, 0, 0); - +bool click_bottom(MEVENT event,struct bottom bottom,int sx,int sy){ + return event.x-sx>= bottom.x && event.x-sx < bottom.x + bottom.width && event.y-sy >= bottom.y && event.y-sy < bottom.y + bottom.height; +} +int Welcome_ui() { + MEVENT event; + int startx, starty; + initscr(); // 初始化curses + cbreak(); // 禁用行缓冲 + noecho(); // 不显示输入字符 + curs_set(0); // 隐藏光标 + keypad(stdscr, TRUE); // 启用键盘按键的特殊处理 + mousemask(ALL_MOUSE_EVENTS, NULL); // 启用鼠标事件 + // 获取屏幕的尺寸 + // 计算按钮的显示位置 + int start_y = (LINES - WIN_HEIGHT) / 2; + int start_x = (COLS - WIN_WIDTH) / 2; + WINDOW *welcome_win = newwin(WIN_HEIGHT, WIN_WIDTH, start_y, start_x); + keypad(welcome_win,TRUE); + wborder(welcome_win, '|', '|', '-','-','+', '+', '+', '+'); // 标题 - mvprintw(2, 20, "Login Window"); - - // 用户名 - mvprintw(USERNAME_Y, USERNAME_X - 10, "Username: "); - mvprintw(PASSWORD_Y, PASSWORD_X - 10, "Password: "); - - // 输入框 - mvprintw(USERNAME_Y, USERNAME_X, "____________________________"); - mvprintw(PASSWORD_Y, PASSWORD_X, "____________________________"); - - // 按钮 - mvprintw(LOGIN_BTN_Y, LOGIN_BTN_X, "[ Login ]"); - mvprintw(REGISTER_BTN_Y, REGISTER_BTN_X, "[ Register ]"); - - refresh(); -} - -void handle_mouse_click(MEVENT event) { - // 处理鼠标点击事件 - if (event.bstate & BUTTON1_CLICKED) { - // 判断是否点击了登录按钮 - if (event.x >= LOGIN_BTN_X && event.x <= LOGIN_BTN_X + 9 && - event.y == LOGIN_BTN_Y) { - printw("\nLogin button clicked!\n"); - refresh(); - } - // 判断是否点击了注册按钮 - else if (event.x >= REGISTER_BTN_X && event.x <= REGISTER_BTN_X + 11 && - event.y == REGISTER_BTN_Y) { - printw("\nRegister button clicked!\n"); - refresh(); + mvwprintw(welcome_win, 1, 1, "Welcome to math calculate system!"); + struct bottom *login= creat_bottom(welcome_win,4,1,5,1,"Login"); + struct bottom *signin= creat_bottom(welcome_win,8,1,6,1,"Signin"); + struct bottom *exit= creat_bottom(welcome_win,12,1,4,1,"Exit"); + wrefresh(welcome_win); + while(1){ + int rc=wgetch(welcome_win); + if(rc==KEY_MOUSE){ + if(nc_getmouse(&event)==OK){ + //mvwprintw(welcome_win, 3, 1, "Mouse at (%d, %d), button %d", event.x-start_x, event.y-start_y, event.bstate); + if(click_bottom(event,*login,start_x,start_y)){ + free(login);free(signin);free(exit);endwin(); + return 1; + } + if(click_bottom(event,*signin,start_x,start_y)){ + free(login);free(signin);free(exit);endwin(); + return 2; + } + if(click_bottom(event,*exit,start_x,start_y)){ + free(login);free(signin);free(exit);endwin(); + return 0; + } + } } } } +int Login_ui(){ + +} + +int Signin_ui(){ + +} + + + + + + + + +//void draw_button(WINDOW *win, int x, int y, const char *label, bool is_active) { +// if (is_active) { +// wattron(win, A_REVERSE); // 激活按钮时反显 +// } +// mvwprintw(win, y, x + (BUTTON_WIDTH - strlen(label)) / 2, "%s", label); +// if (is_active) { +// wattroff(win, A_REVERSE); // 反显结束 +// } +// box(win, 0, 0); // 画按钮的边框 +// wrefresh(win); +//} + +//void draw_login_window() { +// // 绘制登录窗口 +// clear(); +// box(stdscr, 0, 0); +// +// // 标题 +// mvprintw(2, 20, "Login Window"); +// +// // 用户名 +// mvprintw(USERNAME_Y, USERNAME_X - 10, "Username: "); +// mvprintw(PASSWORD_Y, PASSWORD_X - 10, "Password: "); +// +// // 输入框 +// mvprintw(USERNAME_Y, USERNAME_X, "____________________________"); +// mvprintw(PASSWORD_Y, PASSWORD_X, "____________________________"); +// +// // 按钮 +// mvprintw(LOGIN_BTN_Y, LOGIN_BTN_X, "[ Login ]"); +// mvprintw(REGISTER_BTN_Y, REGISTER_BTN_X, "[ Register ]"); +// +// refresh(); +//} + +//void handle_mouse_click(MEVENT event) { +// // 处理鼠标点击事件 +// if (event.bstate & BUTTON1_CLICKED) { +// // 判断是否点击了登录按钮 +// if (event.x >= LOGIN_BTN_X && event.x <= LOGIN_BTN_X + 9 && +// event.y == LOGIN_BTN_Y) { +// printw("\nLogin button clicked!\n"); +// refresh(); +// } +// // 判断是否点击了注册按钮 +// else if (event.x >= REGISTER_BTN_X && event.x <= REGISTER_BTN_X + 11 && +// event.y == REGISTER_BTN_Y) { +// printw("\nRegister button clicked!\n"); +// refresh(); +// } +// } +//} + void clear_input_area(int x, int y, int len) { // 清除输入框区域 for (int i = 0; i < len; i++) { @@ -79,131 +153,58 @@ void clear_input_area(int x, int y, int len) { } } -void Login_ui() { - initscr(); // 初始化屏幕 - cbreak(); // 禁用行缓冲 - noecho(); // 不显示输入的字符 - keypad(stdscr, TRUE); // 启用键盘功能(例如箭头键) - mousemask(ALL_MOUSE_EVENTS, NULL); // 启用所有鼠标事件 - curs_set(0); // 隐藏光标 - - draw_login_window(); // 绘制登录窗口 - - char username[MAX_INPUT_LEN + 1] = ""; - char password[MAX_INPUT_LEN + 1] = ""; - int ch; - MEVENT event; - - while (1) { - ch = getch(); // 获取输入 - - if (ch == KEY_MOUSE) { // 检查是否是鼠标事件 - if (nc_getmouse(&event) == OK) { - handle_mouse_click(event); // 处理鼠标点击事件 - } - } - - // 处理键盘输入(用户输入用户名和密码) - if (ch == 10) { // 回车键 - printw("\n登录成功!\n"); - break; - } - - // 如果是在用户名或密码框中输入 - if (ch == KEY_BACKSPACE || ch == 127) { // 退格键 - // 删除输入框中的最后一个字符 - if (strlen(username) > 0) { - username[strlen(username) - 1] = '\0'; - clear_input_area(USERNAME_X, USERNAME_Y, MAX_INPUT_LEN); - mvprintw(USERNAME_Y, USERNAME_X, "%s", username); - } - } else if (isprint(ch)) { // 只接受可打印的字符 - if (strlen(username) < MAX_INPUT_LEN) { - username[strlen(username)] = ch; - username[strlen(username) + 1] = '\0'; - mvprintw(USERNAME_Y, USERNAME_X, "%s", username); - } - } - - refresh(); - } - - endwin(); // 结束 ncurses 模式 - return; -} +//void Login_ui() { +// initscr(); // 初始化屏幕 +// cbreak(); // 禁用行缓冲 +// noecho(); // 不显示输入的字符 +// keypad(stdscr, TRUE); // 启用键盘功能(例如箭头键) +// mousemask(ALL_MOUSE_EVENTS, NULL); // 启用所有鼠标事件 +// curs_set(0); // 隐藏光标 +// +// draw_login_window(); // 绘制登录窗口 +// +// char username[MAX_INPUT_LEN + 1] = ""; +// char password[MAX_INPUT_LEN + 1] = ""; +// int ch; +// MEVENT event; +// +// while (1) { +// ch = getch(); // 获取输入 +// +// if (ch == KEY_MOUSE) { // 检查是否是鼠标事件 +// if (nc_getmouse(&event) == OK) { +// handle_mouse_click(event); // 处理鼠标点击事件 +// } +// } +// +// // 处理键盘输入(用户输入用户名和密码) +// if (ch == 10) { // 回车键 +// printw("\n登录成功!\n"); +// break; +// } +// +// // 如果是在用户名或密码框中输入 +// if (ch == KEY_BACKSPACE || ch == 127) { // 退格键 +// // 删除输入框中的最后一个字符 +// if (strlen(username) > 0) { +// username[strlen(username) - 1] = '\0'; +// clear_input_area(USERNAME_X, USERNAME_Y, MAX_INPUT_LEN); +// mvprintw(USERNAME_Y, USERNAME_X, "%s", username); +// } +// } else if (isprint(ch)) { // 只接受可打印的字符 +// if (strlen(username) < MAX_INPUT_LEN) { +// username[strlen(username)] = ch; +// username[strlen(username) + 1] = '\0'; +// mvprintw(USERNAME_Y, USERNAME_X, "%s", username); +// } +// } +// +// refresh(); +// } +// +// endwin(); // 结束 ncurses 模式 +// return; +//} // 用于绘制按钮的函数 -void Welcome_ui() { - MEVENT event; - int startx, starty; - int button_x, button_y; - // 初始化ncurses环境 - initscr(); - cbreak(); - noecho(); - keypad(stdscr, TRUE); - curs_set(0); - - // 启用鼠标事件 - mousemask(ALL_MOUSE_EVENTS, NULL); - - // 获取屏幕的尺寸 - int rows, cols; - getmaxyx(stdscr, rows, cols); - - // 计算按钮的显示位置 - startx = cols / 2 - BUTTON_WIDTH / 2; - starty = rows / 2 - 2; // 使文本和按钮垂直居中 - - // 打印欢迎文本 - mvprintw(starty - 3, startx, "欢迎来到我们的程序!"); - refresh(); - - // 创建两个按钮的窗口 - WINDOW *login_button_win = newwin(BUTTON_HEIGHT, BUTTON_WIDTH, starty, startx); - WINDOW *register_button_win = newwin(BUTTON_HEIGHT, BUTTON_WIDTH, starty + BUTTON_HEIGHT + 1, startx); - - // 绘制按钮 - draw_button(login_button_win, 0, 0, "Login", false); - draw_button(register_button_win, 0, 0, "Signin", false); - - refresh(); - - bool login_active = false; - bool register_active = false; - - while (1) { - int ch = getch(); - if (ch == KEY_MOUSE) { - if (nc_getmouse(&event) == OK) { - // 检查点击的区域是否是登录按钮 - if (event.x >= startx && event.x < startx + BUTTON_WIDTH && event.y >= starty && event.y < starty + BUTTON_HEIGHT) { - login_active = true; - register_active = false; - draw_button(login_button_win, 0, 0, "Login", true); // 激活登录按钮 - draw_button(register_button_win, 0, 0, "Signin", false); // 取消激活注册按钮 - printw("您点击了登录按钮!\n"); - } - // 检查点击的区域是否是注册按钮 - else if (event.x >= startx && event.x < startx + BUTTON_WIDTH && event.y >= starty + BUTTON_HEIGHT + 1 && event.y < starty + BUTTON_HEIGHT + 1 + BUTTON_HEIGHT) { - login_active = false; - register_active = true; - draw_button(login_button_win, 0, 0, "登录", false); // 取消激活登录按钮 - draw_button(register_button_win, 0, 0, "注册", true); // 激活注册按钮 - printw("您点击了注册按钮!\n"); - } - refresh(); - } - } - - // 按 'q' 键退出 - if (ch == 'q') { - break; - } - } - - // 清理退出 - endwin(); - return; -}