Jade Ellis
Jade Ellis

Reputation: 1562

Create a popup in Rust using Windows APIs

I would like to create a dialog box or popup in Rust in a similar fashion to PowerShell.

I'm struggling to find documentation on the Windows APIs for this. The closest I've found so far is the CreateDialogA macro (Rust winapi equivalent).

I've found some things on it such as Creating a New Dialog Box (C++), but most of it is not applicable to a Rust project.

Perhaps dialoge template is relevant?

Upvotes: 1

Views: 3458

Answers (1)

Fredrick Brennan
Fredrick Brennan

Reputation: 7357

main.rs

extern crate winapi;

use std::ptr::null_mut as NULL;
use winapi::um::winuser;

fn main() {
    let l_msg: Vec<u16> = "Wassa wassa wassup\0".encode_utf16().collect();
    let l_title: Vec<u16> = "\u{20BF}itconnect\0".encode_utf16().collect();

    unsafe {
        winuser::MessageBoxW(NULL(), l_msg.as_ptr(), l_title.as_ptr(), winuser::MB_OK | winuser::MB_ICONINFORMATION);
    }
}

This uses the MessageBoxW function.

The argument winuser::MB_OK can be winuser::MB_OK, winuser::MB_OKCANCEL, winuser::MB_ABORTRETRYIGNORE, winuser::MB_YESNOCANCEL, winuser::MB_YESNO, winuser::MB_RETRYCANCEL or winuser::MB_CANCELTRYCONTINUE.

The argument winuser::MB_ICONINFORMATION can be winuser::MB_ICONHAND, winuser::MB_ICONQUESTION, winuser:: MB_ICONEXCLAMATION or winuser::MB_ICONASTERISK.

Cargo.toml should include:

[dependencies.winapi]
version = "0.3"
features = ["winuser"]

popup

Upvotes: 5

Related Questions