How to set brightness for one specific monitor

Good day everyone, I'll try my best to describe my issue.

I'm writing a software with Visual C++ .Net Framework. Goal is to plug in a monitor, and the software will be able to change wallpaper and brightness for said monitor.

I've solved the wallpaper issue. The user is able to select monitor (Numbers match monitor identity numbers in Display Settings).

With brightness, I'd like to do something like this here: https://github.com/emoacht/Monitorian


The problem is that you can't seem to get the monitor handle via monitor ID.
I'm able to get the monitor handles with EnumDisplayMonitors
However the order do not match the monitor order in the Display Settings.

I am able to identify this current monitor based on its width/height. But I want a much more reliable variable as the monitor might differ in size in the future.

Please see code snippet below.

So my question is, what would the most straight forward and reliable way be of changing brightness of one specific monitor.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
struct MonitorRects
{
    static BOOL GetMonitorByIndex(HMONITOR hMonitor, HDC hdcMonitor, LPRECT lprcMonitor, LPARAM dwData)
    {
        sEnumInfo* info = (sEnumInfo*)dwData;
        if (--info->iIndex < 0)
        {
            info->hMonitor = hMonitor;
            return FALSE;
        }
        return TRUE;
    }

    HMONITOR getMonitorHandle() {
        sEnumInfo info;
        info.iIndex = 0;
        info.hMonitor = NULL;

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

        return NULL;
    }
};

    HMONITOR hMon = mony.getMonitorHandle();

    if (hMon == NULL) return;

    DWORD num_of_monitors;
    BOOL bOK = GetNumberOfPhysicalMonitorsFromHMONITOR(hMon, &num_of_monitors);

    LPPHYSICAL_MONITOR pPhysical_monitors = (LPPHYSICAL_MONITOR)malloc(num_of_monitors * sizeof(
		PHYSICAL_MONITOR));

    GetPhysicalMonitorsFromHMONITOR(hMon, num_of_monitors, pPhysical_monitors);

    HANDLE hMonitor = pPhysical_monitors[0].hPhysicalMonitor;
    SetMonitorBrightness(hMonitor, 0);

Last edited on
Registered users can post here. Sign in or register to post.