rust 获取和设置鼠标位置

180it 2024-02-27 PM 287℃ 0条
    
#[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))
    }
}



  
fn main() {

//set_cursor_pos(0, 0);

let s=get_cursor_pos();
eprintln!("{:?}", s);  

    // 创建一个元组  
  //let (x, y) = s;  

  let Some((x, y)) = s else { todo!() };
    // 分别输出元组的每个元素  
    println!("x: {}", x);  
    println!("y: {}", y);  


}


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"] }
支付宝打赏支付宝打赏 微信打赏微信打赏

如果文章或资源对您有帮助,欢迎打赏作者。一路走来,感谢有您!

标签: none

rust 获取和设置鼠标位置