如何在 C++ 中获得屏幕分辨率?

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

How to get screen resolution in C++?

c++windowswinapiscreen-resolution

提问by Nate Koppenhaver

Possible Duplicate:
How to get the Monitor Screen Resolution from an hWnd?

可能的重复:
如何从 hWnd 获取监视器屏幕分辨率?

Is there a way to get the screen resolution in C++?
I have searched MSDN but with no luck. The closest thing I found was ChangeDisplaySettingsEx()but that doesn't seem to have a way to just return the res without changing it.

有没有办法在 C++ 中获得屏幕分辨率?
我已经搜索了 MSDN,但没有运气。我发现的最接近的是ChangeDisplaySettingsEx()但这似乎没有办法只返回 res 而不更改它。

回答by eboix

#include "wtypes.h"
#include <iostream>
using namespace std;

// Get the horizontal and vertical screen sizes in pixel
void GetDesktopResolution(int& horizontal, int& vertical)
{
   RECT desktop;
   // Get a handle to the desktop window
   const HWND hDesktop = GetDesktopWindow();
   // Get the size of screen to the variable desktop
   GetWindowRect(hDesktop, &desktop);
   // The top left corner will have coordinates (0,0)
   // and the bottom right corner will have coordinates
   // (horizontal, vertical)
   horizontal = desktop.right;
   vertical = desktop.bottom;
}

int main()
{       
   int horizontal = 0;
   int vertical = 0;
   GetDesktopResolution(horizontal, vertical);
   cout << horizontal << '\n' << vertical << '\n';
   return 0;
}

Source: http://cppkid.wordpress.com/2009/01/07/how-to-get-the-screen-resolution-in-pixels/

来源:http: //cppkid.wordpress.com/2009/01/07/how-to-get-the-screen-resolution-in-pixels/

回答by Shaun07776

In Embarcadero C++ builder you can get it like this

在 Embarcadero C++ builder 中,你可以像这样得到它

Screen->Height;
Screen->Width;

This is specific for VCL framework which is supplied with Embarcadero products: C++ Builder, Delphi.

这是特定于随 Embarcadero 产品提供的 VCL 框架:C++ Builder、Delphi。