From f36a5d2630c2fa0b76caa89e075e391e9e67a9fe Mon Sep 17 00:00:00 2001 From: mi84kabv9 Date: Sat, 8 Jun 2024 17:42:54 +0800 Subject: [PATCH] ADD file via upload --- MessageBox.h | 164 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 164 insertions(+) create mode 100644 MessageBox.h diff --git a/MessageBox.h b/MessageBox.h new file mode 100644 index 0000000..3036245 --- /dev/null +++ b/MessageBox.h @@ -0,0 +1,164 @@ +#include +#include + +class EasyTextBox { +private: + int left = 0, top = 0, right = 0, bottom = 0; + wchar_t* text = NULL; + size_t maxlen = 0; +public: + void create(int x1, int y1, int x2, int y2, int max) { + maxlen = max; + text = new wchar_t[maxlen]; + text[0] = 0; + left = x1, top = y1, right = x2, bottom = y2; + // 绘制用户界面 + show(); + } + ~EasyTextBox() { + if (text != NULL) delete[] text; + } + wchar_t* get_text() { + return text; + } + bool check(int x, int y) { + return left <= x && x <= right && top <= y && y <= bottom; + } + void show() { + // 备份环境值 + int old_line_color = getlinecolor(); + int old_bk_color = getbkcolor(); + int old_fill_color = getfillcolor(); + + setlinecolor(LIGHTGRAY); + setbkcolor(0xeeeeee); + setfillcolor(0xeeeeee); + fillrectangle(left, top, right, bottom); + outtextxy(left + 10, top + 5, (LPCTSTR)(&text[0])); + + // 恢复环境值 + setlinecolor(old_line_color); + setbkcolor(old_line_color); + setfillcolor(old_fill_color); + } + void on_message() { + // 备份环境值 + int old_line_color = getlinecolor(); + int old_bk_color = getbkcolor(); + int old_fill_color = getfillcolor(); + + setlinecolor(BLACK); + setbkcolor(WHITE); + setfillcolor(WHITE); + fillrectangle(left, top, right, bottom); + + int width = textwidth((LPCTSTR)text); + int counter = 0; + bool binput = true; + + ExMessage msg; + while (binput) { + // 获取消息,但不从消息队列中拿出 + while (binput && peekmessage(&msg, EX_MOUSE | EX_CHAR, false)) { + if (msg.message == WM_LBUTTONDOWN) { + // 如果鼠标点击文本框外面,结束文本输入 + if (msg.x < left || msg.x > right || msg.y < top || msg.y > bottom) { + binput = false; + break; + } + } + else if (msg.message == WM_CHAR) { + size_t len = wcslen(text); + switch (msg.ch) { + case '\b': { + if (len > 0) { + text[len - 1] = 0; + width = textwidth((LPCTSTR)text); + counter = 0; + clearrectangle(left + 10 + width, top + 1, right - 1, bottom - 1); + } + break; + } + case '\r': + case '\n': + binput = false; + break; + default: + if (len < maxlen - 1) { + text[len++] = msg.ch; + text[len] = 0; + + clearrectangle(left + 10 + width + 1, top + 3, left + 10 + width + 1, bottom - 3); // 清除画的光标 + // 重新计算文本框宽度 + width = textwidth((LPCTSTR)text); + counter = 0; + outtextxy(left + 10, top + 5, (LPCTSTR)text); + } + } + } + peekmessage(NULL, EX_MOUSE | EX_CHAR); + } + + // 绘制光标(光标闪烁周期为 20ms * 32) + counter = (counter + 1) % 32; + if (counter < 16) + line(left + 10 + width + 1, top + 3, left + 10 + width + 1, bottom - 3); + else + clearrectangle(left + 10 + width + 1, top + 3, left + 10 + width + 1, bottom - 3); // 擦光标 + + // 延时 20ms + Sleep(20); + } + clearrectangle(left + 10 + width + 1, top + 3, left + 10 + width + 1, bottom - 3); // 擦光标 + + // 恢复环境值 + setlinecolor(old_line_color); + setbkcolor(old_bk_color); + setfillcolor(old_fill_color); + + show(); + } +}; + +// 实现按钮控件 +class EasyButton { +private: + int left = 0, top = 0, right = 0, bottom = 0; + wchar_t* text = NULL; // 控件内容 + void (*userfunc)() = NULL; // 控件消息 + +public: + void create(int x1, int y1, int x2, int y2, const wchar_t* title, void(*func)()) { + text = new wchar_t[wcslen(title) + 1]; + wcscpy_s(text, wcslen(title) + 1, title); + left = x1, top = y1, right = x2, bottom = y2; + userfunc = func; + + // 绘制 + show(); + } + ~EasyButton() { + if (text != NULL) delete[] text; + } + bool check(int x, int y) { + return left <= x && x <= right && top <= y && y <= bottom; + } + void show() { + int old_line_color = getlinecolor(); + int old_bk_color = getbkcolor(); + int old_fill_color = getfillcolor(); + + setlinecolor(BLACK); // 设置划线颜色 + setbkcolor(WHITE); // 设置背景颜色 + setfillcolor(WHITE); // 设置填充颜色 + outtextxy(left + (right - left - textwidth((LPCTSTR)text) + 1) / 2, top + (bottom - top - textheight((LPCTSTR)text) + 1) / 2, (LPCTSTR)text); + + setlinecolor(old_line_color); + setbkcolor(old_bk_color); + setfillcolor(old_fill_color); + } + + void on_message() { + if (userfunc != NULL) userfunc(); + } +};