当前位置:网站首页>FTXUI基础笔记(botton按钮组件基础)
FTXUI基础笔记(botton按钮组件基础)
2022-08-10 15:57:00 【zhangrelay】
先看原版示例程序吧:
#include <memory> // for shared_ptr, __shared_ptr_access
#include <string> // for operator+, to_string
#include "ftxui/component/captured_mouse.hpp" // for ftxui
#include "ftxui/component/component.hpp" // for Button, Horizontal, Renderer
#include "ftxui/component/component_base.hpp" // for ComponentBase
#include "ftxui/component/screen_interactive.hpp" // for ScreenInteractive
#include "ftxui/dom/elements.hpp" // for separator, gauge, text, Element, operator|, vbox, border
using namespace ftxui;
int main(int argc, const char* argv[]) {
int value = 50;
// The tree of components. This defines how to navigate using the keyboard.
auto buttons = Container::Horizontal({
Button("计数加1", [&] { value--; }),
Button("计数减1", [&] { value++; }),
});
// Modify the way to render them on screen:
auto component = Renderer(buttons, [&] {
return vbox({
text(" 数值 = " + std::to_string(value)),
separator(),
gauge(value * 0.01f),
separator(),
buttons->Render(),
}) |
border;
});
auto screen = ScreenInteractive::FitComponent();
screen.Loop(component);
return 0;
}
// Copyright 2020 Arthur Sonzogni. All rights reserved.
// Use of this source code is governed by the MIT license that can be found in
// the LICENSE file.
默认数值为50。点击按键实现加减1功能。
简要解释一下:
头文件部分
#include <memory> // for shared_ptr, __shared_ptr_access #include <string> // for operator+, to_string #include "ftxui/component/captured_mouse.hpp" // for ftxui #include "ftxui/component/component.hpp" // for Button, Horizontal, Renderer #include "ftxui/component/component_base.hpp" // for ComponentBase #include "ftxui/component/screen_interactive.hpp" // for ScreenInteractive #include "ftxui/dom/elements.hpp" // for separator, gauge, text, Element, operator|, vbox, border
按钮属性,包括点击后功能
// The tree of components. This defines how to navigate using the keyboard. auto buttons = Container::Horizontal({ Button("计数加1", [&] { value--; }), Button("计数减1", [&] { value++; }), });
整体在终端的显示:
// Modify the way to render them on screen: auto component = Renderer(buttons, [&] { return vbox({ text(" 数值 = " + std::to_string(value)), separator(), gauge(value * 0.01f), separator(), buttons->Render(), }) | border; });
程序bug,加减写反了:
自行修订即可。
这个功能太简陋了,原版复杂一些的示例如下:
#include <memory> // for shared_ptr, __shared_ptr_access
#include <string> // for operator+, to_string
#include "ftxui/component/captured_mouse.hpp" // for ftxui
#include "ftxui/component/component.hpp" // for Button, Horizontal, Renderer
#include "ftxui/component/component_base.hpp" // for ComponentBase
#include "ftxui/component/component_options.hpp" // for ButtonOption
#include "ftxui/component/screen_interactive.hpp" // for ScreenInteractive
#include "ftxui/dom/elements.hpp" // for gauge, separator, text, vbox, operator|, Element, border
#include "ftxui/screen/color.hpp" // for Color, Color::Blue, Color::Green, Color::Red
using namespace ftxui;
int main(int argc, const char* argv[]) {
int value = 50;
// The tree of components. This defines how to navigate using the keyboard.
auto buttons = Container::Horizontal({
Button(
"计数减一", [&] { value--; }, ButtonOption::Animated(Color::Red)),
Button(
"复位(默认50)", [&] { value = 50; }, ButtonOption::Animated(Color::Green)),
Button(
"计数加一", [&] { value++; }, ButtonOption::Animated(Color::Blue)),
});
// Modify the way to render them on screen:
auto component = Renderer(buttons, [&] {
return vbox({
vbox({
text(" 数值 = " + std::to_string(value)),
separator(),
gauge(value * 0.01f),
}) | border,
buttons->Render(),
});
});
auto screen = ScreenInteractive::FitComponent();
screen.Loop(component);
return 0;
}
// Copyright 2020 Arthur Sonzogni. All rights reserved.
// Use of this source code is governed by the MIT license that can be found in
// the LICENSE file.
减
复位
加
看起来效果好一些。
改变之处:
// The tree of components. This defines how to navigate using the keyboard. auto buttons = Container::Horizontal({ Button( "计数减一", [&] { value--; }, ButtonOption::Animated(Color::Red)), Button( "复位(默认50)", [&] { value = 50; }, ButtonOption::Animated(Color::Green)), Button( "计数加一", [&] { value++; }, ButtonOption::Animated(Color::Blue)), });
-End-
边栏推荐
猜你喜欢
随机推荐
cmake记录
秒杀项目收获
二叉树详解
mysql按月查询统计(统计近12个月的项目个数)
JVM学习——2——内存加载过程(类加载器)
cmake tips record
为什么中国的数字是四位一进,而西方的是三位一进?
Reids 源码导读
Gif动图怎么用视频做?一键在线完成视频转gif制作
推荐几款最好用的MySQL开源客户端,建议收藏!
photoshop入门教程
Asterisk SIP media path
接口测试中,应不应该用数据库
视频转成gif动图怎么操作?仅需三步在线完成视频转gif
利用SparkLauncher 提交Job
虚拟电厂可视化大屏,深挖痛点精准减碳
Pigsty:开箱即用的开源数据库发行版
超越神经缩放法则:通过数据剪枝
2025年推出 奥迪透露将推出大型SUV产品
WinUI 3 Fundamentals 5小时教学视频