如何检测当前屏幕分辨率?
如何从 Winapi(C 或 C++ 语言)检测当前屏幕分辨率?
一些背景:
我想启动一个新的 OpenGL 全屏窗口,但希望它以桌面已设置的相同水平和垂直尺寸打开。 (现在,当每个人都使用 LCD 屏幕时,我认为这是获得屏幕原始分辨率的最佳方法。)
我并不迫切需要知道桌面颜色深度,尽管这将是一个不错的奖励。
How do I from Winapi (in C or C++) detect the current screen resolution?
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.)
I don't desperately need to also know the desktop color depth, although that would be a nice bonus.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
GetSystemMetrics< /code>
SM_CXSCREEN
/SM_CYSCREEN
(也可以使用GetDeviceCaps
)GetSystemMetrics
SM_CX/YVIRTUALSCREENSystemParametersInfo
SPI_GETWORKAREA
GetMonitorInfo
编辑:
重要的是要记住,监视器并不总是从 0x0“开始”,因此仅知道大小不足以定位窗口。您可以使用
MonitorFromWindow
查找窗口所在的监视器,然后调用GetMonitorInfo
如果您想走低级路线或更改需要使用的分辨率
EnumDisplayDevices
、EnumDisplaySettings
和ChangeDisplaySettings
(这是获取刷新率 AFAIK 的唯一方法,但是GetDeviceCaps
会告诉您颜色深度)GetSystemMetrics
SM_CXSCREEN
/SM_CYSCREEN
(GetDeviceCaps
can also be used)GetSystemMetrics
SM_CX/YVIRTUALSCREENSystemParametersInfo
SPI_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 callGetMonitorInfo
If you want to go the low-level route or change the resolution you need to use
EnumDisplayDevices
,EnumDisplaySettings
andChangeDisplaySettings
(This is the only way to get the refresh rate AFAIK, butGetDeviceCaps
will tell you the color depth)当系统使用DPI虚拟化(Vista 及更高版本)使用 GetSystemMetrics 或 GetWindowRect 将无法获取真实屏幕分辨率(您将获得虚拟分辨率),除非您创建了 DPI 感知应用程序。
因此,这里最好的选择(简单且向后兼容)是将 EnumDisplaySettings 与 ENUM_CURRENT_SETTINGS 结合使用。
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.
So the best option here (simple and backward compatible) is to use EnumDisplaySettings with ENUM_CURRENT_SETTINGS.
它是带有以下参数的 GetSystemMetrics:
SM_CXSCREEN <宽度
SM_CYSCREEN <高度
正如它所说(SM_CXSCREEN):
It's GetSystemMetrics with these parameters:
SM_CXSCREEN < width
SM_CYSCREEN < height
As it says (SM_CXSCREEN):
我认为 SystemParametersInfo 可能有用。
编辑:也请查看 GetMonitorInfo 。
I think SystemParametersInfo might be useful.
Edit: Look at GetMonitorInfo too.
MFC 示例 GetSystemMetrics EnumDisplayMonitors 和 GetMonitorInfo 的多监视器支持
请点击此链接: 用源代码监控枚举
MFC Example Multiple monitor support with GetSystemMetrics EnumDisplayMonitors and GetMonitorInfo
Follow this link: Monitor enumeration with source code
我使用 GetSystemMetrics 函数
GetSystemMetrics(SM_CXSCREEN) 返回屏幕宽度(以像素为单位)
GetSystemMetrics(SM_CYSCREEN) - 以像素为单位的高度
https://msdn.microsoft.com/en-us/library/windows/desktop/ms724385%28v=vs.85%29.aspx
I use the GetSystemMetrics function
GetSystemMetrics(SM_CXSCREEN) returns screen width(in pixels)
GetSystemMetrics(SM_CYSCREEN) - height in pixels
https://msdn.microsoft.com/en-us/library/windows/desktop/ms724385%28v=vs.85%29.aspx
获得真实的显示器分辨率
在任何情况下,它都会返回原始分辨率,即使操作系统由于进程的 DPI 意识而试图对您撒谎。
获取虚拟分辨率和实际分辨率之间的缩放比例
这将为您提供给定显示器的实际分辨率相对于虚拟分辨率的比率。
如果主显示器的主 DPI 为 225%,而第二个显示器的主 DPI 为 100%,并且您为第二个显示器运行此函数,您将得到 2.25。因为
2.25 * 真实分辨率
= 显示器的虚拟分辨率
。如果第二个显示器的缩放比例为 125%(而主显示器的缩放比例仍为 225%),则此函数将返回
1.79999995
,因为该值是相对于 225% 的 125% (225/125 = 1.8 ),然后再次 - 1.8 * 真实分辨率=
125% 的虚拟分辨率`获取真实 DPI 值(与任何值无关)
给定显示器,A 的 DPI 为 225%,显示器 B 的 DPI 为125% DPI,正如我上面所说,第二台显示器不会获得 1.25(如果您在第二台显示器上运行该函数。正如我所说,您将获得 1.8)。
要克服这个问题,请使用此函数:
此函数取决于我上面编写的前一个函数(您需要复制的函数
GetMonitorScalingRatio
)这将为您提供正确的值
To get real monitor resolution
It will return that native resolution in any case, even if the OS tries to lie to you due to the DPI awareness of the process.
To get the scaling ratio between the virtual resolution and real resolution
This will give you a ratio of the real resolution relative to the virtual resolution of the given monitor.
If the main DPI of the main monitor is 225% and on the second monitor it is 100%, and you run this function for the second monitor, you will get 2.25. because
2.25 * real resolution
=the virtual resolution
of the monitor.If the second monitor has 125% scaling (while the main monitor is still 225% scaling), then this function will return you
1.79999995
because 125% relative to 225% is this value (225/125 = 1.8), and again - 1.8 * real resolution=
the virtual resolution of 125%`To get the real DPI value (not relative to anything)
Given that monitor, A has 225% DPI, and monitor B has 125% DPI, as I said above, you will not get 1.25 for the second monitor (if you run the function on the second monitor. You will get 1.8 as I said).
To overcome this, use this function:
This function depends on the previous function that I wrote above (the function
GetMonitorScalingRatio
that you need to copy)This will give you the correct value
大约一周前删除,然后于 3-4-13 进行编辑。
对于用户决定这样做的情况,这是一个很好的选择
以较低的分辨率(坏主意)或角落运行桌面
某人决定购买一台显示器的情况
图形控制器无法充分利用:
Deleted about a week ago, then edited 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: