37

Possible Duplicate:
How to get the Monitor Screen Resolution from an 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.

Community
  • 1
  • 1
Nate Koppenhaver
  • 1,676
  • 3
  • 21
  • 31

2 Answers2

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

eboix
  • 5,113
  • 1
  • 27
  • 38
  • 2
    Does this work with multiple monitors? – Cody Gray - on strike Dec 31 '11 at 21:25
  • Here's how to do it with multiple monitors (in the answers): http://stackoverflow.com/questions/4631292/how-detect-current-screen-resolution – eboix Dec 31 '11 at 21:28
  • 3
    This will give you size of desktop **without Taskbar** – Quest Aug 22 '14 at 08:37
  • @Quest good point. I just realised that I actually do need a size without task bar :) – Tomáš Zato Nov 22 '14 at 06:13
  • 3
    for 4K screen with 3840 x 2160 resolution in Windows 10, this method returns 1536 x 864. can anybody explain that? – Sergii Jun 06 '17 at 12:14
  • @user2199593 It's probably due to the method, `GetDesktopWindow`; you might have a display of `1536 x 864` as your main display, even if you have a 4k display connected. – kayleeFrye_onDeck May 22 '18 at 18:36
  • @user2199593 I'm seeing the same - it appears to be due to DPI. See this thread on MSDN social: https://social.msdn.microsoft.com/Forums/vstudio/en-US/e38ee167-a246-4e5f-a858-52dcb17e0e4b/getmonitorinfo-tells-app-my-4k-monitor-is-2560-x-1440?forum=vcgeneral – Den-Jason Sep 17 '20 at 23:27
  • 1
    ... so adding `SetProcessDPIAware();` to be the very first thing done in winMain appears to fix it. – Den-Jason Sep 17 '20 at 23:29
  • Also see some really useful stuff on EnumDisplayMonitors here: https://stackoverflow.com/questions/26541484/enumdisplaymonitors-callback – Den-Jason Sep 17 '20 at 23:30
1

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

Screen->Height;
Screen->Width;

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

Jurlie
  • 1,014
  • 10
  • 27
Shaun07776
  • 1,052
  • 1
  • 10
  • 16
  • 3
    This answer applies to other Embarcadero compilers, like Delphi (ie. width := Screen.Width;). The OP wasn't explicit as to the targeted platform or compiler so this answer isn't wrong (that it was specific to Embarcadero was specified). – AlainD Mar 08 '15 at 13:33
  • `#include Display* disp = XOpenDisplay(NULL); Screen* scrn = DefaultScreenOfDisplay(disp); int height = scrn->height; int width = scrn->width;` with the linker option `-lX11`. – Suuuehgi Apr 10 '19 at 19:38