rust 获取和设置鼠标位置

Cargo.toml

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

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

[target.'cfg(windows)'.dependencies]
winapi = { version = "0.3", features = ["winuser"] }

main.rs

#![feature(raw_ref_macros)]
#[cfg(windows)]
extern crate winapi;

fn set_cursor_pos(x: i32, y: i32) -> bool {
    use winapi::um::winuser::SetCursorPos;
    let ret = unsafe { SetCursorPos(x, y) };
    ret == 1
}

fn get_cursor_pos() -> Option<(i32, i32)> {
    use winapi::shared::windef::POINT;
    use winapi::um::winuser::GetCursorPos;

    let mut pt = POINT { x: -1, y: -1 };
    let ret = unsafe { GetCursorPos(&mut pt) };
    if ret != 1 || pt.x == -1 && pt.y == -1 {
        None
    } else {
        Some((pt.x, pt.y))
    }
}