#include "mywin_inc.h" // 弹出式菜单 // 定义弹出式菜单类型 #define WIN_WORKING_TYPE "WIN_PopupStruct" WIN_WorkingStruct *WIN_CreatWorking( WIN_WindowStruct *base, void (*msgLoop)(struct _WIN_WindowStruct *win, WIN_MsgStruct *msg), int x, int y, int x_size, int y_size) { // 重设消息循环 if (msgLoop == 0) { msgLoop = (void (*)(struct _WIN_WindowStruct *win, WIN_MsgStruct *msg))WORKING_DefaultMsgLoop; } WIN_WorkingStruct *ret = mymalloc(sizeof(WIN_WorkingStruct)); // 调用父类的构造函数 if (ret) { mymemset(ret, 0, sizeof(WIN_WorkingStruct)); if (0 == WIN_CreatWindowExt((WIN_WindowStruct *)ret, base, msgLoop, x, y, x_size, y_size)) { // 创建失败 myfree(ret); ret = 0; } else { // 构造一个 ((WIN_WindowStruct *)ret)->winType = WIN_WORKING_TYPE; ((WIN_WindowStruct *)ret)->bkcolor = 0x342f2a; ((WIN_WindowStruct *)ret)->color = 0xc2ae9b; ((WIN_WindowStruct *)ret)->intercept = 1; // 作为子窗口,接管按键消息 ret->rectColor = 0; ret->txtColor = 0xffffff; } } return ret; } const static char g_waite[] = "-\\|/"; // 默认绘制函数 void WORKING_DefaultPaint(WIN_WorkingStruct *working) { int x = 0; int y = 0; int x_size = ((WIN_WindowStruct *)working)->x_size; int y_size = ((WIN_WindowStruct *)working)->y_size; WIN_PaintBackGround((WIN_WindowStruct *)working); WIN_SetFontMode(WIN_DRAWMODE_ALONE); // 绘制信息 WIN_SetLcdColor(working->rectColor); WIN_FillRectByColorAlpha(x + 2, y + 2, x_size - 4, y_size - 4, 16); WIN_SetLcdColor(working->txtColor); WIN_DrawTxtHCenterAt(working->msg_draw, x_size / 2, y + y_size / 2 - WIN_GetFontHight() / 2); // 绘制边框 WIN_SetLcdColor(((WIN_WindowStruct *)working)->bkcolor); WIN_DrawRect(0, 0, ((WIN_WindowStruct *)working)->x_size, ((WIN_WindowStruct *)working)->y_size); } // 默认消息处理函数 void WORKING_DefaultMsgLoop(WIN_WorkingStruct *working, WIN_MsgStruct *msg) { WIN_MoveStruct *m = 0; WIN_KeyStruct *k = 0; switch (msg->msg) { case WIN_MSG_PAINT: WORKING_DefaultPaint(working); break; case WIN_MSG_TIMER: // 定时器时间到 if (msg->data.v == working->time_id1) { if (WIN_GetExWorkFunStat(working->work)) { // 工作已经完成, WIN_SetBlockWinReturn(working->ret, 0, 0); WIN_ExWorkFunClear(working->work); ((WIN_WindowStruct *)working) ->deleteWindow((WIN_WindowStruct *)working); } } else if (msg->data.v == working->time_id2) { working->msg_draw[0] = g_waite[working->msg_h_index]; int now = 0; int all = 0; if (working->get_progress) working->get_progress(&now, &all); if (all) sprintf(&working->msg_draw[1], "%s %d/%d", working->msg, now, all); else sprintf(&working->msg_draw[1], "%s", working->msg); int x = 0; int y = 0; int x_size = 0; int y_size = 0; WIN_WindowStruct *win = (WIN_WindowStruct *)working; WIN_GetTxtRectSize(working->msg_draw, &x_size, &y_size); x_size += 4; y_size += 4; x = (win->baseWin->x_size - x_size) / 2; y = (win->baseWin->y_size - y_size) / 2; WIN_SetPos(win, x, y); WIN_SetSize(win, x_size, y_size); // 重新设置提示的背景 WIN_SetBackPic(win); working->msg_h_index++; if (working->msg_h_index >= 4) working->msg_h_index = 0; WIN_SetInvalidWhenTop((WIN_WindowStruct *)working); } break; case WIN_MSG_KEY: k = msg->data.p; if (k->shortPress & KEY_VALUE_HOME) // 短按 { if (working->break_fun) working->break_fun(working->ptr); } break; case WIN_MSG_MOVE: m = msg->data.p; switch (m->moveType) { case MOVE_DATA_OUTSIDE: // 返回 if (working->break_fun) working->break_fun(working->ptr); break; default: break; } break; default: break; } } // 显示提示框并进行操作,返回值是操作的返回值 int WORKING_DoWork(WIN_WindowStruct *base, char *msg, int (*fun)(void *), void *ptr) { int x, y; int txt_xsize = 0; int txt_ysize = 0; WIN_GetTxtRectSize(msg, &txt_xsize, &txt_ysize); txt_xsize += 4 + WIN_GetFontWidth(); txt_ysize += 4; if (txt_xsize > base->x_size) txt_xsize = base->x_size; txt_ysize += (txt_xsize / base->x_size) * WIN_GetFontHight(); // 单行超过屏幕大小时,最多允许单个行超过屏幕大小 if (txt_ysize > base->y_size) txt_ysize = base->y_size; x = (base->x_size - txt_xsize) / 2; y = (base->y_size - txt_ysize) / 2; WIN_WorkingStruct *w = WIN_CreatWorking(base, 0, x, y, txt_xsize, txt_ysize); sprintf(w->msg, " %s", msg); // 设置提示的背景 WIN_SetBackPic((WIN_WindowStruct *)w); // 创建工作线程 w->work = WIN_CreatThread(fun, ptr, &w->ret); w->time_id1 = WIN_CreatTimer((WIN_WindowStruct *)w, 50); w->time_id2 = WIN_CreatTimer((WIN_WindowStruct *)w, 500); WIN_ShowWindow((WIN_WindowStruct *)w); return WIN_RunBlock((WIN_WindowStruct *)w); } // 显示提示框并进行操作,返回值是操作的返回值 // 这个函数可以指定优先级,并且可以手动退出,可以获取线程进度 int WORKING_DoWorkPro(WIN_WindowStruct *base, char *msg, int (*fun)(void *), void *ptr, void (*break_fun)(void *), void *b_ptr, void (*get_progress)(int *now, int *all), uint8_t pro) { int x, y; int txt_xsize = 0; int txt_ysize = 0; WIN_GetTxtRectSize(msg, &txt_xsize, &txt_ysize); txt_xsize += 4 + WIN_GetFontWidth(); txt_ysize += 4; if (txt_xsize > base->x_size) txt_xsize = base->x_size; txt_ysize += (txt_xsize / base->x_size) * WIN_GetFontHight(); // 单行超过屏幕大小时,最多允许单个行超过屏幕大小 if (txt_ysize > base->y_size) txt_ysize = base->y_size; x = (base->x_size - txt_xsize) / 2; y = (base->y_size - txt_ysize) / 2; WIN_WorkingStruct *w = WIN_CreatWorking(base, 0, x, y, txt_xsize, txt_ysize); sprintf(w->msg, "%s", msg); // 设置提示的背景 WIN_SetBackPic((WIN_WindowStruct *)w); // 创建工作线程 w->work = WIN_CreatThreadPro(fun, ptr, &w->ret, pro); w->break_fun = break_fun; w->ptr = b_ptr; w->get_progress = get_progress; w->time_id1 = WIN_CreatTimer((WIN_WindowStruct *)w, 50); w->time_id2 = WIN_CreatTimer((WIN_WindowStruct *)w, 500); WIN_ShowWindow((WIN_WindowStruct *)w); return WIN_RunBlock((WIN_WindowStruct *)w); }