增加pdcurses,提供图形化

增加头文件ui.h,pdcurses依赖pdcurses.a,curses.h,分离login模块和ui模块。
This commit is contained in:
xianya 2024-12-28 17:24:07 +08:00
parent 06a064eede
commit e787cac39a
9 changed files with 1686 additions and 60 deletions

View File

@ -1,17 +1,19 @@
cmake_minimum_required(VERSION 3.29)
cmake_minimum_required(VERSION 3.28.6)
project(o C)
set(CMAKE_C_STANDARD 11)
include_directories(include)
include_directories(data)
set(CMAKE_C_STANDARD 99)
set(LIBRARY_PATH "${CMAKE_SOURCE_DIR}/lib")
set(LIBRARY_NAME "pdcurses.a")
add_executable(o
include/curses.h
include/function.h
include/sqlite3.h
include/sqlite3ext.h
include/extern.h
include/myerror.h
main.c
sqlite3.c)
sqlite3.c
login.c
ui.c
include/ui.h)
target_link_libraries(o "${LIBRARY_PATH}/${LIBRARY_NAME}")

1440
include/curses.h Normal file

File diff suppressed because it is too large Load Diff

View File

@ -4,29 +4,12 @@
#include"sqlite3.h"
#ifndef _INC_STDIO
#define _INC_STDIO
#endif
#include <stdio.h>
#ifndef FUNCTION_H
#define FUNCTION_H
int Login();
void clear();
void UserSignin();
void StudentsDesktop();
void TeachersDesktop();
void clear(){
fprintf(stdout,"\033[2J"); //利用转义字符清空屏幕
}
int Login(){
sqlite3 *db;
int rc;
rc=sqlite3_open("./include/user.db",db);
if(rc!=SQLITE_OK){
fprintf(stderr,"无法打开数据库:%s\n",sqlite3_errmsg(db));
return -1;
}
}
#endif //FUNCTION_H

View File

@ -2,13 +2,9 @@
// Created by zhang on 2024/12/25.
//
#ifndef _INC_STDIO
#define _INC_STDIO
#endif
#include <stdio.h>
#ifndef FUNCTION_H
#define FUNCTION_H
#endif
#include "function.h"
#ifndef MYERROR_H
#define MYERROR_H

17
include/ui.h Normal file
View File

@ -0,0 +1,17 @@
//
// Created by zhang on 2024/12/28.
//
#ifndef O_UI_H
#define O_UI_H
void Login_ui();
void UserSignin_ui();
void StudentsDesktop_ui();
void TeachersDesktop_ui();
void Welcome_ui();
#endif //O_UI_H

BIN
lib/pdcurses.a Normal file

Binary file not shown.

0
login.c Normal file
View File

31
main.c
View File

@ -4,31 +4,10 @@
#include <stdio.h>
#include <stdbool.h>
#include <windows.h>
#include "function.h"
#include "myerror.h"
#include "extern.h"
#include "./include/function.h"
#include "./include/ui.h"
#include "./include/myerror.h"
#include "./include/extern.h"
int main() {
SetConsoleOutputCP(65001);
int isStudent;
while (true) {
fprintf(stdout, "欢迎进入口算系统!\n\n输入1以登录\n输入2以注册");
int x;
scanf("%d", &x);
if (x == 1) {
isStudent = Login();
if (isStudent == 1 || isStudent == 0)break;
if (isStudent == -1)continue;
} else if (x == 2) {
//UserSignin();
clear();
} else {
//Error_Invalid_input();
}
}
if (isStudent) {
//StudentsDesktop();
} else {
//TeachersDesktop();
}
Welcome_ui();
}

209
ui.c Normal file
View File

@ -0,0 +1,209 @@
//
// Created by zhang on 2024/12/28.
//
#include <string.h>
#include <ctype.h>
#include "./include/function.h"
#include "./include/curses.h"
#include "./include/ui.h"
#include <stdlib.h>
#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);
}
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++) {
mvaddch(y, x + i, ' ');
}
}
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;
}