[Rust GUI]0.10.0版本iced代码示例 - progress_bar

-1 字体支持

iced0.10.0 仅支持指定系统内置字体(iced默认字体中文会乱码)

iced0.10.0 手动加载字体的功能已经砍了,想手动加载就用0.9.0版本,文档0.9.0版本

想显示中文则需要运行在一个自带字体的Windows系统上。而且这个字体最好不要钱。

(Windows闲着没事不会给你放免费字体进去,都是微软自己买的,只能微软用)

如果选用的字体用户的电脑里恰好没有,iced就直接乱码给你看。

0、准备

1、安装Visual Studio C++ Build tools

1、访问微软官网下载生成工具

2、勾选这个

3、对比勾选细节

4、点击安装

5、安装完成

6、关闭Visual Studio Installer

7、重启电脑

2、安装Rust

访问Rust官网下载 RUSTUP-INIT.EXE(64位)

在 PowerShell 中运行$ENV:RUSTUP_DIST_SERVER='https://mirrors.ustc.edu.cn/rust-static';$ENV:RUSTUP_UPDATE_ROOT='https://mirrors.ustc.edu.cn/rust-static/rustup';.\rustup-init.exe,输入1并回车

3、设置cargo镜像

运行powershell -command ii (where.exe cargo).substring(0,(where.exe cargo).Length-'\bin\cargo.exe'.Length)

.cargo目录下新建文件,名为config,无后缀名,保存为以下内容

bash 复制代码
[source.crates-io]
registry = "https://github.com/rust-lang/crates.io-index"
replace-with = 'ustc'
[source.ustc]
registry = "git://mirrors.ustc.edu.cn/crates.io-index"

4、安装VSCode

访问这个👉链接:如何下载安装VSCode

安装插件:简体中文rust-analyzer(中英双语版)

5、下载并安装字体文件

下载思源黑体:下载链接

双击 SourceHanSansSC-Regular.otf 文件,点击安装

0、编程

1、使用cargo创建项目

运行cargo new iced-progress_bar;cd iced-progress_bar

2、添加板条箱iced0.10.0

运行cargo add iced@0.10.0

3、使用VSCode打开项目

运行code .

选中.\iced-progress_bar\src\main.rs激活插件,等待插件加载完毕。

5、运行

运行cargo run,等待编译完成,正常输出Hello, world!

6、编辑.\iced-progress_bar\src\main.rs

6.1、use

编辑第一部分,使用use关键字添加板条箱iced

rust 复制代码
// https://blog.csdn.net/qq_39124701/article/details/132662186
use iced::font::{Family, Weight};
use iced::widget::{button, column, progress_bar, slider, text};
use iced::{window, Element, Font, Sandbox, Settings};

6.2、Progress

编辑第二部分,添加一个结构体

rust 复制代码
#[derive(Default)]
struct Progress {
    value: f32,
}

6.3、Message

编辑第二部分,添加一个枚举

rust 复制代码
#[derive(Debug, Clone, Copy)]
enum Message {
    SliderChanged(f32),
    RunCommand,
}

6.4、impl

6.4.1、编辑第三部分,定义方法

rust 复制代码
impl Sandbox for Progress {}

6.4.2、鼠标选中该行代码,点击灯泡图标,选择Implement missing members并保存

6.4.3、将会自动生成如下代码

rust 复制代码
impl Sandbox for Progress {
    type Message;

    fn new() -> Self {
        todo!()
    }

    fn title(&self) -> String {
        todo!()
    }

    fn update(&mut self, message: Self::Message) {
        todo!()
    }

    fn view(&self) -> Element<'_, Self::Message> {
        todo!()
    }
}

6.4.4、type Message

type Message;改为type Message = Message;

6.4.5、new()

rust 复制代码
    fn new() -> Self {
        todo!()
    }

改为

rust 复制代码
    fn new() -> Self {
        Self::default()
    }

6.4.6、title()

rust 复制代码
    fn title(&self) -> String {
        todo!()
    }

改为

rust 复制代码
    fn title(&self) -> String {
        String::from("进度条 - Iced")
    }

6.4.7、update()

rust 复制代码
    fn update(&mut self, message: Self::Message) {
        todo!()
    }

改为

rust 复制代码
    fn update(&mut self, message: Message) {
        match message {
            Message::SliderChanged(x) => self.value = x,
            Message::RunCommand => {
                std::process::Command::new("cmd")
                    .args(&["/C", "start", "", "https://blog.csdn.net/qq_39124701/article/details/132662186"])
                    .spawn()
                    .expect("Failed to open URL");
            }
        }
    }

6.4.8、view()

rust 复制代码
    fn view(&self) -> Element<'_, Self::Message> {
        todo!()
    }

改为

rust 复制代码
    fn view(&self) -> Element<Message> {
        // https://blog.csdn.net/qq_39124701/article/details/132662186
        column![
            text(self.value).size(50),
            progress_bar(0.0..=100.0, self.value),
            slider(0.0..=100.0, self.value, Message::SliderChanged).step(0.01),
            button("重置为零").on_press(Message::SliderChanged(0.0)),
            button("作者:CSDN 三巧").on_press(Message::RunCommand),
        ]
        .padding(20)
        .spacing(5)
        .into()
    }

6.5、main

rust 复制代码
fn main() {
    println!("Hello, world!");
}

改为

rust 复制代码
pub fn main() -> iced::Result {
    Progress::run(Settings {
        window: window::Settings {
            size: (500, 500),
            ..Default::default()
        },
        default_font: Font {
            family: Family::Name("思源黑体"),
            weight: Weight::Normal,
            ..Default::default()
        },
        ..Default::default()
    })
}

7、运行

运行cargo run,等待编译完成,显示窗口

滑动滑块,数字变化,进度条变化

点击重置为零按钮,数字归零,进度条归零

8、构建

cargo build

生成位置:.\iced-progress_bar\target\debug\iced-progress_bar.exe

9、exe运行有黑框?

编辑.\iced-progress_bar\src\main.rs,在第一行添加#![windows_subsystem = "windows"]后重新构建即可

10、其他

iced官网
crates.io中的iced
Github上的iced
<>
<>
<>

相关推荐
扫地的小何尚8 分钟前
NVIDIA Dynamo深度解析:如何优雅地解决LLM推理中的KV缓存瓶颈
开发语言·人工智能·深度学习·机器学习·缓存·llm·nvidia
野犬寒鸦8 分钟前
多级缓存架构:性能与数据一致性的平衡处理(原理及优势详解+项目实战)
java·服务器·redis·后端·缓存
yi碗汤园1 小时前
【一文了解】C#的StringSplitOptions枚举
开发语言·前端·c#
无敌最俊朗@2 小时前
C++ 序列容器深度解析:vector、deque 与 list
开发语言·数据结构·数据库·c++·qt·list
Da Da 泓3 小时前
LinkedList模拟实现
java·开发语言·数据结构·学习·算法
Humbunklung3 小时前
VC++ 使用OpenSSL创建RSA密钥PEM文件
开发语言·c++·openssl
Humbunklung3 小时前
填坑:VC++ 采用OpenSSL 3.0接口方式生成RSA密钥
开发语言·c++·rsa·openssl 3.0
zl21878654485 小时前
Playwright同步、异步、并行、串行执行效率比较
开发语言·python·测试工具
Tony Bai5 小时前
【Go开发者的数据库设计之道】05 落地篇:Go 语言四种数据访问方案深度对比
开发语言·数据库·后端·golang
gopyer5 小时前
180课时吃透Go语言游戏后端开发3:Go语言中其他常用的数据类型
开发语言·游戏·golang·游戏后端开发