First, set the monitor to copy mode.
Set copy mode: you can manually modify Windows settings and set them to copy mode. You can also use setdisplayconfig (0, null, 0, null, SDC)_ APPLY | SDC_ TOPOLOGY_ Clone); / / set to copy mode
class MyInfo
{
public:
MyInfo();
~MyInfo();
public:
int m_nFlag; //是否是主屏 0 - 否 其他是
HMONITOR m_HMONITOR;
};
MyInfo::MyInfo()
{
m_nFlag = 0; //是否是主屏 0 - 否 其他是
m_HMONITOR = 0;
}
MyInfo::~MyInfo()
{
}
list<MyInfo> m_listInfo;
int numScreen = 0;
BOOL CALLBACK MonitorEnumProc(HMONITOR hMonitor,HDC hdcMonitor, LPRECT lprcMonitor, LPARAM dwData)
{
static bool first = true; / / flag
MyInfo myInfo;
//Save display information
MONITORINFO monitorinfo;
monitorinfo.cbSize = sizeof(MONITORINFO);
//Get the display information and save it in monitorinfo
GetMonitorInfo(hMonitor, &monitorinfo);
myInfo.m_ nFlag = monitorinfo.dwFlags;
myInfo.m_ HMONITOR = hMonitor;
m_listInfo.push_ back(myInfo);
if (monitorinfo.dwFlags == MONITORINFOF_ PRIMARY)
{
if(first) // the home screen is detected for the first time
{
first = FALSE;
numScreen = 1;
return TRUE;
}
else //if the main screen is detected for the second time, it means that all monitors have been detected, so the detection can be stopped
{
First = true; // flag reset
Return false; // end detection
}
}
numScreen++;
return TRUE;
}
//get monitor handle
void Test()
{
EnumDisplayMonitors(NULL, NULL, MonitorEnumProc, 0);
CString strNum = "";
strNum.Format("current monitor num=%d", numScreen);
::AfxMessageBox(strNum); // num = 1
BOOL bFlag;
INT iNumber = 0;
for each (auto v in m_listInfo)
{
if (v.m_nflag == 1) // if it is the home screen - it can only be the home screen, because only one display can be enumerated in copy mode
{
HMONITOR hMonitor = NULL;
DWORD cPhysicalMonitors = 1;
CWnd *hwnd = GetDesktopWindow();
hMonitor = v.m_ HMONITOR;
//hMonitor = MonitorFromWindow(*hwnd,MONITOR_ DEFAULTTOPRIMARY);
BOOL bSuccess;
bSuccess = GetNumberOfPhysicalMonitorsFromHMONITOR(hMonitor,&cPhysicalMonitors); // cphysicalmonitors will automatically change to 2 - because I currently have two monitors
LPPHYSICAL_ MONITOR pPhysicalMonitors = (LPPHYSICAL_ MONITOR)malloc(
cPhysicalMonitors * sizeof(PHYSICAL_ MONITOR));
if (pPhysicalMonitors != NULL)
{
bSuccess = GetPhysicalMonitorsFromHMONITOR(
v.m_ HMONITOR, cPhysicalMonitors, pPhysicalMonitors);
//Display 1 handle pphysicalmonitors [0]. Hphysicalmonitor
//Display 2 handle pphysicalmonitors [1]. Hphysicalmonitor
//This is the place to deal with your own logic
free(pPhysicalMonitors); //free
}
}
}
//Simple explanation:
Set the monitor to copy mode.
Enumdisplaymonitors is used to enumerate displays, and the number of displays enumerated in copy mode is 1。
call getnumberofphysicalmonitorsfromhmonitor。
To lpphysical_ Monitor allocates memory。
Use the function getphysicalmonitorsfromhmonitor to get the monitor handle。
The monitor handle is in the index of the pphysicalmonitors structure array。
How to get the handle and information (display name, etc.) of two conventional displays in copy mode
Afon.zhang
396
Reputation points
How to get the handle and information (display name, etc.) of two conventional displays in copy mode。
There are two conventional displays。
Set to copy mode。
How to get the handle of two monitors。
Accepted answer
-
Afon.zhang 396 Reputation points
2021-01-15T02:12:19.553+00:00