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

相关推荐
指尖流烟几秒前
C#调用图表的使用方法
开发语言·c#
敲代码不忘补水3 分钟前
Python 项目实践:简单的计算器
开发语言·python·json·项目实践
蒟蒻的贤12 分钟前
Web APIs 第二天
开发语言·前端·javascript
ljp_nan19 分钟前
QT --- 初识QT
开发语言·qt
ᅠᅠᅠ@24 分钟前
异常枚举;
开发语言·javascript·ecmascript
hai4058725 分钟前
Spring Boot中的响应与分层解耦架构
spring boot·后端·架构
编程版小新30 分钟前
C++初阶:STL详解(四)——vector迭代器失效问题
开发语言·c++·迭代器·vector·迭代器失效
c4fx1 小时前
Delphi5利用DLL实现窗体的重用
开发语言·delphi·dll
鸽芷咕1 小时前
【Python报错已解决】ModuleNotFoundError: No module named ‘paddle‘
开发语言·python·机器学习·bug·paddle
Jhxbdks1 小时前
C语言中的一些小知识(二)
c语言·开发语言·笔记