在Windows上获取并设置显示器亮度级别

时间:2016-04-07 14:13:55

标签: windows screen-brightness

我们希望在Windows上获取/设置显示器的亮度级别。我们无法找到有效的示例代码。我们继续从GetPhysicalMonitorsFromHMONITOR()获取NULL句柄。任何人都可以提供适用于Windows 7及更高版本的示例代码吗?我们搜索并搜索了工作代码示例,但无法找到任何内容。请参阅下面我们尝试过的代码(包括许多代码)。

typedef struct {
    float * get ;
    float const * set ;
} enum_info_t ;

static
BOOL CALLBACK monitor_callback(
  HMONITOR hMonitor,
  HDC      hdcMonitor,
  LPRECT   lprcMonitor,
  LPARAM   dwData
) {
    BOOL ok ;
    enum_info_t * const info = (enum_info_t *) dwData ;
    eprintf("WINDOWS:brightness:hMonitor=%p info=%p\n", hMonitor, info) ;

    DWORD nmonitor ;
    LPPHYSICAL_MONITOR pPhysicalMonitors = NULL;

    /* Get the number of physical monitors.
     */
    ok = GetNumberOfPhysicalMonitorsFromHMONITOR(
      hMonitor, 
      &nmonitor) ;

    eprintf("WINDOWS:brightness:num_monitors ok=%d count=%ld\n",
        ok, nmonitor) ;

    if (!ok ||
    !nmonitor) {
    goto done ;
    }

    /* Allocate the array of PHYSICAL_MONITOR structures.
     */
    pPhysicalMonitors =
    (LPPHYSICAL_MONITOR) malloc(nmonitor * sizeof(PHYSICAL_MONITOR)) ;

    if (!pPhysicalMonitors) {
    goto done ;
    }

    ok = GetPhysicalMonitorsFromHMONITOR(
    hMonitor, nmonitor, pPhysicalMonitors);

    /* Use the monitor handles (not shown).
     */
    if (ok) {
    for (unsigned ofs=0;ofs<nmonitor;ofs++) {
        LPPHYSICAL_MONITOR pmonitor = &pPhysicalMonitors[ofs] ;
        QString tmp =
        QString::fromWCharArray(pmonitor->szPhysicalMonitorDescription) ;
        seaiq_string_t tmp_s ;

        eprintf("WINDOWS:brightness:monitor[%d] handle=%p description=%s\n",
            ofs, pmonitor->hPhysicalMonitor,
            tmp_s = QString_to_string(tmp)) ;
        seaiq_string_free(tmp_s) ;

        DWORD caps = 0 ;
        DWORD temps = 0 ;
        ok = GetMonitorCapabilities(pmonitor->hPhysicalMonitor, &caps, &temps) ;

        eprintf("WINDOWS:brightness:monitor[%d] ok=%d brightness=%d none=%d\n", ofs, ok,
            (caps & MC_CAPS_BRIGHTNESS) != 0,
            (caps & MC_CAPS_NONE) != 0) ;

        DWORD min = 0 ;
        DWORD cur = 0 ;
        DWORD max = 0 ;
        ok = GetMonitorBrightness(
        pmonitor->hPhysicalMonitor, &min, &cur, &max) ;

        eprintf("WINDOWS:brightness:monitor[%d] ok=%d min=%lu cur=%lu max=%lu\n",
            ofs, ok, min, cur, max) ;
    }

    LPPHYSICAL_MONITOR pmonitor = &pPhysicalMonitors[0] ;

    DWORD min = 0 ;
    DWORD cur = 0 ;
    DWORD max = 0 ;
    ok = GetMonitorBrightness(
        pmonitor->hPhysicalMonitor, &min, &cur, &max) ;

    eprintf("WINDOWS:brightness:GetMonitorBrightness:ok=%d min=%lu cur=%lu max=%lu\n",
        ok, min, cur, max) ;

    if (ok) {
        if (info->get && min < max && min <= cur && cur <= max) {
        *info->get =
            ((float)cur - (float)min) /
            ((float)max - (float)min) ;
        eprintf("WINDOWS:brightness:get:%.3f\n", *info->get) ;
        }

        if (info->set) {
        DWORD dwv = *info->set * (max - min) + min ;

        ok = SetMonitorBrightness(
            pPhysicalMonitors[0].hPhysicalMonitor,
            dwv) ;

        eprintf("WINDOWS:brightness:set:dwv=%ld ok=%d\n", dwv, ok) ;
        }

    }
    }

    /* Close the monitor handles.
     */
    ok = DestroyPhysicalMonitors(
    nmonitor, 
    pPhysicalMonitors);

    /* Free the array.
     */
    free(pPhysicalMonitors);
 done:

    return 1 ;
}

0 个答案:

没有答案