Windows API and GUI in safe, idiomatic Rust.
WinSafe has:
If you're looking for a comprehensive Win32 coverage, take a look at winapi or windows crates, which are unsafe, but have everything.
WinSafe documentation: * stable release: docs.rs/winsafe * master branch: rodrigocfd.github.io/winsafe/winsafe
This crate is still in alpha stage. Below is an estimated progress of feature groups:
| Feature group | Estimated progress |
| - | - |
| User windows (main, modal and control) | |
| Native controls |
|
| Window messages |
|
| Overall Win32 APIs |
| |
Add the dependency in your Cargo.toml
:
toml
[dependencies]
winsafe = "0.0.8"
The COM modules are disabled by default, and can be enabled when needed:
| Module | Cargo.toml [dependencies]
entry |
| - | - |
| Automation | winsafe = { version = "0.0.8", features = ["autom"] }
|
| DirectShow | winsafe = { version = "0.0.8", features = ["dshow"] }
|
| IDL | winsafe = { version = "0.0.8", features = ["idl"] }
|
| Shell | winsafe = { version = "0.0.8", features = ["shell"] }
|
Note: You can find several examples in the dedicated repo: github.com/rodrigocfd/winsafe-examples
WinSafe allows you to create windows in two ways:
.res
file created with a WYSIWYG resource editor.The example below creates a window with a button programmatically. Note how the click event is handled with a closure:
```rust
use winsafe::prelude::*; use winsafe::{gui, POINT, SIZE, WinResult};
fn main() { let my = MyWindow::new(); // instantiate our main window if let Err(e) = my.wnd.run_main(None) { // ... and run it eprintln!("{}", e); } }
pub struct MyWindow { wnd: gui::WindowMain, // responsible for managing the window btn_hello: gui::Button, // a button }
impl MyWindow { pub fn new() -> MyWindow { let wnd = gui::WindowMain::new( // instantiate the window manager gui::WindowMainOpts { title: "My window title".to_owned(), size: SIZE::new(300, 150), ..Default::default() // leave all other options as default }, );
let btn_hello = gui::Button::new(
&wnd, // the window manager is the parent of our button
gui::ButtonOpts {
text: "&Click me".to_owned(),
position: POINT::new(20, 20),
..Default::default()
},
);
let new_self = Self { wnd, btn_hello };
new_self.events(); // attach our events
new_self
}
fn events(&self) {
self.btn_hello.on().bn_clicked({
let wnd = self.wnd.clone(); // clone so it can be passed into the closure
move || {
wnd.hwnd().SetWindowText("Hello, world!")?;
Ok(())
}
});
}
} ```
Licensed under MIT license, see LICENSE.md for details.