easyx鼠标与键盘操作失灵问题

0

win11 cb 20.03下安装easyx2021

写了一个鼠标放到选项上,左侧会出现黄色箭头,并且点击或按键盘对应序号能跳转相关页面的函数。
但是左侧箭头经常有概率瞎闪,点击和按键盘有时会失灵,没有进行页面跳转,请问一下是什么问题。

#include <stdio.h>
#include <easyx.h>
#include <graphics.h>
int main()
{
    int n=0,m=0;
    initgraph(1080,720,EX_SHOWCONSOLE);
    ExMessage msg;
    settextcolor(YELLOW);
    settextstyle(70,0,"宋体",0,0,0,true,false,false);
    outtextxy(540-textwidth("主 菜 单")/2,80,"主 菜 单");
    settextcolor(WHITE);
    settextstyle(50,0,"宋体",0,0,0,false,false,false);
    outtextxy(540-textwidth("1.柱状图")/2,200,"1.柱状图");
    outtextxy(540-textwidth("2.圆饼图")/2,320,"2.圆饼图");
    settextcolor(YELLOW);
    while(!m)
    {
        if(peekmessage(&msg,EX_MOUSE))
        {
            switch(msg.message)
            {
                case WM_MOUSEMOVE:
                    if(540-textwidth("1.柱状图")/2-textwidth("→")<=msg.x&&msg.x<=540+textwidth("1.柱状图")/2&&200<=msg.y&&msg.y<=200+textheight("柱状图"))
                       outtextxy(540-textwidth("1.柱状图")/2-textwidth("→"),200,"→");
                    if(540-textwidth("2.圆饼图")/2-textwidth("→")<=msg.x&&msg.x<=540+textwidth("2.圆饼图")/2&&320<=msg.y&&msg.y<=320+textheight("圆饼图"))
                       outtextxy(540-textwidth("2.圆饼图")/2-textwidth("→"),320,"→");
                    if(!(540-textwidth("1.柱状图")/2-textwidth("→")<=msg.x&&msg.x<=540+textwidth("1.柱状图")/2&&200<=msg.y&&msg.y<=200+textheight("柱状图")))
                       clearrectangle(540-textwidth("1.柱状图")/2-textwidth("→"),200,540-textwidth("1.柱状图")/2,200+textheight("→"));
                    if(!(540-textwidth("2.圆饼图")/2-textwidth("→")<=msg.x&&msg.x<=540+textwidth("2.圆饼图")/2&&320<=msg.y&&msg.y<=320+textheight("圆饼图")))
                        clearrectangle(540-textwidth("1.柱状图")/2-textwidth("→"),320,540-textwidth("1.柱状图")/2,320+textheight("→"));
                       break;
                case WM_LBUTTONDOWN:
                    if(540-textwidth("1.柱状图")/2<=msg.x&&msg.x<=540+textwidth("1.柱状图")/2&&200<=msg.y&&msg.y<=200+textheight("柱状图"))
                        m=1;
                    if(540-textwidth("2.圆饼图")/2<=msg.x&&msg.x<=540+textwidth("2.圆饼图")/2&&320<=msg.y&&msg.y<=320+textheight("圆饼图"))
                        m=2;
                    break;
                default:
                    break;
            }
        }
        if(peekmessage(&msg,EX_KEY))
            switch(msg.message)
            {
                case WM_KEYDOWN:
                    if(msg.vkcode==97||msg.vkcode==49)
                        m=1;
                    if(msg.vkcode==98||msg.vkcode==50)
                        m=2;
                    break;
                default:
                    break;
            }
    }
    return 0;
}
ava
原谅龙

2023-4-22

1

执行 peekmessage(&msg, EX_MOUSE)) 时,会只获取鼠标消息,同时抛弃其它消息。
同理,peekmessage(&msg, EX_KEY) 会只获取键盘消息,而抛弃其它消息。

所以,你的程序会遇到“操作失灵”的问题。

如果需要同时获取鼠标消息和键盘消息,你的程序可以这样修改:

#include <graphics.h>

int main()
{
    int n = 0, m = 0;
    initgraph(1080, 720, EX_SHOWCONSOLE);
    ExMessage msg;
    settextcolor(YELLOW);
    settextstyle(70, 0, "宋体", 0, 0, 0, true, false, false);
    outtextxy(540 - textwidth("主 菜 单") / 2, 80, "主 菜 单");
    settextcolor(WHITE);
    settextstyle(50, 0, "宋体", 0, 0, 0, false, false, false);
    outtextxy(540 - textwidth("1.柱状图") / 2, 200, "1.柱状图");
    outtextxy(540 - textwidth("2.圆饼图") / 2, 320, "2.圆饼图");
    settextcolor(YELLOW);
    while (!m)
    {
        while (peekmessage(&msg, EX_MOUSE | EX_KEY))
        {
            switch (msg.message)
            {
            case WM_MOUSEMOVE:
                if (540 - textwidth("1.柱状图") / 2 - textwidth("→") <= msg.x && msg.x <= 540 + textwidth("1.柱状图") / 2 && 200 <= msg.y && msg.y <= 200 + textheight("柱状图"))
                    outtextxy(540 - textwidth("1.柱状图") / 2 - textwidth("→"), 200, "→");
                if (540 - textwidth("2.圆饼图") / 2 - textwidth("→") <= msg.x && msg.x <= 540 + textwidth("2.圆饼图") / 2 && 320 <= msg.y && msg.y <= 320 + textheight("圆饼图"))
                    outtextxy(540 - textwidth("2.圆饼图") / 2 - textwidth("→"), 320, "→");
                if (!(540 - textwidth("1.柱状图") / 2 - textwidth("→") <= msg.x && msg.x <= 540 + textwidth("1.柱状图") / 2 && 200 <= msg.y && msg.y <= 200 + textheight("柱状图")))
                    clearrectangle(540 - textwidth("1.柱状图") / 2 - textwidth("→"), 200, 540 - textwidth("1.柱状图") / 2, 200 + textheight("→"));
                if (!(540 - textwidth("2.圆饼图") / 2 - textwidth("→") <= msg.x && msg.x <= 540 + textwidth("2.圆饼图") / 2 && 320 <= msg.y && msg.y <= 320 + textheight("圆饼图")))
                    clearrectangle(540 - textwidth("1.柱状图") / 2 - textwidth("→"), 320, 540 - textwidth("1.柱状图") / 2, 320 + textheight("→"));
                break;
            case WM_LBUTTONDOWN:
                if (540 - textwidth("1.柱状图") / 2 <= msg.x && msg.x <= 540 + textwidth("1.柱状图") / 2 && 200 <= msg.y && msg.y <= 200 + textheight("柱状图"))
                    m = 1;
                if (540 - textwidth("2.圆饼图") / 2 <= msg.x && msg.x <= 540 + textwidth("2.圆饼图") / 2 && 320 <= msg.y && msg.y <= 320 + textheight("圆饼图"))
                    m = 2;
                break;
            case WM_KEYDOWN:
                if (msg.vkcode == 97 || msg.vkcode == 49)
                    m = 1;
                if (msg.vkcode == 98 || msg.vkcode == 50)
                    m = 2;
                break;
            default:
                break;
            }
        }
        Sleep(15);
    }
    return 0;
}
ava
慢羊羊

2023-4-23

技术讨论社区