你的浏览器版本过低,可能导致网站不能正常访问!
为了你能正常使用网站功能,请使用这些浏览器。

【STM32H745I-DISCO】TouchGFX探索——3、触屏滑屏操作与中文显示

[复制链接]
网络孤客 发布时间:2025-3-7 11:20

继续以Button Example案例为基础,删除了按键,移动显示到屏幕中央,增加box控件,把Alpha通道设置为0(全透明)

box.jpg

修改TextArea控件,Buffer大小设置为10,文字改为middle,调整字体大小,并设置文字范围

字体.jpg

MainView.hpp中设置

    virtual void handleGestureEvent(const touchgfx::GestureEvent& event);   //滑屏事件
    virtual void updateGFXElements();     //更新显示

MainView.cpp中添加cpp

// 处理手势事件
void MainView::handleGestureEvent(const touchgfx::GestureEvent& event)
{
    if (event.getType() == touchgfx::GestureEvent::SWIPE_HORIZONTAL)
    {
        if (event.getVelocity() > 0)
        {
            // 向右滑动
            touchgfx::Unicode::snprintf(MsgTextBuffer, MSGTEXT_SIZE, "%s", "rightward");
        }
        else
        {
            // 向左滑动
            touchgfx::Unicode::snprintf(MsgTextBuffer, MSGTEXT_SIZE, "%s", "leftward");
        }
    }
    else if (event.getType() == touchgfx::GestureEvent::SWIPE_VERTICAL)
    {
        if (event.getVelocity() > 0)
        {
            // 向下滑动
            touchgfx::Unicode::snprintf(MsgTextBuffer, MSGTEXT_SIZE, "%s", "downward");
        }
        else
        {
            // 向上滑动
            touchgfx::Unicode::snprintf(MsgTextBuffer, MSGTEXT_SIZE, "%s", "upward");
        }
    }
}

void MainView::updateGFXElements()
{
    MsgText.invalidate();
    Toucharea.invalidate();
}

保存,接板运行,结果一堆错误。

发现,改了字体要把generated文件夹删除,从新使用Designer生成代码

删除.jpg

我仍不满足英文显示,想显示中文

中文字体.jpg

代码:

// 处理手势事件 void MainView::handleGestureEvent(const touchgfx::GestureEvent& event) { // 定义 UnicodeChar 类型的常量数组来存储中文字符串 static const touchgfx::Unicode::UnicodeChar RIGHT[] = { L'向', L'右', 0 }; static const touchgfx::Unicode::UnicodeChar LEFT[] = { L'向', L'左', 0 }; static const touchgfx::Unicode::UnicodeChar DOWN[] = { L'向', L'下', 0 }; static const touchgfx::Unicode::UnicodeChar UP[] = { L'向', L'上', 0 };

if (event.getType() == touchgfx::GestureEvent::SWIPE_HORIZONTAL) { if (event.getVelocity() > 0) { // 向右滑动 touchgfx::Unicode::snprintf(MsgTextBuffer, MSGTEXT_SIZE, "%s", RIGHT); } else { // 向左滑动 touchgfx::Unicode::snprintf(MsgTextBuffer, MSGTEXT_SIZE, "%s", LEFT); } } else if (event.getType() == touchgfx::GestureEvent::SWIPE_VERTICAL) { if (event.getVelocity() > 0) { // 向下滑动 touchgfx::Unicode::snprintf(MsgTextBuffer, MSGTEXT_SIZE, "%s", DOWN); } else { // 向上滑动 touchgfx::Unicode::snprintf(MsgTextBuffer, MSGTEXT_SIZE, "%s", UP); } } }

效果:

触摸测试.gif
收藏 评论4 发布时间:2025-3-7 11:20

举报

4个回答
网络孤客 回答时间:3 天前

这编辑器太无语了。

再发一次最后代码

// 处理手势事件
void MainView::handleGestureEvent(const touchgfx::GestureEvent& event)
{
    // 定义 UnicodeChar 类型的常量数组来存储中文字符串
    static const touchgfx::Unicode::UnicodeChar RIGHT[] = { L'向', L'右', 0 };
    static const touchgfx::Unicode::UnicodeChar LEFT[] = { L'向', L'左', 0 };
    static const touchgfx::Unicode::UnicodeChar DOWN[] = { L'向', L'下', 0 };
    static const touchgfx::Unicode::UnicodeChar UP[] = { L'向', L'上', 0 };

    if (event.getType() == touchgfx::GestureEvent::SWIPE_HORIZONTAL)
    {
        if (event.getVelocity() > 0)
        {
            // 向右滑动
            touchgfx::Unicode::snprintf(MsgTextBuffer, MSGTEXT_SIZE, "%s", RIGHT);
        }
        else
        {
            // 向左滑动
            touchgfx::Unicode::snprintf(MsgTextBuffer, MSGTEXT_SIZE, "%s", LEFT);
        }
    }
    else if (event.getType() == touchgfx::GestureEvent::SWIPE_VERTICAL)
    {
        if (event.getVelocity() > 0)
        {
            // 向下滑动
            touchgfx::Unicode::snprintf(MsgTextBuffer, MSGTEXT_SIZE, "%s", DOWN);
        }
        else
        {
            // 向上滑动
            touchgfx::Unicode::snprintf(MsgTextBuffer, MSGTEXT_SIZE, "%s", UP);
        }
    }
}

探讨:

为什么我使用

static const touchgfx::Unicode::UnicodeChar RIGHT[] = { L'向', L'右', 0 };
touchgfx::Unicode::snprintf(MsgTextBuffer, MSGTEXT_SIZE, "%s", RIGHT);

而不使用

Unicode::snprintf(MsgTextBuffer, this->MSGTEXT_SIZE,L"向右");

因为使用时,只能显示第一个字符,一直解决不了,查看Buffer中,Buffer[0]为第一个字符值,Buffer[1]之后全为0,有会的弟兄能否告知,谢谢

STMCU-管管 回答时间:3 天前

加个收拾传感器,感觉你可以做个类似油烟机控制面板的UI

网络孤客 回答时间:3 天前

STMCU-管管 发表于 2025-3-7 16:58
加个收拾传感器,感觉你可以做个类似油烟机控制面板的UI

我对油烟机不感兴趣,还是做回我的2048游戏吧😄

不过这个编辑界面会出些问题,希望能完善。

网络孤客 回答时间:昨天 20:16

有了滑屏操作,移植个2048已没有技术问题了,不再另开分享。

与小屏不同,我每个格做一个image控件

  • 没方块时设为不可见

  • 数值变化,载入不同图片

2048.gif

所属标签

相似分享

官网相关资源

关于
我们是谁
投资者关系
意法半导体可持续发展举措
创新与技术
意法半导体官网
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
官方最新发布
STM32N6 AI生态系统
STM32MCU,MPU高性能GUI
ST ACEPACK电源模块
意法半导体生物传感器
STM32Cube扩展软件包
关注我们
st-img 微信公众号
st-img 手机版