Skip to content

Instantly share code, notes, and snippets.

@the-nose-knows
Last active June 20, 2018 05:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save the-nose-knows/46f0115a8fba7975ce693db9353f9727 to your computer and use it in GitHub Desktop.
Save the-nose-knows/46f0115a8fba7975ce693db9353f9727 to your computer and use it in GitHub Desktop.

A Windows MonitorInfo class

MonitorInfo.h

#ifndef MONITOR_INFO_H
#define MONITOR_INFO_H

#include <Windows.h>
#include <vector>

struct MonitorRects
{
	std::vector<RECT> rcMonitors;

	static BOOL CALLBACK MonitorEnum(HMONITOR hMon, HDC hdc, LPRECT lprcMonitor, LPARAM pData)
	{
		MonitorRects* pThis = reinterpret_cast<MonitorRects*>(pData);
		pThis->rcMonitors.push_back(*lprcMonitor);
		return TRUE;
	}

	MonitorRects()
	{
		EnumDisplayMonitors(0, 0, MonitorEnum, (LPARAM)this);
	}
};
typedef MonitorRects MonitorRects;

struct MonitorData
{
	RECT monitor;
	bool isPrimaryDisplay;
	int height;
	int width;
	MonitorData() : 
		isPrimaryDisplay(false){};
};
typedef MonitorData MonitorData;

class MonitorInfo
{
private:	

	int m_smallest_X;
	int m_smallest_Y;
	int m_biggest_X;
	int m_biggest_Y;

public:
	MonitorInfo();
	~MonitorInfo();

	std::vector<MonitorData> monitors;

	RECT virtual_desktop;

	int virtual_desktop_height;
	int virtual_desktop_width;
};

#endif // MONITOR_INFO_H

MonitorInfo.cpp

#include "MonitorInfo.h"

MonitorInfo::MonitorInfo()
{
	MonitorRects monitors;

	this->m_biggest_X = -INT_MAX;
	this->m_biggest_Y = -INT_MAX;
	this->m_smallest_X = INT_MAX;
	this->m_smallest_Y = INT_MAX;

	for (auto i : monitors.rcMonitors)
	{
		MonitorData monitorData;
		monitorData.monitor = i;
		
		if (i.top == 0 && i.left == 0)
			monitorData.isPrimaryDisplay = true;

		monitorData.height = monitorData.monitor.bottom - monitorData.monitor.top;
		monitorData.width = monitorData.monitor.right - monitorData.monitor.left;

		this->m_smallest_Y = (monitorData.monitor.top < this->m_smallest_Y) ? (monitorData.monitor.top) : (this->m_smallest_Y);
		this->m_smallest_X = (monitorData.monitor.left < this->m_smallest_X) ? (monitorData.monitor.left) : (this->m_smallest_X);

		this->m_biggest_Y = (monitorData.monitor.bottom > this->m_biggest_Y) ? (monitorData.monitor.bottom) : (this->m_biggest_Y);
		this->m_biggest_X = (monitorData.monitor.right > this->m_biggest_X) ? (monitorData.monitor.right) : (this->m_biggest_X);

		this->monitors.push_back(monitorData);
	}

	this->virtual_desktop_height = std::abs(this->m_biggest_Y) + std::abs(this->m_smallest_Y);
	this->virtual_desktop_width = std::abs(this->m_biggest_X) + std::abs(this->m_smallest_X);

	this->virtual_desktop.top = this->m_smallest_Y;
	this->virtual_desktop.left = this->m_smallest_X;
	this->virtual_desktop.bottom = this->m_biggest_Y;	
	this->virtual_desktop.right = this->m_biggest_X;
}

MonitorInfo::~MonitorInfo(){}

Entry-Point

#include "MonitorInfo.h"
#include <iostream>

int main()
{
	MonitorInfo monitors;

	std::cout << "\tVirtual Desktop Info" << std::endl;
	std::cout << "Virtual Desktop Height:\t\t" << monitors.virtual_desktop_height << std::endl;
	std::cout << "Virtual Desktop Width:\t\t" << monitors.virtual_desktop_width << std::endl;

	for (auto i : monitors.monitors)
	{
		std::cout << std::endl;

		if (i.isPrimaryDisplay)
		{
			std::cout << "\tPrimary Display Info" << std::endl;			
		}
		else
		{
			std::cout << "\tA Secondary Display's Info" << std::endl;
		}
		std::cout << "Height:\t\t\t\t" << i.height << std::endl;
		std::cout << "Width:\t\t\t\t" << i.width << std::endl;
		std::cout << "Top Y Coordinate:\t\t" << i.monitor.top << std::endl;
		std::cout << "Bottom Y Coordinate:\t\t" << i.monitor.bottom << std::endl;
		std::cout << "Left X Coordinate:\t\t" << i.monitor.left << std::endl;
		std::cout << "Right X Coordinate:\t\t" << i.monitor.right << std::endl;
	}
	return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment