EasyX的自用按钮和文本框封装

初学者封装,请各位大神指教

按钮:

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();
    }
}

  • 7
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
你可以使用EasyX图形库提供的Edit类创建一个文本框,然后在用户输入数学表达式后,使用数学库计算表达式的值,并将结果输出到窗口中。 以下是一个示例代码: ```cpp #include <graphics.h> #include <conio.h> #include <iostream> #include <string> #include <stack> #include <cmath> using namespace std; double compute(string exp) { stack<double> nums; stack<char> ops; for(int i = 0; i < exp.size(); i++) { if(exp[i] == ' ') continue; if(exp[i] >= '0' && exp[i] <= '9') { double num = 0; while(i < exp.size() && ((exp[i] >= '0' && exp[i] <= '9') || exp[i] == '.')) { if(exp[i] == '.') { i++; double frac = 1; while(i < exp.size() && exp[i] >= '0' && exp[i] <= '9') { frac /= 10; num += (exp[i] - '0') * frac; i++; } } else { num = num * 10 + (exp[i] - '0'); i++; } } nums.push(num); i--; } else if(exp[i] == '(') ops.push(exp[i]); else if(exp[i] == ')') { while(ops.top() != '(') { double num2 = nums.top(); nums.pop(); double num1 = nums.top(); nums.pop(); char op = ops.top(); ops.pop(); if(op == '+') nums.push(num1 + num2); else if(op == '-') nums.push(num1 - num2); else if(op == '*') nums.push(num1 * num2); else if(op == '/') nums.push(num1 / num2); } ops.pop(); } else { while(!ops.empty() && ((exp[i] == '*' || exp[i] == '/') || (exp[i] == '+' || exp[i] == '-') && (ops.top() == '*' || ops.top() == '/'))) { double num2 = nums.top(); nums.pop(); double num1 = nums.top(); nums.pop(); char op = ops.top(); ops.pop(); if(op == '+') nums.push(num1 + num2); else if(op == '-') nums.push(num1 - num2); else if(op == '*') nums.push(num1 * num2); else if(op == '/') nums.push(num1 / num2); } ops.push(exp[i]); } } while(!ops.empty()) { double num2 = nums.top(); nums.pop(); double num1 = nums.top(); nums.pop(); char op = ops.top(); ops.pop(); if(op == '+') nums.push(num1 + num2); else if(op == '-') nums.push(num1 - num2); else if(op == '*') nums.push(num1 * num2); else if(op == '/') nums.push(num1 / num2); } return nums.top(); } int main() { initgraph(640, 480); setbkcolor(WHITE); cleardevice(); EditBox expBox(20, 20, 400, 30, true); expBox.SetDefaultText("请输入数学表达式"); while(1) { if(expBox.IsEnter()) { string exp = expBox.GetText(); double result = compute(exp); expBox.Clear(); expBox.SetDefaultText("请输入数学表达式"); char resultStr[50]; sprintf_s(resultStr, "%.2f", result); outtextxy(20, 60, resultStr); } Sleep(10); } getch(); closegraph(); return 0; } ``` 这个示例中使用了一个compute函数,该函数可以计算一个字符串形式的数学表达式的值。在主函数中,创建了一个EditBox对象,用于接收用户输入的数学表达式,然后调用compute函数计算表达式的值,并将结果输出到窗口中。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值