-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、對比勾選細節(jié)
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
,無后綴名,保存為以下內容
[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創(chuàng)建項目
運行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
激活插件,等待插件加載完畢。
4、運行
運行cargo run
,等待編譯完成,正常輸出Hello, world!
5、編輯.\iced-progress_bar\src\main.rs
5.1、use
編輯第一部分,使用use
關鍵字添加板條箱iced
// 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};
5.2、Progress
編輯第二部分,添加一個結構體
#[derive(Default)]
struct Progress {
value: f32,
}
5.3、Message
編輯第二部分,添加一個枚舉
#[derive(Debug, Clone, Copy)]
enum Message {
SliderChanged(f32),
RunCommand,
}
5.4、impl
5.4.1、編輯第三部分,定義方法
impl Sandbox for Progress {}
5.4.2、鼠標選中該行代碼,點擊燈泡圖標,選擇Implement missing members
并保存
5.4.3、將會自動生成如下代碼
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!()
}
}
5.4.4、type Message
將type Message;
改為type Message = Message;
5.4.5、new()
將
fn new() -> Self {
todo!()
}
改為
fn new() -> Self {
Self::default()
}
5.4.6、title()
將
fn title(&self) -> String {
todo!()
}
改為
fn title(&self) -> String {
String::from("進度條 - Iced")
}
5.4.7、update()
將
fn update(&mut self, message: Self::Message) {
todo!()
}
改為
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");
}
}
}
5.4.8、view()
將
fn view(&self) -> Element<'_, Self::Message> {
todo!()
}
改為
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()
}
5.5、main
將
fn main() {
println!("Hello, world!");
}
改為
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()
})
}
6、運行
運行cargo run
,等待編譯完成,顯示窗口
滑動滑塊,數字變化,進度條變化
點擊重置為零按鈕,數字歸零,進度條歸零
7、構建
cargo build
生成位置:.\iced-progress_bar\target\debug\iced-progress_bar.exe
8、exe運行有黑框?
編輯.\iced-progress_bar\src\main.rs
,在第一行添加#![windows_subsystem = "windows"]
后重新構建即可文章來源:http://www.zghlxwxcb.cn/news/detail-701056.html
9、其他
iced官網
crates.io中的iced
Github上的iced文章來源地址http://www.zghlxwxcb.cn/news/detail-701056.html
到了這里,關于[Rust GUI]0.10.0版本iced代碼示例 - progress_bar的文章就介紹完了。如果您還想了解更多內容,請在右上角搜索TOY模板網以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網!