如何获得屏幕/显示分辨率?

How do I get the screen / display resolution?

我目前正在用 Rust (Piston Crate) 开发一款游戏,我想将它创建的 Window 居中。它允许我更改 window 从屏幕左上角的偏移量。我想获得操作系统工作的分辨率(屏幕/显示器/显示器分辨率)并以此为基础 window 居中。

例如在Python中,通过使用pyautogui,您可以使用pyautogui.size()获得屏幕分辨率。

在Java中,通过使用java.awt,您可以使用Toolkit.getDefaultToolkit().getScreenSize()获得屏幕分辨率。

我可以使用 Rust 中的类似内容吗? (std- 或任何外部板条箱)

这取决于您使用的后端。 例如,如果你使用 Winit / Glutin 后端,你可以使用 MonitorHandle 结构,它有一个 size() 方法。

文档:https://docs.rs/winit/0.26.1/winit/monitor/struct.MonitorHandle.html

或 Glutin https://docs.rs/glutin/0.28.0/glutin/window/struct.Window.html#method.available_monitors

Glutin 模块还有一个dpi 模块可以提供信息。 https://docs.rs/glutin/0.28.0/glutin/dpi/index.html

如果你使用 SDL2 后端,你可以看看 sdl2_sys 模块 SDL_HINT_RENDER_LOGICAL_SIZE https://docs.rs/sdl2-sys/0.35.2/sdl2_sys/constant.SDL_HINT_RENDER_LOGICAL_SIZE_MODE.html