The function GetWindowRect(), defined in WinUser.h, is able to give the current screen resolution. The declaration of GetWindowRect() is
BOOL WINAPI GetWindowRect( __in HWND hWnd, __out LPRECT lpRect);
We can give a handle to the current desktop window to the above function as input. A sample implementation for getting the current screen resolution is given below.
#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;
}
February 15, 2009 at 6:50 am
Thank you!! This piece of code was very helpful.
Paulo Silveira – Brazil
March 10, 2009 at 8:03 am
Thanks a lot! that’s exactly what i was looking for
June 6, 2009 at 11:35 am
Hi, this is indeed helpful.
But is there a way to exclude the taskbar in this script?
August 12, 2009 at 2:47 pm
Thanks! Been googling for about an hour trying to find how to do that!