windows C++ 找出显卡支持的分辨率

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4089073/
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-09-15 15:32:23  来源:igfitidea点击:

C++ Finding out what resolutions are supported by the graphics card

c++windowswinapiqt4resolution

提问by The Dude

I am writing a small program to let me switch my resolution back and forth because my projector cannot handle the same resolution as my screen. I already know how to set the screen resolution using the windows API. As well as read the current resolution using the windows API or the QT4 toolkit. My problem is I want a menu of all of the different resolutions supported by the screen and graphics card. This program will be distributed so I need the program to actually communicate to the graphics card to find out what it supports. The only API I want to use is the windows API, or the QT4 toolkit, but I don't think QT4 does that unless you are using the graphics widgets in odd ways.

我正在编写一个小程序,让我来回切换分辨率,因为我的投影仪无法处理与屏幕相同的分辨率。我已经知道如何使用 windows API 设置屏幕分辨率。以及使用 windows API 或 QT4 工具包读取当前分辨率。我的问题是我想要一个包含屏幕和显卡支持的所有不同分辨率的菜单。该程序将被分发,因此我需要该程序与显卡进行实际通信以找出它支持的内容。我想使用的唯一 API 是 windows API 或 QT4 工具包,但我认为 QT4 不会这样做,除非您以奇怪的方式使用图形小部件。

I am pretty sure this is possible with the WINDOWS API. I just don't know how to do it.

我很确定这可以通过 WINDOWS API 实现。我只是不知道该怎么做。

Oh and please cut me some slack, I am familiar with QT4 and C++ but I am typically a Linux programmer, I am writing this for someone else. The only thing I have ever done with the windows API is make a message box, set the background, and used system variables. So please explain the process simply. Please don't just post a link to the msdn, I hate their documentation, and I hate Microsoft. I use windows maybe twice a year.

哦,请让我放松一下,我熟悉 QT4 和 C++,但我通常是一名 Linux 程序员,我是为其他人写这篇文章的。我使用 Windows API 所做的唯一一件事就是创建一个消息框、设置背景和使用系统变量。所以请简单解释一下这个过程。请不要只发布指向 msdn 的链接,我讨厌他们的文档,而且我讨厌 Microsoft。我可能一年使用两次窗户。

回答by MerickOWA

The following should probably work for you in the general case

在一般情况下,以下内容可能对您有用

DEVMODE dm = { 0 };
dm.dmSize = sizeof(dm);
for( int iModeNum = 0; EnumDisplaySettings( NULL, iModeNum, &dm ) != 0; iModeNum++ ) {
  cout << "Mode #" << iModeNum << " = " << dm.dmPelsWidth << "x" << dm.dmPelsHeight << endl;
  }

This should print out all the supported resolutions on the current display that the .exe is running on. Assuming you're not dealing with a multi-display graphics card this should work. Otherwise you'd have to use EnumDisplayDevices loop over each display.

这应该会在 .exe 正在运行的当前显示器上打印出所有支持的分辨率。假设您没有处理多显示图形卡,这应该可行。否则,您必须在每个显示器上使用 EnumDisplayDevices 循环。

Once you figure out what resolution you want you can use 'ChangeDisplaySettingsEx' to change the display to the mode you want.

一旦确定了您想要的分辨率,您就可以使用“ChangeDisplaySettingsEx”将显示更改为您想要的模式。

Using DirectX is possible but I wouldn't recommend it as the code is alot more complicated (having to initialize DirectX and using COM pointers) unless you plan to actually use DirectX for more than just determining display resolutions.

使用 DirectX 是可能的,但我不推荐它,因为代码要复杂得多(必须初始化 DirectX 并使用 COM 指针),除非您打算实际使用 DirectX,而不仅仅是确定显示分辨率。

回答by YWE

EnumDisplaySettings:)

枚举显示设置:)

From MSDN:

来自 MSDN:

"To obtain the current display settings, pass the ENUM_CURRENT_SETTINGS constant in the iModeNum parameter to the EnumDisplaySettings API, as illustrated by the following C++ code."

“要获取当前的显示设置,请将 iModeNum 参数中的 ENUM_CURRENT_SETTINGS 常量传递给 EnumDisplaySettings API,如下面的 C++ 代码所示。”

DEVMODE dm;
// initialize the DEVMODE structure
ZeroMemory(&dm, sizeof(dm));
dm.dmSize = sizeof(dm);
if (0 != EnumDisplaySettings(NULL, ENUM_CURRENT_SETTINGS, &dm))
{
// inspect the DEVMODE structure to obtain details
// about the display settings such as
//  - Orientation
//  - Width and Height
//  - Frequency
//  - etc.
}