2011-12-31 147 views
23

可能重複:
How to get the Monitor Screen Resolution from an hWnd?如何在C++中獲得屏幕分辨率?

有沒有辦法讓在C++屏幕分辨率?
我已經搜索MSDN但沒有運氣。我發現的最接近的是ChangeDisplaySettingsEx(),但似乎沒有辦法在不改變它的情況下返回res。

+1

相關:http://stackoverflow.com/questions/2156212/how-to-get-the-monitor-screen-resolution-from-an-hwnd – Mat 2011-12-31 21:22:52

+0

好,一個這個問題的答案有效。 ('GetSystemMetrics()')標記爲重複 – 2011-12-31 21:28:07

回答

45
#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/

+2

這是否適用於多臺顯示器? – 2011-12-31 21:25:42

+0

下面是如何使用多個顯示器(在答案中):http://stackoverflow.com/questions/4631292/how-detect-current-screen-resolution – eboix 2011-12-31 21:28:36

+1

+1爲一個獨特的解決方案。我從來沒有想過這樣做 – 2011-12-31 21:30:14

0

在英巴卡迪諾C++ Builder,您可以得到它這樣

Screen->Height; 
Screen->Width; 

這是特異性針對與英巴卡迪諾產品提供VCL框架:C++ Builder中,德爾福。

+1

此答案適用於其他Embarcadero編譯器,如Delphi(即.width:= Screen.Width;)。 OP對於目標平臺或編譯器並不明確,所以這個答案沒有錯(這是Embarcadero特定的)。 – AlainD 2015-03-08 13:33:48