Skip to main content

native_windows_gui2/win32/
high_dpi.rs

1#[cfg(not(feature = "high-dpi"))]
2#[deprecated(
3    note = "Specifying the default process DPI awareness via API is not recommended. Use the '<dpiAware>true</dpiAware>' setting in the application manifest. https://docs.microsoft.com/ru-ru/windows/win32/hidpi/setting-the-default-dpi-awareness-for-a-process"
4)]
5pub unsafe fn set_dpi_awareness() {}
6
7#[cfg(feature = "high-dpi")]
8#[deprecated(
9    note = "Specifying the default process DPI awareness via API is not recommended. Use the '<dpiAware>true</dpiAware>' setting in the application manifest. https://docs.microsoft.com/ru-ru/windows/win32/hidpi/setting-the-default-dpi-awareness-for-a-process"
10)]
11pub unsafe fn set_dpi_awareness() {
12    use winapi::um::winuser::SetProcessDPIAware;
13    unsafe {
14        SetProcessDPIAware();
15    }
16}
17
18#[cfg(not(feature = "high-dpi"))]
19pub fn scale_factor() -> f64 {
20    return 1.0;
21}
22
23#[cfg(feature = "high-dpi")]
24pub fn scale_factor() -> f64 {
25    use winapi::um::winuser::USER_DEFAULT_SCREEN_DPI;
26    let dpi = dpi();
27    f64::from(dpi) / f64::from(USER_DEFAULT_SCREEN_DPI)
28}
29
30#[cfg(not(feature = "high-dpi"))]
31pub fn logical_to_physical(x: i32, y: i32) -> (i32, i32) {
32    (x, y)
33}
34
35#[cfg(feature = "high-dpi")]
36pub fn logical_to_physical(x: i32, y: i32) -> (i32, i32) {
37    use muldiv::MulDiv;
38    use winapi::um::winuser::USER_DEFAULT_SCREEN_DPI;
39    let dpi = dpi();
40    let x = x.mul_div_round(dpi, USER_DEFAULT_SCREEN_DPI).unwrap_or(x);
41    let y = y.mul_div_round(dpi, USER_DEFAULT_SCREEN_DPI).unwrap_or(y);
42    (x, y)
43}
44
45#[cfg(not(feature = "high-dpi"))]
46pub fn physical_to_logical(x: i32, y: i32) -> (i32, i32) {
47    (x, y)
48}
49
50#[cfg(feature = "high-dpi")]
51pub fn physical_to_logical(x: i32, y: i32) -> (i32, i32) {
52    use muldiv::MulDiv;
53    use winapi::um::winuser::USER_DEFAULT_SCREEN_DPI;
54    let dpi = dpi();
55    let x = x.mul_div_round(USER_DEFAULT_SCREEN_DPI, dpi).unwrap_or(x);
56    let y = y.mul_div_round(USER_DEFAULT_SCREEN_DPI, dpi).unwrap_or(y);
57    (x, y)
58}
59
60pub fn dpi() -> i32 {
61    use winapi::um::wingdi::GetDeviceCaps;
62    use winapi::um::wingdi::LOGPIXELSX;
63    use winapi::um::winuser::GetDC;
64    let screen = unsafe { GetDC(std::ptr::null_mut()) };
65    let dpi = unsafe { GetDeviceCaps(screen, LOGPIXELSX) };
66    dpi
67}