将显示器显示屏上的大数字与MONITORINFOEX中的szDevice匹配

时间:2016-04-06 07:25:26

标签: c++ winapi

我正在实施一个应用程序,屏幕由用户捕获选定的监视器。 UI的设计与" Monitor Display"。

完全相同

enter image description here

我一直在寻找如何获得大数字,我发现这是不正式支持但我在测试 EnumDisplayMonitors 时发现了一些可能性。

Enumertaion回调提供 szDevice ,它位于 MONITORINFOEX 中,并与" Monitor Display"匹配。 '第ie) szDevice .//./ DISPLAY1 匹配大数字" 1"

我测试了几台机器并且总是一致的结果。我可以假设 szDevice 的这个数字末尾总是与" Monitor Display"相同。 ' S?任何帮助都会得到满足。感谢。

1 个答案:

答案 0 :(得分:2)

您似乎正在尝试复制用于在控制面板屏幕分辨率小程序中排列显示器的显示器,该小程序使用大量数字来识别显示器。这似乎只是枚举这些监视器的顺序,因为附加到MONITORINFOEX返回的GetMonitorInfo()结构的szDevice字段的数字使用不同的数字。

在附有2台显示器的笔记本电脑上,我们获得了如下图所示的UI显示。 three monitor arrangement

请注意,主监视器位于中间且编号为3.此监视器的左上角为0,0。

下面的程序显示EnumDisplayMonitors以相同的数字顺序(1,2,3)返回这些监视器:

C:\Code\Demos>lsmonitor.exe
1 00010003 -1920x0+0+1080\\.\DISPLAY1
2 00010005 1920x0+3840+1080\\.\DISPLAY2
3 00010001 0x0+1920+1080\\.\DISPLAY4 (primary)

测试程序(C中)。我们必须自己跟踪显示索引(在本例中是主函数中的counter变量:

/* Print out information about the connected monitors
 *
 * To compile:
 *  (MSVC++):  cl -nologo -W3 -MDd -Zi -Od lsmonitor.c
 */

#define UNICODE
#define _UNICODE
#define WIN32
#define STRICT
#define WIN32_LEAN_AND_MEAN

#include <windows.h>
#include <tchar.h>
#include <stdio.h>

#pragma comment(lib, "user32")

static void PrintError(LPCTSTR szPrefix, DWORD dwError);

static BOOL CALLBACK
MonitorEnumProc(HMONITOR hMonitor, HDC hdcMonitor, LPRECT prcMonitor, LPARAM pData)
{
    int *pCounter = (int *)pData;
    MONITORINFOEX mi;
    mi.cbSize = sizeof(mi);

    *pCounter += 1;

    _tprintf(_T("%d %p %dx%d+%d+%d"),
             *pCounter,
             hMonitor,
             prcMonitor->left, prcMonitor->top,
             prcMonitor->right, prcMonitor->bottom);
    if (GetMonitorInfo(hMonitor, (LPMONITORINFO)&mi))
    {
        _tprintf(_T("%.*s"), CCHDEVICENAME, mi.szDevice);
        if (mi.dwFlags == MONITORINFOF_PRIMARY)
            _tprintf(_T(" (primary)"));
    }
    else
    {
        PrintError(_T("GetMonitorInfo"), GetLastError());
    }

    _puttchar('\n');
    return TRUE;
}

int
_tmain(int argc, TCHAR *argv)
{
    HDC hdc = NULL; /* NULL means all screens */
    LPRECT prcClip = NULL; /* No clipping */
    int counter = 0;
    BOOL br = EnumDisplayMonitors(hdc, prcClip, MonitorEnumProc, (LPARAM)&counter);
    if (!br)
    {
        PrintError(_T("EnumDisplayMonitors"), GetLastError());
        return 1;
    }
}

static void
PrintError(LPCTSTR szPrefix, DWORD dwError)
{
    LPTSTR lpsz = NULL;
    DWORD cch = 0;

    cch = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER
                        | FORMAT_MESSAGE_FROM_SYSTEM
                        | FORMAT_MESSAGE_IGNORE_INSERTS,
                        NULL, dwError, LANG_NEUTRAL,
                        (LPTSTR)&lpsz, 0, NULL);
    if (cch < 1) {
        cch = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER
                            | FORMAT_MESSAGE_FROM_STRING
                            | FORMAT_MESSAGE_ARGUMENT_ARRAY,
                            "Code 0x%1!08x!",
                            0, LANG_NEUTRAL, (LPTSTR)&lpsz, 0,
                            (va_list*)&dwError);
    }

    _ftprintf(stderr, _T("%s (0x%08x): %s"), szPrefix, dwError, lpsz);

    LocalFree((HLOCAL)lpsz);
}