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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4631292/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 15:59:59  来源:igfitidea点击:

How detect current screen resolution?

c++cwinapiapiresolution

提问by Prof. Falken contract breached

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. (Now when everyone uses LCD screens, I figured this is the best way to get the native resolution of the screen.)

我想启动一个新的 OpenGL 全屏窗口,但希望它以桌面已设置的相同水平和垂直大小打开。(现在每个人都使用 LCD 屏幕时,我认为这是获得屏幕原始分辨率的最佳方法。)

I don't desperately need to also know the desktop color depth, although that would be a nice bonus.

我并不迫切需要知道桌面颜色深度,尽管这将是一个不错的奖励。

回答by Anders

  • Size of the primary monitor: GetSystemMetricsSM_CXSCREEN / SM_CYSCREEN (GetDeviceCaps can also be used)
  • Size of all monitors (combined): GetSystemMetrics SM_CX/YVIRTUALSCREEN
  • Size of work area (screen excluding taskbar and other docked bars) on primary monitor: SystemParametersInfoSPI_GETWORKAREA
  • Size of a specific monitor (work area and "screen"): GetMonitorInfo
  • 主监视器的大小:GetSystemMetricsSM_CXSCREEN / SM_CYSCREEN(也可以使用 GetDeviceCaps)
  • 所有监视器的大小(组合):GetSystemMetrics SM_CX/YVIRTUALSCREEN
  • 主监视器上的工作区大小(屏幕不包括任务栏和其他停靠栏):SystemParametersInfoSPI_GETWORKAREA
  • 特定监视器的大小(工作区和“屏幕”):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. You can use MonitorFromWindow to find the monitor your window is on and then call GetMonitorInfo

编辑:重要的是要记住,监视器并不总是从 0x0“开始”,因此仅知道大小不足以定位您的窗口。您可以使用 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 会告诉您颜色深度)

回答by fingeros

When system use DPI virtualization(Vista and above) using GetSystemMetrics or GetWindowRect will failto 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 EnumDisplaySettingswith ENUM_CURRENT_SETTINGS.

所以这里的最佳选择(简单且向后兼容)是将EnumDisplaySettings与 ENUM_CURRENT_SETTINGS一起使用。

回答by Poni

It's GetSystemMetricswith these parameters:
SM_CXSCREEN < width
SM_CYSCREEN < height

它是带有这些参数的GetSystemMetrics
SM_CXSCREEN < 宽度
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)。

回答by user541686

I think SystemParametersInfomight be useful.

我认为SystemParametersInfo可能有用。

Edit: Look at GetMonitorInfotoo.

编辑:也看看GetMonitorInfo

回答by Ulterior

MFC Example Multiple monitor support with GetSystemMetrics EnumDisplayMonitors and GetMonitorInfo

MFC 示例 使用 GetSystemMetrics EnumDisplayMonitors 和 GetMonitorInfo 支持多监视器

Follow this link: Monitor enumeration with source code

按照此链接:使用源代码监视枚举

回答by formerBGIuser

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);

回答by Petr

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