从其句柄 (HMONITOR) 获取监视器索引
Get monitor index from its handle (HMONITOR)
我有兴趣在给定显示器句柄的情况下获取监视器索引(从 1 开始,以匹配 Windows 编号)。
使用案例:给定一个窗口的矩形,我想知道它所属的显示器。我可以使用MonitorFromRect
获取显示器的手柄:
// RECT rect
const HMONITOR hMonitor = MonitorFromRect(rect, MONITOR_DEFAULTTONEAREST);
如何从此句柄获取监视器索引?
PS:不确定是否重复,但我一直在四处寻找,但没有运气。
我发现这篇文章有相反的问题:找到给定索引的句柄(在这种情况下从 0 开始)。
基于它,我使用了这个解决方案:
struct sEnumInfo {
int iIndex = 0;
HMONITOR hMonitor = NULL;
};
BOOL CALLBACK GetMonitorByHandle(HMONITOR hMonitor, HDC hdcMonitor, LPRECT lprcMonitor, LPARAM dwData)
{
auto info = (sEnumInfo*)dwData;
if (info->hMonitor == hMonitor) return FALSE;
++info->iIndex;
return TRUE;
}
int GetMonitorIndex(HMONITOR hMonitor)
{
sEnumInfo info;
info.hMonitor = hMonitor;
if (EnumDisplayMonitors(NULL, NULL, GetMonitorByHandle, (LPARAM)&info)) return -1;
return info.iIndex + 1; // 1-based index
}
相关文章:
- C++为构建时间获取QDateTime的可靠方法
- lambda参数转换为constexpr技巧,然后获取带链接的数组
- 如何使用 < 和 > 命令获取 c++ 中的输入和输出?
- 使用指针从C++中的数组中获取最大值
- 如何获取std::result_of函数的返回类型
- 如何在openssl-ecc中获取十六进制格式的私钥
- 使用Unreal C++获取VR耳机的世界位置/方向
- 获取日期异步信号安全吗?如果在信号处理程序中使用,它会导致死锁吗
- 从C字符串中获取奇怪的字符串长度
- 为什么我的for循环不能正确获取argv
- 从python中调用C++函数并获取返回值
- 如何获取一个数字的前3位
- 获取字符串的长度并将其分配给数组
- 获取窗口监视器的唯一标识符
- 获取特定监视器的处理程序以在其中放置弹出窗口
- 从其句柄 (HMONITOR) 获取监视器索引
- 通过(实际)监视器的索引获取其句柄
- 使用 EnumDisplayDevices 获取监视器的名称
- 作为windows服务运行时,如何获取活动监视器的数量?C++
- GetDC(NULL)获取主监视器或虚拟屏幕