[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
<>
<>
<>

相关推荐
qq. 280403398423 分钟前
js 原型链分析
开发语言·javascript·ecmascript
鹿衔`28 分钟前
Flask入门
后端·python·flask
Elnaij29 分钟前
从C++开始的编程生活(13)——list和浅谈stack、queue
开发语言·c++
q***07142 小时前
Spring Boot 多数据源解决方案:dynamic-datasource-spring-boot-starter 的奥秘(上)
java·spring boot·后端
q***49863 小时前
Spring Boot 3.4 正式发布,结构化日志!
java·spring boot·后端
一晌小贪欢5 小时前
【Python数据分析】数据分析与可视化
开发语言·python·数据分析·数据可视化·数据清洗
IT古董5 小时前
Windows 11 专业版 安装与配置 Docker Desktop 保姆级手册(包成功永久免关注免VIP)
windows·docker·容器
alwaysrun6 小时前
Rust中元组详解
rust·元组·tuple·解构
ftpeak6 小时前
Tauri开发手记——1.开发环境
rust·tauri
草莓火锅6 小时前
用c++使输入的数字各个位上数字反转得到一个新数
开发语言·c++·算法