Warning: file_get_contents() [function.file-get-contents]: SSL: 远程主机强迫关闭了一个现有的连接。 in D:\wwwroot\huidong\wwwroot\function.inc.php on line 884

Warning: file_get_contents(https://whois.pconline.com.cn/jsLabel.jsp?ip=127.0.0.1) [function.file-get-contents]: failed to open stream: HTTP request failed! in D:\wwwroot\huidong\wwwroot\function.inc.php on line 884
EasyX 基本控件【现有:按钮、输入框】 - huidong

huidong

首页 | 会员登录 | 关于争取 2022 寒假做出汇东网 Ver3.0.0 !
搜索文章


按钮

#include <easyx.h>

// 实现按钮控件
// 引用 https://codebus.cn/bestans/textbox-sample 有删改
class EasyButton
{
private:
    int left = 0, top = 0, right = 0, bottom = 0;    // 控件坐标
    wchar_t* text = NULL;                            // 控件内容
    void (*userfunc)() = NULL;                        // 控件消息

public:

    // 创建按钮
    // x, y 按钮位置
    // w, h 按钮大小
    void Create(int x, int y, int w, int h, LPCTSTR title, void (*func)())
    {
        int len = (int)wcslen(title) + 1;
        text = new wchar_t[len];
        wcscpy_s(text, len, title);
        left = x, top = y, right = x + w, bottom = y + h;
        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 oldlinecolor = getlinecolor();
        int oldbkcolor = getbkcolor();
        int oldfillcolor = getfillcolor();
        int oldtextcolor = gettextcolor();

        setlinecolor(BLACK);
        setbkcolor(WHITE);
        setfillcolor(WHITE);
        settextcolor(BLACK);
        fillrectangle(left, top, right, bottom);
        outtextxy(left + (right - left - textwidth(text) + 1) / 2, top + (bottom - top - textheight(text) + 1) / 2, text);

        setlinecolor(oldlinecolor);
        setbkcolor(oldbkcolor);
        setfillcolor(oldfillcolor);
        settextcolor(oldtextcolor);
    }

    void OnMessage()
    {
        if (userfunc != NULL)
            userfunc();
    }
};

void OnBtnClicked()
{
    outtextxy(200, 200, L"按下按钮");
}

int main()
{
    initgraph(640, 480);

    EasyButton btn;
    btn.Create(10, 10, 100, 50, L"Click Me!", OnBtnClicked);

    ExMessage msg;
    while (true)
    {
        msg = getmessage(EM_MOUSE);            // 获取消息输入

        if (msg.message == WM_LBUTTONDOWN)
        {
            if (btn.Check(msg.x, msg.y))
            {
                btn.OnMessage();
            }
        }
    }

    closegraph();
}

引用了 https://codebus.cn/bestans/textbox-sample 【有删改!】


输入框

#include <easyx.h>

// 实现文本框控件
// 引用 https://codebus.cn/bestans/textbox-sample 有删改
class EasyTextBox
{
private:
    int left = 0, top = 0, right = 0, bottom = 0;    // 控件坐标
    wchar_t* text = NULL;                            // 控件内容
    size_t maxlen = 0;                                // 文本框最大内容长度

public:

    // 创建文本框
    // x, y 位置
    // w, h 大小
    // max 最长输入限制
    void Create(int x, int y, int w, int h, int max)
    {
        maxlen = max;
        text = new wchar_t[maxlen];
        text[0] = 0;
        left = x, top = y, right = x + w, bottom = y + h;

        // 绘制用户界面
        Show();
    }

    ~EasyTextBox()
    {
        if (text != NULL)
            delete[] text;
    }

    LPCTSTR 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();
        int oldtextcolor = gettextcolor();

        setlinecolor(LIGHTGRAY);        // 设置画线颜色
        setbkcolor(WHITE);                // 设置背景颜色
        setfillcolor(WHITE);            // 设置填充颜色
        settextcolor(BLACK);            // 设置文本颜色
        fillrectangle(left, top, right, bottom);
        outtextxy(left + 10, top + 5, text);

        // 恢复环境值
        setlinecolor(oldlinecolor);
        setbkcolor(oldbkcolor);
        setfillcolor(oldfillcolor);
        settextcolor(oldtextcolor);
    }

    void OnMessage()
    {
        // 备份环境值
        int oldlinecolor = getlinecolor();
        int oldbkcolor = getbkcolor();
        int oldfillcolor = getfillcolor();
        int oldtextcolor = gettextcolor();

        setlinecolor(BLACK);            // 设置画线颜色
        setbkcolor(WHITE);                // 设置背景颜色
        setfillcolor(WHITE);            // 设置填充颜色
        settextcolor(BLACK);            // 设置文本颜色
        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, EM_MOUSE | EM_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(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, EM_MOUSE | EM_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);
        settextcolor(oldtextcolor);

        Show();
    }
};

int main()
{
    initgraph(640, 480);

    EasyTextBox txt;
    txt.Create(100, 100, 200, 25, 20);

    ExMessage msg;
    while (true)
    {
        msg = getmessage(EM_MOUSE);

        if (msg.message == WM_LBUTTONDOWN)
        {
            if (txt.Check(msg.x, msg.y))
            {
                txt.OnMessage();
            }
        }
    }

    closegraph();
}

引用了 https://codebus.cn/bestans/textbox-sample 【有删改!】




返回首页


Copyright (C) 2018-2024 huidong