#winapi

winsafe

Windows API and GUI in safe, idiomatic Rust

22 releases

0.0.22 Aug 1, 2024
0.0.21 Jun 1, 2024
0.0.20 Mar 1, 2024
0.0.18 Oct 1, 2023
0.0.1 Oct 28, 2019

#9 in Windows APIs

Download history 65179/week @ 2024-08-15 68179/week @ 2024-08-22 65397/week @ 2024-08-29 73786/week @ 2024-09-05 72456/week @ 2024-09-12 70119/week @ 2024-09-19 82562/week @ 2024-09-26 89489/week @ 2024-10-03 110205/week @ 2024-10-10 223294/week @ 2024-10-17 261930/week @ 2024-10-24 223894/week @ 2024-10-31 249631/week @ 2024-11-07 228017/week @ 2024-11-14 201695/week @ 2024-11-21 110846/week @ 2024-11-28

832,429 downloads per month
Used in 637 crates (7 directly)

MIT license

3MB
66K SLoC

WinSafe

Crates.io Crates.io total downloads License: MIT Lines of code

Windows API and GUI in safe, idiomatic Rust.

WinSafe has:

  • low-level Win32 API constants, functions and structs;
  • high-level structs to build native Win32 GUI applications.

WinSafe documentation:

Branch Docs
Stable docs.rs/winsafe
Nightly (master) rodrigocfd.github.io/winsafe/winsafe

Current status

Native FFI items implemented

Native FFI item Count
Functions 783
Structs 237
Constants 13,417
Window messages 655
Handles 45
COM interfaces 90
COM methods 541

Although WinSafe already has a lot of Win32 APIs, it doesn't have everything, simply because Win32 API is gigantic. So if you're looking for a comprehensive Win32 coverage, take a look at winapi or windows crates, which are unsafe, but have everything.

High-level GUI controls implemented

  • User custom window/dialog – main, modal, modeless, control, message-only.
  • Native controls – button, check box, combo box, date and time picker, edit, header, label, list box, list view, month calendar, progress bar, radio button, status bar, tab, track bar, tree view, up down.

Usage

Add the dependency in your Cargo.toml:

[dependencies]
winsafe = { version = "0.0.22", features = [] }

You can, alternatively, use the Nightly (master) branch directly, to get the latest features right away:

[dependencies]
winsafe = { git = "https://github.com/rodrigocfd/winsafe", features = [] }

Then you must enable the Cargo features you want to be included – these modules are named after native Windows DLL and library names, mostly.

Cargo features

The APIs in WinSafe are split into Cargo features to speed up compilation time. Only the features you include will be compiled.

The following Cargo features are available so far:

Feature Description
advapi Advapi32.dll and Ktmw32.dll, advanced kernel functions
comctl ComCtl32.dll, the Common Controls
dshow DirectShow
dwm Desktop Window Manager
dxgi DirectX Graphics Infrastructure
gdi Gdi32.dll, the Windows GDI
gui The WinSafe high-level GUI abstractions
kernel Kernel32.dll, basic kernel functions
mf Media Foundation
ole Basic OLE/COM support
oleaut OLE Automation
raw-dylib Enables raw-dylib linking
shell Shell32.dll, Shlwapi.dll, and Userenv.dll, the COM-based Windows Shell
taskschd Task Scheduler
user User32.dll and ComDlg32.dll, the basic Windows GUI support
uxtheme UxTheme.dll, extended window theming
version Version.dll, to manipulate *.exe version info

Don't worry about including dependency features. Once you use a feature, Cargo will add and resolve all dependencies automatically.

Example

Note: You can find several examples in the dedicated repo: github.com/rodrigocfd/winsafe-examples

WinSafe allows you to create windows in two ways:

  • programmatically defining parameters; or
  • loading dialogs from a .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:

Example 01

[dependencies]
winsafe = { version = "0.0.22", features = ["gui"] }
#![windows_subsystem = "windows"]

use winsafe::{self as w, prelude::*, gui};

fn main() {
    let my = MyWindow::new(); // instantiate our main window
    if let Err(e) = my.wnd.run_main(None) { // ... and run it
        eprintln!("{}", e);
    }
}


#[derive(Clone)]
pub struct MyWindow {
    wnd:       gui::WindowMain, // responsible for managing the window
    btn_hello: gui::Button,     // a button
}

impl MyWindow {
    pub fn new() -> Self {
        let wnd = gui::WindowMain::new( // instantiate the window manager
            gui::WindowMainOpts {
                title: "My window title".to_owned(),
                size: (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: (20, 20),
                ..Default::default()
            },
        );

        let new_self = Self { wnd, btn_hello };
        new_self.events(); // attach our events
        new_self
    }

    fn events(&self) {
        let wnd = self.wnd.clone(); // clone so it can be passed into the closure
        self.btn_hello.on().bn_clicked(move || {
            wnd.hwnd().SetWindowText("Hello, world!")?; // call native Windows API
            Ok(())
        });
    }
}

License

Licensed under MIT license, see LICENSE.md for details.

No runtime deps

Features