如何在C++中获取屏幕分辨率? [重复]

问题描述 投票:0回答:2

可能重复:
如何从 hWnd 获取显示器屏幕分辨率?

有没有办法在C++中获取屏幕分辨率?
我搜索过 MSDN 但没有运气。我发现的最接近的是

ChangeDisplaySettingsEx
() 但这似乎没有办法只返回 res 而不更改它。

c++ windows winapi screen-resolution
2个回答
72
投票
#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;
}

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


1
投票

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

Screen->Height;
Screen->Width;

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

© www.soinside.com 2019 - 2024. All rights reserved.