Featured image of post 在 Rust 中顯示 MessageBox

在 Rust 中顯示 MessageBox

您可以按照以下步驟在 Rust 中顯示 MessageBox。

  1. 安裝 Rust。 參考 Rust 入門
  2. 在命令提示字元中執行 cargo new --bin MessageBox
  3. 進入 MessageBox 目錄。
  4. 打開 Cargo.toml 並修改如下。
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
[package]
name = "hello_world"
version = "0.1.0"
edition = "2021"

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

[dependencies]
winapi = "0.2.7"
user32-sys = "0.2.0"
  1. 打開 src\main.rs 並修改如下。
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
extern crate user32;
extern crate winapi;

use std::ffi::CString;
use user32::MessageBoxA;
use winapi::winuser::{MB_OK, MB_ICONINFORMATION};

fn main() {
    let lp_text = CString::new("Hello, world!").unwrap();
    let lp_caption = CString::new("MessageBox Example").unwrap();

    unsafe {
        MessageBoxA(
            std::ptr::null_mut(),
            lp_text.as_ptr(),
            lp_caption.as_ptr(),
            MB_OK | MB_ICONINFORMATION
        );
    }
}
  1. 在命令提示字元中執行 cargo runimg.png

  2. 如果您想建立發布版本,請執行 cargo build --release

參考

Hello World MesssageBox example in Rust

comments powered by Disqus
使用 Hugo 建立
主題 StackJimmy 設計