初学者封装,请各位大神指教
按钮:
class EasyButton
{
private:
int left = 0, top = 0, right = 0, bottom = 0; // 控件坐标
TCHAR* text = NULL; // 控件内容
void (*userfunc)() = NULL; // 控件消息
//float scale=1.0f; //缩放比例
bool checkMouse; //是否在按钮上方
public:
void Create(int x1, int y1, int x2, int y2, const TCHAR* title, void (*func)())
{
text = new TCHAR[_tcslen(title) + 1];
_tcscpy(text, title);
left = x1, top = y1, right = x2, bottom = y2;
userfunc = func;
// 绘制用户界面
Show();
}
~EasyButton()
{
if (text != NULL)
delete[] text;
}
bool Check(int x, int y)
{
/*checkMouse= (left <= x && x <= right && top <= y && y <= bottom);
Show();*/
/*checkMouse = (left <= x && x <= right && top <= y && y <= bottom);*/
return (left <= x && x <= right && top <= y && y <= bottom);
}
// 绘制界面
void Show()
{
int oldlinecolor = getlinecolor();
int oldbkcolor = getbkcolor();
int oldfillcolor = getfillcolor();
// setlinecolor(BLACK); // 设置画线颜色
//setbkcolor(WHITE); // 设置背景颜色
//setfillcolor(BLACK); // 设置填充颜色
// setlinestyle(PS_SOLID, 2);
// setfillcolor(WHITE);
if (checkMouse ==true)
{
setlinecolor(RGB(0, 120, 215));
setfillcolor(RGB(229, 241, 251));
//scale = 0.8f;
}
else
{
setlinecolor(RGB(173, 173, 173));
setfillcolor(RGB(225, 225, 225));
//scale = 1.0f;
}
fillrectangle(left, top, right, bottom);
settextcolor(BLACK);
setbkmode(TRANSPARENT);
outtextxy(left + (right - left - textwidth(text) + 1) / 2, top + (bottom - top - textheight(text) + 1) / 2, text);
setlinecolor(oldlinecolor);
setbkcolor(oldbkcolor);
setfillcolor(oldfillcolor);
}
void OnMessage()//双运行实现点击效果
{
checkMouse = true;
Show();
Sleep(20);
if (userfunc != NULL)
userfunc();
checkMouse = false;
Show();
}
int ExitMessage()
{
checkMouse = true;
Show();
Sleep(20);
if (userfunc != NULL)
userfunc();
checkMouse = false;
clearrectangle(left, top, right, bottom);
return 0;
}
}
文本框(平时当文本转参用) :
class EasyTextBox
{
private:
int left = 0, top = 0, right = 0, bottom = 0; // 控件坐标
TCHAR* text = NULL; // 控件内容
size_t maxlen = 0; // 文本框最大内容长度
char* text2 = NULL;
char* text3 = NULL;
char* text4 = NULL;
double re;
double re2;
double re3;
double re4;
int success = 0;
public:
void Create(int x1, int y1, int x2, int y2, int max)
{
maxlen = max;
text = new TCHAR[maxlen];
text[0] = 0;
left = x1, top = y1, right = x2, bottom = y2;
// 绘制用户界面
Show();
}
~EasyTextBox()
{
if (text != NULL)
delete[] text;
}
TCHAR* Text()
{
return text;
}
char* Text2()
{
return text2;
}
char* Text3()
{
return text3;
}
char* Text4()
{
return text4;
}
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();
setlinecolor(LIGHTGRAY); // 设置画线颜色
setbkcolor(0xeeeeee); // 设置背景颜色
setfillcolor(0xeeeeee); // 设置填充颜色
fillrectangle(left, top, right, bottom);
outtextxy(left + 10, top + 5, text);
/*outtextxy(left + (right - left - textwidth(text) + 1) / 2, top + (bottom - top - textheight(text) + 1) / 2, text);*/
// 恢复环境值
setlinecolor(oldlinecolor);
setbkcolor(oldbkcolor);
setfillcolor(oldfillcolor);
}
void GetMessage1(int max,char *str)
{
maxlen = max;
text = new TCHAR[maxlen];
strcpy(text, str);
}
void GetMessage2(int max, char* str)
{
maxlen = max;
text2 = new char[maxlen];
strcpy(text2, str);
}
void GetMessage3(int max, char* str)
{
maxlen = max;
text3 = new char[maxlen];
strcpy(text3, str);
}
void GetMessage4(int max, char* str)
{
maxlen = max;
text4 = new char[maxlen];
strcpy(text4, str);
}
void ClearMessage(int max)
{
maxlen = max;
text[maxlen] = NULL;
size_t len = _tcslen(text);
int width = textwidth(text);
for(;len<=maxlen&&len>0;len--)
{
text[len - 1] = 0;
width = textwidth(text);
clearrectangle(left + 10 + width, top + 1, right - 1, bottom - 1);
}
Show();
}
int TXT_MAXreturn()
{
size_t len = _tcslen(text);
if (len == maxlen - 1)
return 1;
else
return 0;
}
void Result(int input)
{
success +=input;
}
int Result_get()
{
return success;
}
void Clear_Result()
{
success =0;
}
void Value(double input)
{
re = input;
}
void Value2(double input)
{
re2 = input;
}
void Value3(double input)
{
re3 = input;
}
void Value4(double input)
{
re4 = input;
}
double Value_get()
{
return re;
}
double Value_get2()
{
return re2;
}
double Value_get3()
{
return re3;
}
double Value_get4()
{
return re4;
}
void OnMessage()
{
// 备份环境值
int oldlinecolor = getlinecolor();
int oldbkcolor = getbkcolor();
int oldfillcolor = getfillcolor();
setlinecolor(BLACK); // 设置画线颜色
setbkcolor(WHITE); // 设置背景颜色
setfillcolor(WHITE); // 设置填充颜色
fillrectangle(left, top, right, bottom);
outtextxy(left + 10, top + 5, 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 = _tcslen(text);
switch (msg.ch)
{
case '\b': // 用户按退格键,删掉一个字符
if (len > 0)
{
text[len - 1] = 0;
width = textwidth(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(text); // 重新计算文本框宽度
counter = 0;
outtextxy(left + 10, top + 5, 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(oldlinecolor);
setbkcolor(oldbkcolor);
setfillcolor(oldfillcolor);
Show();
}
}