Skip to content

Instantly share code, notes, and snippets.

@SuperKogito
Last active June 20, 2022 11:42
Show Gist options
  • Save SuperKogito/c31c0c0f9e69e484a1740b67a207a5c1 to your computer and use it in GitHub Desktop.
Save SuperKogito/c31c0c0f9e69e484a1740b67a207a5c1 to your computer and use it in GitHub Desktop.
A small snippet displaying how to get the rectangles structures (coordinates) associated with all the connected screens/monitors.
/*********************************************************************
* \file CaptureMonitorsRects.cpp
* \brief capture monitors rectangles handles in a custom structure.
*
* \author SuperKogito
* \date July 2020
*
* @note:
* references and sources:
*
*
*********************************************************************/
#include <windows.h>
#include <vector>
#include <iostream>
/**
* Structure that includes all screen hanldes and rectangles.
*/
struct cMonitorsVec
{
std::vector<int> iMonitors;
std::vector<HMONITOR> hMonitors;
std::vector<HDC> hdcMonitors;
std::vector<RECT> rcMonitors;
static BOOL CALLBACK MonitorEnum(HMONITOR hMon, HDC hdc, LPRECT lprcMonitor, LPARAM pData)
{
cMonitorsVec* pThis = reinterpret_cast<cMonitorsVec*>(pData);
pThis->hMonitors.push_back(hMon);
pThis->hdcMonitors.push_back(hdc);
pThis->rcMonitors.push_back(*lprcMonitor);
pThis->iMonitors.push_back(pThis->hdcMonitors.size());
return TRUE;
}
cMonitorsVec()
{
EnumDisplayMonitors(0, 0, MonitorEnum, (LPARAM)this);
}
};
int main()
{
cMonitorsVec Monitors;
for (int monitorIndex=0; monitorIndex < Monitors.iMonitors.size(); monitorIndex++)
{
std::wcout << "Screen id: " << monitorIndex << std::endl;
std::wcout << "-----------------------------------------------------" << std::endl;
std::wcout << " - screen left-top corner coordinates : (" << Monitors.rcMonitors[monitorIndex].left
<< "," << Monitors.rcMonitors[monitorIndex].top
<< ")" << std::endl;
std::wcout << " - screen dimensions (width x height) : (" << std::abs(Monitors.rcMonitors[monitorIndex].right - Monitors.rcMonitors[monitorIndex].left)
<< "," << std::abs(Monitors.rcMonitors[monitorIndex].top - Monitors.rcMonitors[monitorIndex].bottom)
<< ")" << std::endl;
std::wcout << "-----------------------------------------------------" << std::endl;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment