Java教程

rust win32api EnumWindows

本文主要是介绍rust win32api EnumWindows,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

1 Cargo.toml

[package]
name = "hellowinapi"
version = "0.1.0"
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
windows = "0.19"

[build-dependencies]
windows = "0.19"

2  build.rs

fn main() {
    windows::build! {
        Windows::Win32::
        {UI::WindowsAndMessaging::{
            EnumWindows, GetWindowTextW,            
        },
        {Storage::FileSystem::GetLogicalDriveStringsW,}
    }
    };
}

3 src/main.rs

mod bindings {
    windows::include_bindings!();
}

use {bindings::Windows::Win32::{UI::WindowsAndMessaging::{EnumWindows,GetWindowTextW,},
    Foundation::{LPARAM,HWND,BOOL,PWSTR,}
}};
fn main() -> windows::Result<()> {
    unsafe { EnumWindows(Some(enum_window), LPARAM(0)).ok() }
}

extern "system" fn enum_window(window: HWND, _: LPARAM) -> BOOL {
    unsafe {
        let mut text: [u16; 512] = [0; 512];
        let len = GetWindowTextW(window, PWSTR(text.as_mut_ptr()), text.len() as i32);
        let text = String::from_utf16_lossy(&text[..len as usize]);

        if !text.is_empty() {
            println!("{}", text);
        }

        return true.into();
    }
}

 

 

 

参考:https://hub.fastgit.org/microsoft/windows-samples-rs

这篇关于rust win32api EnumWindows的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!