简体   繁体   English

如何检测当前屏幕分辨率?

[英]How to detect the current screen resolution?

How do I from Winapi (in C or C++) detect the current screen resolution?我如何从 Winapi(在 C 或 C++ 中)检测当前的屏幕分辨率?

Some background:一些背景:

I want to start a new OpenGL fullscreen window, but want it open with the same horizontal and vertical size which the desktop already is set to.我想启动一个新的 OpenGL 全屏窗口,但希望它以桌面已设置的相同水平和垂直大小打开。 (Now when everyone uses LCD screens, I figured this is the best way to get the native resolution of the screen.) (现在每个人都使用 LCD 屏幕时,我认为这是获得屏幕原始分辨率的最佳方法。)

I don't desperately need to also know the desktop color depth, although that would be a nice bonus.我并不迫切需要知道桌面颜色深度,尽管这将是一个不错的奖励。

  • Size of the primary monitor: GetSystemMetrics SM_CXSCREEN / SM_CYSCREEN (GetDeviceCaps can also be used)主监视器的大小: GetSystemMetrics SM_CXSCREEN / SM_CYSCREEN(也可以使用 GetDeviceCaps)
  • Size of all monitors (combined): GetSystemMetrics SM_CX/YVIRTUALSCREEN所有监视器的大小(组合):GetSystemMetrics SM_CX/YVIRTUALSCREEN
  • Size of work area (screen excluding taskbar and other docked bars) on primary monitor: SystemParametersInfo SPI_GETWORKAREA主监视器上的工作区大小(屏幕不包括任务栏和其他停靠栏): SystemParametersInfo SPI_GETWORKAREA
  • Size of a specific monitor (work area and "screen"): GetMonitorInfo特定监视器的大小(工作区和“屏幕”): GetMonitorInfo

Edit: It is important to remember that a monitor does not always "begin" at 0x0 so just knowing the size is not enough to position your window.编辑:重要的是要记住,监视器并不总是从 0x0“开始”,因此仅知道大小不足以定位您的窗口。 You can use MonitorFromWindow to find the monitor your window is on and then call GetMonitorInfo您可以使用 MonitorFromWindow 查找您的窗口所在的监视器,然后调用 GetMonitorInfo

If you want to go the low-level route or change the resolution you need to use EnumDisplayDevices, EnumDisplaySettings and ChangeDisplaySettings (This is the only way to get the refresh rate AFAIK, but GetDeviceCaps will tell you the color depth)如果您想走低级路线或更改分辨率,则需要使用 EnumDisplayDevices、EnumDisplaySettings 和 ChangeDisplaySettings(这是获得刷新率 AFAIK 的唯一方法,但 GetDeviceCaps 会告诉您颜色深度)

When system use DPI virtualization (Vista and above) using GetSystemMetrics or GetWindowRect will fail to get the real screen resolution (you will get the virtual resolution) unless you created DPI Aware Application .当系统使用DPI 虚拟化(Vista 及以上)时,使用 GetSystemMetrics 或 GetWindowRect 将无法获得真实的屏幕分辨率(您将获得虚拟分辨率),除非您创建了DPI Aware Application

So the best option here (simple and backward compatible) is to use EnumDisplaySettings with ENUM_CURRENT_SETTINGS.所以这里最好的选择(简单且向后兼容)是将EnumDisplaySettings与 ENUM_CURRENT_SETTINGS 一起使用。

It's GetSystemMetrics with these parameters:它是带有这些参数的GetSystemMetrics
SM_CXSCREEN < width SM_CXSCREEN < 宽度
SM_CYSCREEN < height SM_CYSCREEN < 高度

As it says (SM_CXSCREEN):正如它所说的(SM_CXSCREEN):

The width of the screen of the primary display monitor, in pixels.主显示器的屏幕宽度,以像素为单位。 This is the same value obtained by calling GetDeviceCaps as follows: GetDeviceCaps( hdcPrimaryMonitor, HORZRES).这与通过调用 GetDeviceCaps 获得的值相同,如下所示:GetDeviceCaps(hdcPrimaryMonitor, HORZRES)。

I think SystemParametersInfo might be useful.我认为SystemParametersInfo可能有用。

Edit: Look at GetMonitorInfo too.编辑:也看看GetMonitorInfo

MFC Example Multiple monitor support with GetSystemMetrics EnumDisplayMonitors and GetMonitorInfo MFC 示例 使用 GetSystemMetrics EnumDisplayMonitors 和 GetMonitorInfo 支持多监视器

Follow this link: Monitor enumeration with source code按照此链接: 使用源代码监视枚举

Deleted about a week ago, then edited 3-4-13.大约一周前删除,然后编辑 3-4-13。

Here's a good one for situations where the user has decided to run their desktop in a lower resolution (bad idea) or corner cases where a person decided to get a monitor that their graphics controller couldn't take full advantage of:对于用户决定以较低分辨率(坏主意)运行桌面的情况或人们决定获得其图形控制器无法充分利用的显示器的极端情况,这是一个很好的方法:

// get actual size of desktop
RECT actualDesktop;
GetWindowRect(GetDesktopWindow(), &actualDesktop);

I use the GetSystemMetrics function我使用 GetSystemMetrics 函数

GetSystemMetrics(SM_CXSCREEN) returns screen width(in pixels) GetSystemMetrics(SM_CXSCREEN) 返回屏幕宽度(以像素为单位)

GetSystemMetrics(SM_CYSCREEN) - height in pixels GetSystemMetrics(SM_CYSCREEN) - 以像素为单位的高度

https://msdn.microsoft.com/en-us/library/windows/desktop/ms724385%28v=vs.85%29.aspx https://msdn.microsoft.com/en-us/library/windows/desktop/ms724385%28v=vs.85%29.aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM