在Windows用Rust显示消息框

541 阅读1分钟

使用微软官方Rust开发包,调用WindowsAPI,显示两个消息框。

cargo new message_box
cd message_box

Cargo.toml配置如下

[package]
name = "message_box"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

 [dependencies.windows]
 version = "0.43.0"
 features = [
     "Win32_Foundation",
     "Win32_UI_WindowsAndMessaging",
 ]

源码

// src\main.rs
use windows::{
    core::*,
    Win32::UI::WindowsAndMessaging::*
};

fn main() {
    unsafe {
        MessageBoxA(None, s!("Ansi"), s!("World"), MB_OK);
        MessageBoxW(None, w!("Wide"), w!("World"), MB_OK);
    }
}

结果如下 image.png image.png

绝大部分内容源于微软[官方文档](在 Windows 上通过 Rust 进行开发的概述 | Microsoft Learn)。