• Stars
    star
    442
  • Rank 98,677 (Top 2 %)
  • Language
    Rust
  • License
    MIT License
  • Created about 5 years ago
  • Updated 7 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Windows API and GUI in safe, idiomatic Rust.

WinSafe

Crates.io Docs.rs Lines of code License: MIT

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

These are the estimated progresses of the GUI features:

GUI feature Estimated progress
User window/dialogs (main, modal, modeless and control) 100%
Native controls 85%

Plus, below are the numbers of native FFI items implemented:

Native FFI item Count
Functions 715
Structs 203
Constants 12,935
Window messages 648
Handles 40
COM interfaces 72
COM methods 269

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.

Usage

Add the dependency in your Cargo.toml:

[dependencies]
winsafe = { version = "0.0.17", 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.

The following Cargo features are available so far:

Feature Description
comctl ComCtl32.dll, for Common Controls
comdlg ComDlg32.dll, for the old Common Dialogs
dshow DirectShow
dwm Dwmapi.dll, the Desktop Window Manager
dxgi DirectX Graphics Infrastructure
gdi Gdi32.dll, the Windows GDI
gui The WinSafe high-level GUI abstractions
kernel Kernel32.dll, Advapi32.dll and Ktmw32.dll – all others will include it
mf Media Foundation
msimg Msimg32.dll
ole OLE and basic COM support
oleaut OLE Automation
shell Shell32.dll and Shlwapi.dll, the COM-based Windows Shell
taskschd Task Scheduler
user User32.dll, the basic Windows GUI support
uxtheme UxTheme.dll, extended window theming
version Version.dll, to manipulate *.exe version info

Note that a Cargo feature may depend on other features, which will be enabled 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.17", features = ["gui"] }
#![windows_subsystem = "windows"]

use winsafe::prelude::*;
use winsafe::{gui, POINT, SIZE};

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!")?;
            Ok(())
        });
    }
}

License

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

More Repositories

1

windigo

Windows API and GUI in idiomatic Go.
Go
353
star
2

winlamb

A lightweight modern C++11 library for Win32 API, using lambdas to handle Windows messages.
C++
320
star
3

winsafe-examples

Examples of native Windows applications written in Rust with WinSafe.
Rust
57
star
4

string-tension-calc

Guitar string tension calculator with graphical plotting.
TypeScript
22
star
5

defer-lite

A lightweight high-performance implementation of Go's defer statement.
Rust
20
star
6

react-require

React with RequireJS: no build step, no Webpack, not even Node.js.
JavaScript
14
star
7

flac-lame-frontend

A native Win32 GUI to work with FLAC and LAME command line tools.
Rust
11
star
8

html5-tree-graph

Pure HTML5 and JavaScript tree graph.
JavaScript
10
star
9

chromium-peeker

A native C++11 Win32 utility to download a list of Chromium builds for Windows.
C++
9
star
10

binary-file-diff

Drag two binary files into the page, and compare them side by side.
JavaScript
8
star
11

click-lines

Simple C++11, Win32, WinLamb example using raw windows and custom controls.
C++
7
star
12

ordenador-cespe

Um script para ordenação dos resultados divulgados pelo Cespe.
5
star
13

format-comment

VSCode extension to format comments to fit a maximum number of chars on each line.
TypeScript
5
star
14

id3-padding-remover

C and Win32 program to remove padding from ID3v2 tags in MP3 files.
C
4
star
15

vscode-font-patch

An utility to patch a Visual Studio Code installation on Windows, slightly enhancing the font rendering, and fixing the autocomplete icon color.
Rust
3
star
16

react-mpa

PoC with React and Webpack for a multi-page application, JavaScript and TypeScript.
JavaScript
1
star