rust 提示窗口

[target.'cfg(windows)'.dependencies]
winapi = { version = "0.3", features = ["winuser"] }

[dependencies]


#[cfg(windows)] extern crate winapi;
use std::io::Error;


#[cfg(windows)]
fn print_message(msg: &str) -> Result<i32, Error> {
    use std::ffi::OsStr;
    use std::iter::once;
    use std::os::windows::ffi::OsStrExt;
    use std::ptr::null_mut;
    use winapi::um::winuser::{MB_TOPMOST, MB_SYSTEMMODAL, MessageBoxW};
    let wide: Vec<u16> = OsStr::new(msg).encode_wide().chain(once(0)).collect();
    let ret = unsafe {
        MessageBoxW(null_mut(), wide.as_ptr(), wide.as_ptr(), MB_TOPMOST | MB_SYSTEMMODAL)
    };
    if ret == 0 { Err(Error::last_os_error()) }
    else { Ok(ret) }
}


#[cfg(not(windows))]
fn print_message(msg: &str) -> Result<(), Error> {
    println!("{}", msg);
    Ok(())
}
fn main() {
    print_message("Hello, world!").unwrap();
}

  

posted @ 2023-04-11 08:20  CrossPython  阅读(90)  评论(0编辑  收藏  举报