C++ | 获取第二个屏幕坐标信息

276 阅读1分钟

「这是我参与11月更文挑战的第6天,活动详情查看:2021最后一次更文挑战」。

1. 定义全局变量: CRect rect[2] = {(0,0,0,0),(0,0,0,0)}; // 分别存放两个屏幕的坐标

2. 定义全局函数:

 BOOL CALLBACK Monitorenumproc( HMONITOR hMonitor,HDC hdcMonitor, LPRECT lprcMonitor, LPARAM dwData)
 {        

  static BOOL first = FALSE; //重复检测标志
  
  MONITORINFO monitorinfo; //显示器信息结构体
  monitorinfo.cbSize = sizeof(MONITORINFO);
  GetMonitorInfo(hMonitor, &monitorinfo); //获取显示器信息

  if(monitorinfo.dwFlags == MONITORINFOF_PRIMARY) //如果是主显示器
  {
    if(!first) //如果是第一次检测到主显示器
    {
        first = TRUE;
        rect[0] = monitorinfo.rcMonitor; //将主显示器的坐标信息存到第一个位置
        return TRUE;
    }else
    {
        first = FALSE;
        return FALSE;
    }
    }else//如果不是主显示器,将显示器的坐标信息存到第二个位置
    {
        rect[1] = monitorinfo.rcMonitor;
    }
    return TRUE;
}

3. 在主函数调用: EnumDisplayMonitors(NULL, NULL, Monitorenumproc, 0);  // 枚举屏幕的系统函数