rust 获取鼠标位置及颜色

winapi = {version = "0.3.9",features = ["winuser"]}
use std::ptr::null_mut;

use winapi::um::wingdi::{ GetBValue, GetGValue, GetPixel, GetRValue};
use winapi::um::winuser::{GetCursorPos, GetDC, ReleaseDC};
use winapi::shared::windef::POINT;


// 获取鼠标位置
// GetCursorPos is a function from winapi
pub fn get_cursor_pos() -> POINT {
    let mut point = POINT { x: 0, y: 0 };
    unsafe {
        GetCursorPos(&mut point);
    }
    point
}


// 获取像素颜色
#[derive(Debug)]
pub struct Color {
    r: u8,
    g: u8,
    b: u8,
    a: u8,
}
pub fn get_pixel_color(point: POINT) -> Color {
    // let point = get_cursor_pos();
    unsafe {
        let hdc = GetDC(null_mut());
        let color_value = GetPixel(hdc, point.x, point.y);
        ReleaseDC(null_mut(), hdc);
        let color = Color {
            r: GetRValue(color_value),
            g: GetGValue(color_value),
            b: GetBValue(color_value),
            a: 0,
        };
        color
    }
}
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容