目录
前言
为了方便快捷绘制按钮、输入框、表格等图形,封装成类方便调用。
一、按钮--封装类
class EasyButton
{
private:
int left = 0, top = 0, right = 0, bottom = 0; // 控件坐标
int r = 0; // 控件倒角
int rgb_1 = 0; // 控件线颜色选择
int rgb_2 = 1; // 控件填充颜色选择
int size = 10; // 控件字体大小
wchar_t* text = NULL; // 控件内容
wchar_t* text2 = NULL; // 鼠标停留时控件内容
int name = 1; // 选择使用的名称
void (*userfunc)() = NULL; // 控件消息
COLORREF color1[12] = // 颜色选择
{ RGB(255,229,204),
RGB(229, 255, 179),RGB(17, 153, 142),RGB(56, 239, 125),RGB(224, 255, 255),RGB(192,192,192),
RGB(56,239,125), RGB(17, 153, 142),RGB(19, 113, 171),RGB(201, 222, 245), };
public:
//左上X坐标,左上Y坐标,右下X坐标,右下Y坐标,圆角,字体大小,背景颜色1,背景颜色2,按钮名称,按钮名称2,调用函数;
void init(int x1, int y1, int x2, int y2, int r1, int size1, int rgb1, int rgb2, const wchar_t* title, const wchar_t* title2, void (*func)())
{
text = new wchar_t[wcslen(title) + 1];
wcscpy_s(text, wcslen(title) + 1, title);
text2 = new wchar_t[wcslen(title2) + 1];
wcscpy_s(text2, wcslen(title2) + 1, title2);
left = x1, top = y1, right = x2, bottom = y2;
r = r1;
rgb_1 = rgb1;
rgb_2 = rgb2;
size = size1;
userfunc = func;
}
~EasyButton()
{
if (text != NULL)
delete[] text;
if (text2 != NULL)
delete[] text2;
}
bool Check(int x, int y)
{
return (left <= x && x <= right && top <= y && y <= bottom);
}
// 绘制界面/*填充颜色 1 或 2 */
void Show(int name1)
{
int oldlinecolor = getlinecolor();
int oldbkcolor = getbkcolor();
int oldfillcolor = getfillcolor();
LOGFONT f;
gettextstyle(&f); // 获取当前字体设置。
f.lfHeight = size; // 设置字体高度为 48。
_tcscpy_s(f.lfFaceName, _T("楷体"));
f.lfQuality = ANTIALIASED_QUALITY; // 设置输出效果为抗锯齿。
settextstyle(&f);
setbkmode(TRANSPARENT); // 设置透明的背景。
//setlinestyle(PS_NULL);
setlinestyle(PS_SOLID, 1);
settextcolor(BLACK); // 设置字体颜色
setlinecolor(RED); // 设置画线颜色
setbkcolor(WHITE); // 设置背景颜色
if (name1 == 1)
{
setfillcolor(color1[rgb_1]); // 设置填充颜色
fillroundrect(left, top, right, bottom, r, r);
setbkmode(TRANSPARENT);
outtextxy(left + (right - left - textwidth(text) + 1) / 2, top + (bottom - top - textheight(text) + 1) / 2, text);
}
else
{
setfillcolor(color1[rgb_2]); // 设置填充颜色
fillroundrect(left, top, right, bottom, r, r);
setbkmode(TRANSPARENT);
outtextxy(left + (right - left - textwidth(text2) + 1) / 2, top + (bottom - top - textheight(text2) + 1) / 2, text2);
}
setlinecolor(oldlinecolor);
setbkcolor(oldbkcolor);
setfillcolor(oldfillcolor);
}
void OnMessage()
{
if (userfunc != NULL)
userfunc();
}
};
1.建立按钮变量
EasyButton btn1;
2.绘制按钮基本条件
btn1.init(950,500,1050,550,10,30,2,1,L"确定", L"-确定-", on_btn1_cleck);
3.按钮显示
btn1.Show(1);
二、输入框--封装类
class EasyTextBox
{
private:
int left = 0, top = 0, right = 0, bottom = 0; // 控件坐标
wchar_t* text = NULL; // 控件内容
int rgb = 0; // 字体颜色
int size = 10; // 控件字体大小
size_t maxlen = 0; // 文本框最大内容长度
public:
//左上x坐标,左上Y坐标,右下X坐标,右下Y坐标,文本最大长度,字体颜色,字体大小
void init(int x1, int y1, int x2, int y2, int max, int rgb1, int size1)
{
maxlen = max;
rgb = rgb1;
size = size1;
text = new wchar_t[maxlen];
text[0] = 0;
left = x1, top = y1, right = x2, bottom = y2;
}
~EasyTextBox()
{
if (text != NULL)
delete[] text;
}
wchar_t* Text()
{
return text;
}
bool Check(int x, int y)
{
return (left <= x && x <= right && top <= y && y <= bottom);
}
// 绘制界面
void Show()
{
// 备份环境值
int oldlinecolor = getlinecolor();
int oldbkcolor = getbkcolor();
int oldfillcolor = getfillcolor();
LOGFONT f;
gettextstyle(&f); // 获取当前字体设置。
f.lfHeight = size; // 设置字体高度为 48。
_tcscpy_s(f.lfFaceName, _T("楷体"));
f.lfQuality = ANTIALIASED_QUALITY; // 设置输出效果为抗锯齿。
settextstyle(&f);
setbkmode(TRANSPARENT); // 设置透明的背景。
setlinecolor(LIGHTGRAY); // 设置画线颜色
setbkcolor(0xeeeeee); // 设置背景颜色
setfillcolor(0xeeeeee); // 设置填充颜色
if (rgb == 0)
{
settextcolor(BLACK); // 设置字体颜色
}
if (rgb == 1)
{
settextcolor(RED); // 设置字体颜色
}
fillrectangle(left, top, right, bottom);
outtextxy(left + 10, top + (bottom - top - textheight(text) + 1) / 2, text);
// 恢复环境值
setlinecolor(oldlinecolor);
setbkcolor(oldbkcolor);
setfillcolor(oldfillcolor);
}
void OnMessage()
{
// 备份环境值
int oldlinecolor = getlinecolor();
int oldbkcolor = getbkcolor();
int oldfillcolor = getfillcolor();
LOGFONT f;
gettextstyle(&f); // 获取当前字体设置。
f.lfHeight = size; // 设置字体高度为 48。
_tcscpy_s(f.lfFaceName, _T("楷体"));
f.lfQuality = ANTIALIASED_QUALITY; // 设置输出效果为抗锯齿。
settextstyle(&f);
setbkmode(TRANSPARENT); // 设置透明的背景。
setlinecolor(BLACK); // 设置画线颜色
setbkcolor(WHITE); // 设置背景颜色
setfillcolor(WHITE); // 设置填充颜色
if (rgb == 0)
{
settextcolor(BLACK); // 设置字体颜色
}
if (rgb == 1)
{
settextcolor(RED); // 设置字体颜色
}
fillrectangle(left, top, right, bottom);
outtextxy(left + 10, top + (bottom - top - textheight(text) + 1) / 2, text);
int width = textwidth(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);
//settextcolor(BLACK);
if (rgb == 0)
{
settextcolor(BLACK); // 设置字体颜色
}
if (rgb == 1)
{
settextcolor(RED); // 设置字体颜色
}
switch (msg.ch)
{
case '\b': // 用户按退格键,删掉一个字符
if (len > 0)
{
text[len - 1] = 0;
width = textwidth(text);
counter = 0;
setbkcolor(WHITE); // 设置背景颜色
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;
setlinecolor(WHITE); // 设置画线颜色
setbkcolor(WHITE); // 设置背景颜色
clearrectangle(left + 10 + width + 3, top + 3, left + 10 + width + 6, bottom - 3); // 清除画的光标
width = textwidth(text); // 重新计算文本框宽度
counter = 0;
outtextxy(left + 10, top + (bottom - top - textheight(text) + 1) / 2, text); // 输出新的字符串
}
}
}
peekmessage(NULL, EX_MOUSE | EX_CHAR); // 从消息队列抛弃刚刚处理过的一个消息
}
// 绘制光标(光标闪烁周期为 20ms * 32)
counter = (counter + 1) % 32;
if (counter < 16)
{
setlinecolor(WHITE); // 设置画线颜色
setbkcolor(WHITE); // 设置背景颜色
fillrectangle(left + 10 + width + 3, top + 3, left + 10 + width + 6, bottom - 3); // 画光标
;
}
else
{
setlinecolor(BLACK); // 设置画线颜色
setbkcolor(BLACK); // 设置背景颜色
clearrectangle(left + 10 + width + 4, top + 4, left + 10 + width + 5, bottom - 4); // 擦光标
}
// 延时 20ms
Sleep(20);
}
clearrectangle(left + 10 + width + 4, top + 3, left + 10 + width + 5, bottom - 2); // 擦光标
// 恢复环境值
setlinecolor(oldlinecolor);
setbkcolor(oldbkcolor);
setfillcolor(oldfillcolor);
Show();
}
};
1.建立输入框变量
EasyTextBox edit1;
2.绘制输入框基本条件
edit1.init(850, 400, 1150, 450, 50, 0, 20);
3.输入框显示
edit1.Show();
三、表格界面--封装类
class Tabledialog
{
private:
int x = 0; // 起点坐标X
int y = 0; // 起点坐标Y
int width = 200;
int height = 300;
int currentIndex = -1;
vector<IMAGE*> pages;
vector<vector<TableWidget1*>> tables;
void addPage(IMAGE* page)
{
pages.push_back(page);
tables.push_back({});
}
void addTable(int index, TableWidget1* button)
{
if (index >= 0 && index < tables.size())
{
tables[index].push_back(button);
}
}
public:
int selectRow = 0; // 被选中的行
~Tabledialog() {}
void mouseClick(int mouseX, int mouseY)
{
if (currentIndex >= 0 && currentIndex < tables.size())
{
for (TableWidget1* table : tables[currentIndex])
{
//table->handleMouseClick(mouseX, mouseY);
selectRow = table->handleMouseClick(mouseX, mouseY);
}
}
}
void mouseWheel(int mouseX, int mouseY, int wheel)
{
if (currentIndex >= 0 && currentIndex < tables.size())
{
for (TableWidget1* table : tables[currentIndex])
{
table->scroll(mouseX, mouseY, wheel);
}
}
}
void draw()
{
if (currentIndex >= 0 && currentIndex < tables.size())
{
putimage(0, 0, pages[currentIndex]);
for (TableWidget1* table : tables[currentIndex])
{
table->draw();
}
}
}
void init(int zou_x, int zou_y, int width1, int height1)
{
width = width1;
height = height1;
currentIndex = -1;
x = zou_x;
y = zou_y;
// 创建页面1
IMAGE* page1 = new IMAGE(width, height);
setfillcolor(RGB(240, 240, 240));
solidrectangle(x, y, width, height);
getimage(page1, x, y, width, height);
addPage(page1);
vector<vector<wstring>> myvt; // 定义模板类对象
myvt.reserve(65); // 设置大小
// 添加内容
myvt.push_back({ Tipe_Tchar[0],Tipe_Tchar[1],Tipe_Tchar[2],Tipe_Tchar[3],Tipe_Tchar[4] ,Tipe_Tchar[5] });
for (int i = 0; i < 24; i++)
{
myvt.push_back({ Time_Tchar[i],Date1_Tchar[i],Date2_Tchar[i],Date3_Tchar[i] ,Date4_Tchar[i] ,UPH_Tchar[i] });
}
//TableWidget1* table1 = new TableWidget1(100, 100, 400, 300, 12);
TableWidget1* table1 = new TableWidget1(x, y, width, height, 12, 20);
table1->setData(myvt);
addTable(0, table1);
currentIndex = 0;
}
void run()
{
ExMessage msg;
BeginBatchDraw();
if (peekmessage(&msg))
{
int mouseX = msg.x;
int mouseY = msg.y;
switch (msg.message)
{
case WM_LBUTTONDOWN:
mouseClick(mouseX, mouseY);
break;
case WM_MOUSEWHEEL:
mouseWheel(mouseX, mouseY, msg.wheel);
break;
}
}
draw();
FlushBatchDraw();
EndBatchDraw();
Sleep(10);
}
void close()
{
closegraph();
}
};
1.建立变量
Tabledialog Table1;
2.绘制基本条件
Table1.init(0, 0, 800, 600);
3.显示
Table1.Show();
示例
#include <graphics.h>
#include <string>
#include <vector>
#include <iostream>
using namespace std;
TCHAR Tipe_Tchar[10][20] = // 标题
{
L"时间",L"通道1", L"通道2", L"通道3", L"通道4", L"合计",
};
TCHAR Time_Tchar[24][20] = // 时间
{
L"08:00-09:00", L"09:00-10:00",
L"10:00-11:00", L"11:00-12:00",
L"12:00-13:00", L"13:00-14:00",
L"14:00-15:00", L"15:00-16:00",
L"16:00-17:00", L"17:00-18:00",
L"18:00-19:00", L"19:00-20:00",
L"20:00-21:00", L"21:00-22:00",
L"22:00-23:00", L"23:00-00:00",
L"00:00-01:00", L"01:00-02:00",
L"02:00-03:00", L"03:00-04:00",
L"04:00-05:00", L"05:00-06:00",
L"06:00-07:00", L"07:00-08:00",
};
TCHAR Date1_Tchar[24][20] = // 通道1
{
L"11212", L"12452",
L"12412", L"12563",
L"12547", L"15268",
L"15478", L"15246",
L"18456", L"15247",
L"15987", L"16487",
L"16894", L"13546",
L"19456", L"18456",
L"17854", L"16245",
L"18465", L"19465",
L"17843", L"16542",
L"17452", L"12451",
};
TCHAR Date2_Tchar[24][20] = // 通道2
{
L"11212", L"12452",
L"12412", L"12563",
L"12547", L"15268",
L"15478", L"15246",
L"18456", L"15247",
L"15987", L"16487",
L"16894", L"13546",
L"19456", L"18456",
L"17854", L"16245",
L"18465", L"19465",
L"17843", L"16542",
L"17452", L"12451",
};
TCHAR Date3_Tchar[24][20] = // 通道3
{
L"11212", L"12452",
L"12412", L"12563",
L"12547", L"15268",
L"15478", L"15246",
L"18456", L"15247",
L"15987", L"16487",
L"16894", L"13546",
L"19456", L"18456",
L"17854", L"16245",
L"18465", L"19465",
L"17843", L"16542",
L"17452", L"12451",
};
TCHAR Date4_Tchar[24][20] = // 通道4
{
L"11212", L"12452",
L"12412", L"12563",
L"12547", L"15268",
L"15478", L"15246",
L"18456", L"15247",
L"15987", L"16487",
L"16894", L"13546",
L"19456", L"18456",
L"17854", L"16245",
L"18465", L"19465",
L"17843", L"16542",
L"17452", L"12451",
};
TCHAR UPH_Tchar[24][20] = // 合计
{
L"11212", L"12452",
L"12412", L"12563",
L"12547", L"15268",
L"15478", L"15246",
L"18456", L"15247",
L"15987", L"16487",
L"16894", L"13546",
L"19456", L"18456",
L"17854", L"16245",
L"18465", L"19465",
L"17843", L"16542",
L"17452", L"12451",
};
// 实现按钮控件
class EasyButton
{
private:
int left = 0, top = 0, right = 0, bottom = 0; // 控件坐标
int r = 0; // 控件倒角
int rgb_1 = 0; // 控件线颜色选择
int rgb_2 = 1; // 控件填充颜色选择
int size = 10; // 控件字体大小
wchar_t* text = NULL; // 控件内容
wchar_t* text2 = NULL; // 鼠标停留时控件内容
int name = 1; // 选择使用的名称
void (*userfunc)() = NULL; // 控件消息
COLORREF color1[12] = // 颜色选择
{ RGB(255,229,204),
RGB(229, 255, 179),RGB(17, 153, 142),RGB(56, 239, 125),RGB(224, 255, 255),RGB(192,192,192),
RGB(56,239,125), RGB(17, 153, 142),RGB(19, 113, 171),RGB(201, 222, 245), };
public:
//左上X坐标,左上Y坐标,右下X坐标,右下Y坐标,圆角,字体大小,背景颜色1,背景颜色2,按钮名称,按钮名称2,调用函数;
void init(int x1, int y1, int x2, int y2, int r1, int size1, int rgb1, int rgb2, const wchar_t* title, const wchar_t* title2, void (*func)())
{
text = new wchar_t[wcslen(title) + 1];
wcscpy_s(text, wcslen(title) + 1, title);
text2 = new wchar_t[wcslen(title2) + 1];
wcscpy_s(text2, wcslen(title2) + 1, title2);
left = x1, top = y1, right = x2, bottom = y2;
r = r1;
rgb_1 = rgb1;
rgb_2 = rgb2;
size = size1;
userfunc = func;
}
~EasyButton()
{
if (text != NULL)
delete[] text;
if (text2 != NULL)
delete[] text2;
}
bool Check(int x, int y)
{
return (left <= x && x <= right && top <= y && y <= bottom);
}
// 绘制界面/*填充颜色 1 或 2 */
void Show(int name1)
{
int oldlinecolor = getlinecolor();
int oldbkcolor = getbkcolor();
int oldfillcolor = getfillcolor();
LOGFONT f;
gettextstyle(&f); // 获取当前字体设置。
f.lfHeight = size; // 设置字体高度为 48。
_tcscpy_s(f.lfFaceName, _T("楷体"));
f.lfQuality = ANTIALIASED_QUALITY; // 设置输出效果为抗锯齿。
settextstyle(&f);
setbkmode(TRANSPARENT); // 设置透明的背景。
//setlinestyle(PS_NULL);
setlinestyle(PS_SOLID, 1);
settextcolor(BLACK); // 设置字体颜色
setlinecolor(RED); // 设置画线颜色
setbkcolor(WHITE); // 设置背景颜色
if (name1 == 1)
{
setfillcolor(color1[rgb_1]); // 设置填充颜色
fillroundrect(left, top, right, bottom, r, r);
setbkmode(TRANSPARENT);
outtextxy(left + (right - left - textwidth(text) + 1) / 2, top + (bottom - top - textheight(text) + 1) / 2, text);
}
else
{
setfillcolor(color1[rgb_2]); // 设置填充颜色
fillroundrect(left, top, right, bottom, r, r);
setbkmode(TRANSPARENT);
outtextxy(left + (right - left - textwidth(text2) + 1) / 2, top + (bottom - top - textheight(text2) + 1) / 2, text2);
}
setlinecolor(oldlinecolor);
setbkcolor(oldbkcolor);
setfillcolor(oldfillcolor);
}
void OnMessage()
{
if (userfunc != NULL)
userfunc();
}
};
// 实现文本框控件
class EasyTextBox
{
private:
int left = 0, top = 0, right = 0, bottom = 0; // 控件坐标
wchar_t* text = NULL; // 控件内容
int rgb = 0; // 字体颜色
int size = 10; // 控件字体大小
size_t maxlen = 0; // 文本框最大内容长度
public:
//左上x坐标,左上Y坐标,右下X坐标,右下Y坐标,文本最大长度,字体颜色,字体大小
void init(int x1, int y1, int x2, int y2, int max, int rgb1, int size1)
{
maxlen = max;
rgb = rgb1;
size = size1;
text = new wchar_t[maxlen];
text[0] = 0;
left = x1, top = y1, right = x2, bottom = y2;
}
~EasyTextBox()
{
if (text != NULL)
delete[] text;
}
wchar_t* Text()
{
return text;
}
bool Check(int x, int y)
{
return (left <= x && x <= right && top <= y && y <= bottom);
}
// 绘制界面
void Show()
{
// 备份环境值
int oldlinecolor = getlinecolor();
int oldbkcolor = getbkcolor();
int oldfillcolor = getfillcolor();
LOGFONT f;
gettextstyle(&f); // 获取当前字体设置。
f.lfHeight = size; // 设置字体高度为 48。
_tcscpy_s(f.lfFaceName, _T("楷体"));
f.lfQuality = ANTIALIASED_QUALITY; // 设置输出效果为抗锯齿。
settextstyle(&f);
setbkmode(TRANSPARENT); // 设置透明的背景。
setlinecolor(LIGHTGRAY); // 设置画线颜色
setbkcolor(0xeeeeee); // 设置背景颜色
setfillcolor(0xeeeeee); // 设置填充颜色
if (rgb == 0)
{
settextcolor(BLACK); // 设置字体颜色
}
if (rgb == 1)
{
settextcolor(RED); // 设置字体颜色
}
fillrectangle(left, top, right, bottom);
outtextxy(left + 10, top + (bottom - top - textheight(text) + 1) / 2, text);
// 恢复环境值
setlinecolor(oldlinecolor);
setbkcolor(oldbkcolor);
setfillcolor(oldfillcolor);
}
void OnMessage()
{
// 备份环境值
int oldlinecolor = getlinecolor();
int oldbkcolor = getbkcolor();
int oldfillcolor = getfillcolor();
LOGFONT f;
gettextstyle(&f); // 获取当前字体设置。
f.lfHeight = size; // 设置字体高度为 48。
_tcscpy_s(f.lfFaceName, _T("楷体"));
f.lfQuality = ANTIALIASED_QUALITY; // 设置输出效果为抗锯齿。
settextstyle(&f);
setbkmode(TRANSPARENT); // 设置透明的背景。
setlinecolor(BLACK); // 设置画线颜色
setbkcolor(WHITE); // 设置背景颜色
setfillcolor(WHITE); // 设置填充颜色
if (rgb == 0)
{
settextcolor(BLACK); // 设置字体颜色
}
if (rgb == 1)
{
settextcolor(RED); // 设置字体颜色
}
fillrectangle(left, top, right, bottom);
outtextxy(left + 10, top + (bottom - top - textheight(text) + 1) / 2, text);
int width = textwidth(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);
//settextcolor(BLACK);
if (rgb == 0)
{
settextcolor(BLACK); // 设置字体颜色
}
if (rgb == 1)
{
settextcolor(RED); // 设置字体颜色
}
switch (msg.ch)
{
case '\b': // 用户按退格键,删掉一个字符
if (len > 0)
{
text[len - 1] = 0;
width = textwidth(text);
counter = 0;
setbkcolor(WHITE); // 设置背景颜色
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;
setlinecolor(WHITE); // 设置画线颜色
setbkcolor(WHITE); // 设置背景颜色
clearrectangle(left + 10 + width + 3, top + 3, left + 10 + width + 6, bottom - 3); // 清除画的光标
width = textwidth(text); // 重新计算文本框宽度
counter = 0;
outtextxy(left + 10, top + (bottom - top - textheight(text) + 1) / 2, text); // 输出新的字符串
}
}
}
peekmessage(NULL, EX_MOUSE | EX_CHAR); // 从消息队列抛弃刚刚处理过的一个消息
}
// 绘制光标(光标闪烁周期为 20ms * 32)
counter = (counter + 1) % 32;
if (counter < 16)
{
setlinecolor(WHITE); // 设置画线颜色
setbkcolor(WHITE); // 设置背景颜色
fillrectangle(left + 10 + width + 3, top + 3, left + 10 + width + 6, bottom - 3); // 画光标
;
}
else
{
setlinecolor(BLACK); // 设置画线颜色
setbkcolor(BLACK); // 设置背景颜色
clearrectangle(left + 10 + width + 4, top + 4, left + 10 + width + 5, bottom - 4); // 擦光标
}
// 延时 20ms
Sleep(20);
}
clearrectangle(left + 10 + width + 4, top + 3, left + 10 + width + 5, bottom - 2); // 擦光标
// 恢复环境值
setlinecolor(oldlinecolor);
setbkcolor(oldbkcolor);
setfillcolor(oldfillcolor);
Show();
}
};
//表格类
class TableWidget1 {
private:
int x;
int y;
int width; // 窗口宽度
int height; // 窗口高度
int visibleRowCount; // 最多可见行数
int rowHeight; // 行的高度
int scrollOffset; // 偏移
vector<vector<wstring>> data;
vector<int> columnWidths; // 列的宽度
int selectedRow; // 被选中的行
int scrollbarWidth;
int handleHeight; // 滑块高度
int handleY;
int sizea;
public:
TableWidget1(int x, int y, int width, int height, int visibleRowCount,int size1)
: x(x), y(y), width(width), height(height), visibleRowCount(visibleRowCount) {
data = { {} };
rowHeight = height / visibleRowCount;
selectedRow = -1;
scrollOffset = 0;
scrollbarWidth = 20;
handleHeight = 30;
handleY = 0;
sizea = size1;
}
void ziti_size(int size)
{
LOGFONT f;
gettextstyle(&f); // 获取当前字体设置。
f.lfHeight = size; // 设置字体高度为 48。
_tcscpy_s(f.lfFaceName, _T("楷体"));
f.lfQuality = ANTIALIASED_QUALITY; // 设置输出效果为抗锯齿。
settextstyle(&f);
setbkmode(TRANSPARENT); // 设置透明的背景。
}
int getSelectedRow() const
{
return selectedRow;
}
wstring getSelectedInfo(int col) const
{
return data[selectedRow][col];
}
void setData(const vector<vector<wstring>>& newData)
{
data = newData;
calculateColumnWidths();
}
//计算每列的宽度
void calculateColumnWidths()
{
columnWidths.clear();
if (!data.empty())
{
columnWidths.resize(data[0].size(), 0);
for (const auto& row : data)
{
for (size_t j = 0; j < row.size(); ++j)
{
int width = textwidth(row[j].c_str());
if (width > columnWidths[j]) //每列的最大宽度(即每列中字符最大长度)
{
columnWidths[j] = width;
}
}
}
}
//根据每列最大宽度计算该列长度
int sumWidth = 0;
float abc = 0.1f;
for (auto it : columnWidths)
sumWidth += it;
for (auto& it : columnWidths)
{
abc = (float)it / sumWidth * width;
it = (int)abc;
}
}
void scrollUp()
{
if (scrollOffset > 0)
{
scrollOffset--;
}
if (scrollOffset < 0)
{
scrollOffset = 0;
}
}
void scrollDown()
{
int maxScrollOffset = int(data.size()) - visibleRowCount;
if (scrollOffset < maxScrollOffset)
{
scrollOffset++;
}
}
void scroll(int mouseX, int mouseY, int wheel)
{
if (mouseX >= x && mouseX <= x + width && mouseY >= y && mouseY <= y + height)
{
if (wheel > 0)
{
scrollUp();
}
else if (wheel < 0) {
scrollDown();
}
}
}
int handleMouseClick(int mouseX, int mouseY)
{
if (mouseX >= x && mouseX <= x + width && mouseY >= y && mouseY <= y + height)
{
int clickedRow = (mouseY - y) / rowHeight + scrollOffset;
if (clickedRow >= 0 && clickedRow < static_cast<int>(data.size()))
{
selectedRow = clickedRow;
}
else
{
selectedRow = -1;
}
}
return selectedRow;
}
void draw()
{
setbkmode(1);
// 绘制表格区域
setfillcolor(WHITE);
solidrectangle(x, y, x + width, y + height);
setlinecolor(BLACK);
settextstyle(12, 0, _T("宋体"));
//字体大小
ziti_size(sizea);
// 计算需要绘制的行数
int rowCount = min(visibleRowCount, static_cast<int>(data.size()));
// 绘制表头
int headerY = y;
int columnX = x;
for (int j = 0; j < data[0].size(); ++j) {
int columnWidth = columnWidths[j];
rectangle(columnX, headerY, columnX + columnWidth, headerY + rowHeight);
int textX = columnX + (columnWidth - textwidth(data[0][j].c_str())) / 2;
int textY = headerY + (rowHeight - textheight(_T("宋体"))) / 2;
outtextxy(textX, textY, data[0][j].c_str());
columnX += columnWidth;
}
// 绘制表格内容
for (int i = 1; i < rowCount; ++i) {
int rowY = y + i * rowHeight;
int dataIndex = i + scrollOffset;
columnX = x;
for (int j = 0; j < data[dataIndex].size(); ++j) {
int columnWidth = columnWidths[j];
bool isSelectedRow = (dataIndex == selectedRow);
if (isSelectedRow) {
setfillcolor(LIGHTBLUE);
settextcolor(RED);
}
else {
setfillcolor(WHITE);
settextcolor(BLACK);
}
fillrectangle(columnX, rowY, columnX + columnWidth, rowY + rowHeight);
int textX = columnX + (columnWidth - textwidth(data[dataIndex][j].c_str())) / 2;
int textY = rowY + (rowHeight - textheight(_T("宋体"))) / 2;
outtextxy(textX, textY, data[dataIndex][j].c_str());
columnX += columnWidth;
}
}
// 绘制滚动条背景
int scrollbarX = x + width;
setfillcolor(LIGHTGRAY);
solidrectangle(scrollbarX, y, scrollbarX + scrollbarWidth, y + height);
// 计算滑块位置和大小
int handleX = scrollbarX;
int handleWidth = scrollbarWidth;
float abc = 0.1f;
float abd = 0.1f;
abc = visibleRowCount / (float(data.size()) - 1);
abd = abc * height;
handleHeight = int(abd);
int maxHandleY = height - handleHeight;
handleY = maxHandleY * scrollOffset / (int(data.size()) - visibleRowCount);
//handleY = maxHandleY * double(scrollOffset) / (data.size() - visibleRowCount);
// 绘制滑块
setfillcolor(DARKGRAY);
solidrectangle(handleX, y + handleY, handleX + handleWidth, y + handleY + handleHeight);
}
};
//表格窗口类
class Tabledialog
{
private:
int x = 0; // 起点坐标X
int y = 0; // 起点坐标Y
int width = 200;
int height = 300;
int currentIndex = -1;
vector<IMAGE*> pages;
vector<vector<TableWidget1*>> tables;
void addPage(IMAGE* page)
{
pages.push_back(page);
tables.push_back({});
}
void addTable(int index, TableWidget1* button)
{
if (index >= 0 && index < tables.size())
{
tables[index].push_back(button);
}
}
public:
int selectRow = 0; // 被选中的行
~Tabledialog() {}
void mouseClick(int mouseX, int mouseY)
{
if (currentIndex >= 0 && currentIndex < tables.size())
{
for (TableWidget1* table : tables[currentIndex])
{
//table->handleMouseClick(mouseX, mouseY);
selectRow = table->handleMouseClick(mouseX, mouseY);
}
}
}
void mouseWheel(int mouseX, int mouseY, int wheel)
{
if (currentIndex >= 0 && currentIndex < tables.size())
{
for (TableWidget1* table : tables[currentIndex])
{
table->scroll(mouseX, mouseY, wheel);
}
}
}
void draw()
{
if (currentIndex >= 0 && currentIndex < tables.size())
{
putimage(0, 0, pages[currentIndex]);
for (TableWidget1* table : tables[currentIndex])
{
table->draw();
}
}
}
void init(int zou_x, int zou_y, int width1, int height1)
{
width = width1;
height = height1;
currentIndex = -1;
x = zou_x;
y = zou_y;
// 创建页面1
IMAGE* page1 = new IMAGE(width, height);
setfillcolor(RGB(240, 240, 240));
solidrectangle(x, y, width, height);
getimage(page1, x, y, width, height);
addPage(page1);
vector<vector<wstring>> myvt; // 定义模板类对象
myvt.reserve(65); // 设置大小
// 添加内容
myvt.push_back({ Tipe_Tchar[0],Tipe_Tchar[1],Tipe_Tchar[2],Tipe_Tchar[3],Tipe_Tchar[4] ,Tipe_Tchar[5] });
for (int i = 0; i < 24; i++)
{
myvt.push_back({ Time_Tchar[i],Date1_Tchar[i],Date2_Tchar[i],Date3_Tchar[i] ,Date4_Tchar[i] ,UPH_Tchar[i] });
}
//TableWidget1* table1 = new TableWidget1(100, 100, 400, 300, 12);
TableWidget1* table1 = new TableWidget1(x, y, width, height, 12, 20);
table1->setData(myvt);
addTable(0, table1);
currentIndex = 0;
}
void Show()
{
ExMessage msg;
BeginBatchDraw();
if (peekmessage(&msg))
{
int mouseX = msg.x;
int mouseY = msg.y;
switch (msg.message)
{
case WM_LBUTTONDOWN:
mouseClick(mouseX, mouseY);
break;
case WM_MOUSEWHEEL:
mouseWheel(mouseX, mouseY, msg.wheel);
break;
}
}
draw();
FlushBatchDraw();
EndBatchDraw();
Sleep(10);
}
void close()
{
closegraph();
}
};
EasyButton btn1;
EasyTextBox edit1;
Tabledialog Table1;
void size23(int size)
{
LOGFONT f;
gettextstyle(&f); // 获取当前字体设置。
f.lfHeight = size; // 设置字体高度为 48。
_tcscpy_s(f.lfFaceName, _T("楷体"));
f.lfQuality = ANTIALIASED_QUALITY; // 设置输出效果为抗锯齿。
settextstyle(&f);
setbkmode(TRANSPARENT); // 设置透明的背景。
}
void on_btn1_cleck()
{
cout << "按钮已按下:" << endl;
TCHAR cc11[] = L"按钮点击生效";
outtextxy(830, 300, cc11);
;
}
void miaoshu(int a)
{
if (a > 0)
{
int b = a - 1;
TCHAR cc11[] = L"当前选择的是:";
TCHAR cc12[] = L"时间段";
setlinecolor(WHITE); // 设置画线颜色
setbkcolor(WHITE); // 设置背景颜色
setfillcolor(WHITE); // 设置填充颜色
fillrectangle(830, 200, 1200, 200 + textheight(cc11));
size23(20);
outtextxy(830, 200, cc11);
outtextxy(830 + textwidth(cc11), 200, Time_Tchar[b]);
outtextxy(830 + textwidth(cc11) + textwidth(Time_Tchar[b]), 200, cc12);
}
}
void mousemessage()
{
ExMessage msg;
if (peekmessage(&msg))
{
int mouseX = msg.x;
int mouseY = msg.y;
if (mouseX > 950 && mouseX < 1050 && mouseY > 500 && mouseY < 550)
{
btn1.Show(2);
;
}
else
{
btn1.Show(1);
;
}
switch (msg.message)
{
case WM_LBUTTONDOWN:
if (btn1.Check(mouseX, mouseY))
{
btn1.OnMessage();
}
if (edit1.Check(mouseX, mouseY))
{
edit1.OnMessage();
}
break;
case WM_MOUSEWHEEL:
break;
}
}
Sleep(10);
}
int main()
{
initgraph(1200, 600);
setbkcolor(WHITE);
// 用背景色清空屏幕
cleardevice();
//Tabledialog Table1(0,0,800, 600);
btn1.init(950,500,1050,550,10,30,2,1,L"确定", L"-确定-", on_btn1_cleck);
edit1.init(850, 400, 1150, 450, 50, 0, 20);
Table1.init(0, 0, 800, 600);
while (true)
{
edit1.Show();
Table1.Show();
mousemessage();
miaoshu(Table1.selectRow);
}
//mousemessage();
Table1.close();
return 0;
}