2011-12-31 19 views
23

Powiel możliwe:
How to get the Monitor Screen Resolution from an hWnd?Jak uzyskać rozdzielczość ekranu w C++?

Czy istnieje sposób, aby uzyskać rozdzielczość ekranu w C++?
Szukałem MSDN, ale bez powodzenia. Najbliższą rzeczą, jaką znalazłem, było ChangeDisplaySettingsEx(), ale wydaje się, że nie ma sposobu, aby po prostu zwrócić res bez zmiany.

+1

pokrewne: http://stackoverflow.com/questions/2156212/how-to-get-the-monitor-screen-resolution-from-an-hwnd – Mat

+0

Ok, jeden odpowiedzi na to pytanie działało. ('GetSystemMetrics()') Oznaczenie jako duplikat –

Odpowiedz

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; 
} 

Źródło: http://cppkid.wordpress.com/2009/01/07/how-to-get-the-screen-resolution-in-pixels/

+2

Czy działa to z wieloma monitorami? –

+0

Oto, jak to zrobić z wieloma monitorami (w odpowiedziach): http://stackoverflow.com/questions/4631292/how-detect- current-screen-resolution – eboix

+1

+1 dla unikatowego rozwiązania. Nigdy nie myślałem o zrobieniu tego w ten sposób –

0

W Embarcadero C++ Builder można dostać to tak

Screen->Height; 
Screen->Width; 

ten jest specyficzny dla VCL ram, które jest dostarczane z produktami Embarcadero: C++ Builder, Delphi.

+1

Ta odpowiedź dotyczy innych kompilatorów Embarcadero, takich jak Delphi (tj. Width: = Screen.Width;). OP nie było jednoznaczne co do docelowej platformy lub kompilatora, więc ta odpowiedź nie jest błędna (że została określona dla Embarcadero). – AlainD

Powiązane problemy