本文介绍了通过其索引获取(实际)监视器的句柄的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有3个监视器。如何仅通过第二个索引获取第二个的句柄? EnumDisplayMonitors()无法使用,因为它也枚举了伪设备,而EnumDisplayDevices()却不给我句柄。

Suppose I have 3 monitors. How do I get the handle of the second one only by its index? EnumDisplayMonitors() won't work because it enumerates the pseudo-devices as well and EnumDisplayDevices() doesn't give me the handle.

推荐答案

您需要使用 EnumDisplayMonitors()代替 EnumDisplayDevices()来访问每个监视器的 HMONITOR 句柄。

You need to use EnumDisplayMonitors() instead of EnumDisplayDevices() to access the HMONITOR handle of each monitor.

但是,监视器未通过索引标识。 GetMonitorInfo()可以告诉您哪个监视器是主要监视器,仅此而已。无法知道哪个监视器是第二,第三等。并且您也无法使用监视器位置来确定,因为第二监视器可以相对于主监视器放置在任何位置。监视器,然后可以将第三监视器放置在相对于第一或第二监视器的任何位置。

However, monitors are not identified by index. GetMonitorInfo() can tell you which monitor is "primary", but that is all. There is no way to know which monitor is "second", "third", etc. And you can't use monitor locations to determine that, either, as the "second" monitor could be positioned anywhere in relation to the "primary" monitor, and then the "third" monitor can be positioned anywhere in relation to either "first" or "second" monitor.

因此,您必须希望 EnumDisplayMonitors()枚举监视器的安装顺序,然后您可以执行以下操作:

So you have to hope that EnumDisplayMonitors() enumerates in the order that the monitors are installed, then you can do something like this:

struct sEnumInfo
{
    int iIndex;
    HMONITOR hMonitor;
};

BOOL CALLBACK GetMonitorByIndex(HMONITOR hMonitor, HDC hdcMonitor, LPRECT lprcMonitor, LPARAM dwData)
{
    sEnumInfo *info = (sEnumInfo*) dwData;
    if (--info->iIndex < 0)
    {
        info->hMonitor = hMonitor;
        return FALSE;
    }
    return TRUE;
}
sEnumInfo info;
info.iIndex = 1;
info.hMonitor = NULL;

EnumDisplayMonitors(NULL, NULL, GetMonitorByIndex, (LPARAM)&info);
if (info.hMonitor != NULL)
{
    //...
}

这篇关于通过其索引获取(实际)监视器的句柄的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 12:35