2

I was wondering what is the native way to adjust the brightness in Windows?

By native, I mean the method that also displays the brightness overlay in the top left corner in Windows 8, 8.1, and 10, as if the special brightness keys have been pressed.

I was looking all over the Internet for this, but some solutions that do work, adjust the brightness, but no overlay is shown. Any idea? Is there something like

SendMessage(HWND_BROADCAST, WM_SYSCOMMAND, SC_MONITORPOWER, 2);

which turns off the monitor, but for brightness, that can be used from C++? Or C#? Thanks.

Update: here is the sample code.

HMONITOR hMonitor = NULL;
DWORD cPhysicalMonitors;
LPPHYSICAL_MONITOR pPhysicalMonitors = NULL;

// Get the monitor handle.
hMonitor = MonitorFromWindow(GetForegroundWindow(), MONITOR_DEFAULTTOPRIMARY);

// Get the number of physical monitors.
BOOL bSuccess = GetNumberOfPhysicalMonitorsFromHMONITOR(
    hMonitor,
    &cPhysicalMonitors
);

if (bSuccess)
{
    // Allocate the array of PHYSICAL_MONITOR structures.
    pPhysicalMonitors = (LPPHYSICAL_MONITOR)malloc(
        cPhysicalMonitors * sizeof(PHYSICAL_MONITOR));

    if (pPhysicalMonitors != NULL)
    {
        // Get the array.
        bSuccess = GetPhysicalMonitorsFromHMONITOR(
            hMonitor, cPhysicalMonitors, pPhysicalMonitors);

        // Use the monitor handles (not shown).
        DWORD pdwMinimumBrightness = 0;
        DWORD pdwCurrentBrightness = 0;
        DWORD pdwMaximumBrightness = 0;

        DWORD dwMonitorCapabilities = 0;
        DWORD dwSupportedColorTemperatures = 0;

        bSuccess = GetMonitorCapabilities(pPhysicalMonitors, &dwMonitorCapabilities, &dwSupportedColorTemperatures);

        cout << bSuccess << endl;

        // Close the monitor handles.
        bSuccess = DestroyPhysicalMonitors(
            cPhysicalMonitors,
            pPhysicalMonitors);

        // Free the array.
        free(pPhysicalMonitors);
    }
}

cout << bSuccess always writes 0 in terminal.

GetMonitorCapabilities fails with error -1071241854 (0xC0262582: "An error occurred while transmitting data to the device on the I2C bus."). Then how do the brightness keys on my computer work?

Valentin Radu
  • 629
  • 1
  • 11
  • 26
  • possible duplicate http://stackoverflow.com/questions/4013622/adjust-screen-brightness-using-c-sharp – JokingBatman Jun 07 '16 at 13:35
  • 1
    You've got this backwards: It's not the brightness controlling API that displays the GUI. It's the GUI application that calls the API to control the brightness. – IInspectable Jun 07 '16 at 14:03
  • Okay, so how do I display the GUI then, and control it? Because afaik there is no key defined as brightness down/up, the same way volume down/up exist, so there is clearly a driver doing this on my PC, so it should be possible to achieve the same from own software. Anyway GetMonitorBrightness fails for me for whatever reason. – Valentin Radu Jun 07 '16 at 14:05
  • 1
    The GUI is automatic. Set the brightness, and let windows handle the rest. If `Get/SetMonitorBrightness` fails then you should show us the code that you're using. Preferably in the form of a [mcve] – theB Jun 07 '16 at 14:14
  • Okay, I updated the post with the code I have – Valentin Radu Jun 07 '16 at 14:19
  • The obvious way to fake it would be to figure out what keys correspond to the brightness up/down buttons, and then simulate presses of those keys. – Cody Gray - on strike Jun 07 '16 at 15:36
  • Yeah, easy said, hard to do. Those keys are Fn+F5, and Fn+F6, so no way you can touch them from software... I mean they do not produce key codes... – Valentin Radu Jun 07 '16 at 15:51
  • In the code you posted I don't see anywhere where you call `GetMonitorBrightness` – theB Jun 07 '16 at 15:59
  • sorry, GetMonitorCapabilities, returns the same error – Valentin Radu Jun 07 '16 at 16:13
  • The Fn key modifies the scan codes sent by the hardware. So all you need to do is figure out what scan codes are actually sent when you press Fn+F5 and Fn+F6. If this program will work on any system, then the hardware-dependent nature of scan codes is clearly not a problem. – Cody Gray - on strike Jun 07 '16 at 16:16
  • I don't know, Fn generates a scan code, but then when I press F5, and/or F6, nothing is generated. Plus, I would like this to work on any platform, not only on my PC. There is no brightness down/up key. Probably I have a driver on my PC that acts when the methods for Fn+F5, and Fn+F6 happen in the DSDT, and then the driver does all the work of toggling the brightness, and showing the overlay somehow. No one has any clue? Even if GetMonitorCapabilities does not work on my system, something has to, I can clearly change the brightness of my screen. – Valentin Radu Jun 07 '16 at 16:57
  • Re the error calling the function, see Hans Passant's answer [here](http://stackoverflow.com/a/31015580/5240004) The FN+ keys are probably functioning through one of the system's drivers, or changing the hardware settings directly, which windows is just reacting to. – theB Jun 07 '16 at 20:42
  • Okay, but I still can use an app like NirSoft's nircmd to change the brightness, so it clearly be changed somehow... Pity that Windows does not have a standard way that is working all the time. – Valentin Radu Jun 07 '16 at 21:13
  • Windows *does* have a standard way. You've already found it: the SetMonitorBrightness function. The problem is your hardware drivers. The last remaining option I can think of is to use WMI. That may well be what NirSoft does. https://blogs.technet.microsoft.com/heyscriptingguy/2013/07/25/use-powershell-to-report-and-set-monitor-brightness/ Still requires hardware support, of course. Try it and see if you get lucky. – Cody Gray - on strike Jun 08 '16 at 05:42

0 Answers0