在 Rust 中更改 .exe 文件的图标

问题描述

所以我做了一些研究,发现了 Windows 中的 rs.exe 可以将包含名称 to_do 和 PATH 的 resources.rs 文件转换为 ico 到 res。唯一的问题是在导航到其目录并运行命令 rs resource.exe 之后。我收到错误 RC1109,我告诉我 rc.exe 找不到路径。

我做错了什么?我应该将我的 resources.rs 文件与 rc.exe 放在同一个文件夹中吗?

我是否必须以某种方式格式化文件中包含的文本?

一如既往地感谢您的帮助!

解决方法

当您运行 cargo buildcargo run 时,您可以使用 winres crate 为 .exe 设置图标。

将此添加到您的 Cargo.toml

[package]
...
build = "build.rs"

[build-dependencies]
winres = "0.1"

然后在 Cargo.toml 所在目录下创建一个 build.rs 文件,内容如下:

extern crate winres;

fn main() {
  if cfg!(target_os = "windows") {
    let mut res = winres::WindowsResource::new();
    res.set_icon("my_icon.ico"); // Replace this with the filename of your .ico file.
    res.compile().unwrap();
  }
}

然后,您也必须将 .ico 文件复制到同一目录中。不要忘记更改 build.rs 中的文件名。