native_windows_gui/win32/
high_dpi.rs

1#[cfg(not(feature = "high-dpi"))]
2#[deprecated(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")]
3pub unsafe fn set_dpi_awareness() {
4}
5
6#[cfg(feature = "high-dpi")]
7#[deprecated(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")]
8pub unsafe fn set_dpi_awareness() {
9    use winapi::um::winuser::SetProcessDPIAware;
10    SetProcessDPIAware();
11}
12
13#[cfg(not(feature = "high-dpi"))]
14pub fn scale_factor() -> f64 {
15    return 1.0;
16}
17
18#[cfg(feature = "high-dpi")]
19pub fn scale_factor() -> f64 {
20    use winapi::um::winuser::USER_DEFAULT_SCREEN_DPI;
21    let dpi = unsafe { dpi() };
22    f64::from(dpi) / f64::from(USER_DEFAULT_SCREEN_DPI)
23}
24
25#[cfg(not(feature = "high-dpi"))]
26pub unsafe fn logical_to_physical(x: i32, y: i32) -> (i32, i32) {
27    (x, y)
28}
29
30#[cfg(feature = "high-dpi")]
31pub unsafe fn logical_to_physical(x: i32, y: i32) -> (i32, i32) {
32    use muldiv::MulDiv;
33    use winapi::um::winuser::USER_DEFAULT_SCREEN_DPI;
34    let dpi = dpi();
35    let x = x.mul_div_round(dpi, USER_DEFAULT_SCREEN_DPI).unwrap_or(x);
36    let y = y.mul_div_round(dpi, USER_DEFAULT_SCREEN_DPI).unwrap_or(y);
37    (x, y)
38}
39
40#[cfg(not(feature = "high-dpi"))]
41pub unsafe fn physical_to_logical(x: i32, y: i32) -> (i32, i32) {
42    (x, y)
43}
44
45#[cfg(feature = "high-dpi")]
46pub unsafe fn physical_to_logical(x: i32, y: i32) -> (i32, i32) {
47    use muldiv::MulDiv;
48    use winapi::um::winuser::USER_DEFAULT_SCREEN_DPI;
49    let dpi = dpi();
50    let x = x.mul_div_round(USER_DEFAULT_SCREEN_DPI, dpi).unwrap_or(x);
51    let y = y.mul_div_round(USER_DEFAULT_SCREEN_DPI, dpi).unwrap_or(y);
52    (x, y)
53}
54
55pub unsafe fn dpi() -> i32 {
56    use winapi::um::winuser::GetDC;
57    use winapi::um::wingdi::GetDeviceCaps;
58    use winapi::um::wingdi::LOGPIXELSX;
59    let screen = GetDC(std::ptr::null_mut());
60    let dpi = GetDeviceCaps(screen, LOGPIXELSX);
61    dpi
62}