logo
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#[cfg(feature = "sys_info")]
#[cfg(target_os = "windows")]
use winapi::um::winuser::{
    GetSystemMetrics, SM_CXFULLSCREEN, SM_CXSCREEN, SM_CXVIRTUALSCREEN, SM_CYFULLSCREEN,
    SM_CYSCREEN, SM_CYVIRTUALSCREEN, SM_XVIRTUALSCREEN, SM_YVIRTUALSCREEN,
};

// #[repr(C)]
#[derive(Debug, Clone, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct MonitorInfo {
    pub xscreen: i32,
    pub yscreen: i32,
    pub cy_fullscreen: i32,
    pub cx_fullscreen: i32,
    pub cxvirtual_screen: i32,
    pub cyvirtual_screen: i32,
    pub xvirtual_screen: i32,
    pub yvirtual_screen: i32,
}

impl MonitorInfo {
    #[cfg(feature = "sys_info")]
    #[cfg(target_os = "windows")]
    pub fn new() -> Self {
        Self {
            xscreen: unsafe { GetSystemMetrics(SM_CXSCREEN) },
            yscreen: unsafe { GetSystemMetrics(SM_CYSCREEN) },
            cx_fullscreen: unsafe { GetSystemMetrics(SM_CXFULLSCREEN) },
            cy_fullscreen: unsafe { GetSystemMetrics(SM_CYFULLSCREEN) },
            cxvirtual_screen: unsafe { GetSystemMetrics(SM_CXVIRTUALSCREEN) },
            cyvirtual_screen: unsafe { GetSystemMetrics(SM_CYVIRTUALSCREEN) },
            xvirtual_screen: unsafe { GetSystemMetrics(SM_XVIRTUALSCREEN) },
            yvirtual_screen: unsafe { GetSystemMetrics(SM_YVIRTUALSCREEN) },
        }
    }
    #[cfg(not(feature = "sys_info"))]
    #[cfg(not(target_os = "windows"))]
    pub fn new() -> Self {
        Self {
            ..Default::default()
        }
    }
}

// impl MonitorInfo {
//     pub fn new() -> Self {
//         get_monitor().unwrap_or(MonitorInfo::default())
//     }
// }
// extern "C" {
//     #[cfg(target_os = "windows")]
//     fn get_monitor_info() -> MonitorInfo;
// }

// /// 获取内存信息
// pub fn get_monitor() -> Result<MonitorInfo, std::io::Error> {
//     #[cfg(target_os = "windows")]
//     {
//         Ok(unsafe { get_monitor_info() })
//     }
// }