如何在C/C++应用中读写Windows显示器变暗相关电源设置?
要在C/C++应用里搞定Windows的“Dim the display after”和“Display dimming brightness”这两个高级电源设置,咱们得用到Windows提供的电源计划API(主要是PowrProf.h里的函数)。下面一步步给你拆解查询和设置的实现方式:
查询这两项设置
首先,你需要获取当前活动电源计划的句柄,然后分别针对AC(插电)和DC(电池)场景读取对应的值。这里要用到几个预定义的GUID,它们对应电源计划里的显示子组和具体设置项:
- 显示设置子组:
GUID_DISPLAY_SUBGROUP - 变暗超时(AC/DC通用):
GUID_VIDEO_DIM_TIMEOUT - 变暗亮度(AC/DC通用):
GUID_VIDEO_DIM_BRIGHTNESS
以下是完整的查询示例代码:
#include <Windows.h> #include <PowrProf.h> #include <iostream> // 链接PowrProf库,编译时需要 #pragma comment(lib, "PowrProf.lib") int main() { // 获取当前活动电源计划的句柄 GUID* pActiveScheme = nullptr; if (PowerGetActiveScheme(nullptr, &pActiveScheme) != ERROR_SUCCESS) { std::cerr << "Failed to get active power scheme. Error code: " << GetLastError() << std::endl; return 1; } DWORD dataSize = sizeof(DWORD); DWORD value = 0; // 读取AC状态下的变暗超时(单位:秒,0表示从不变暗) if (PowerReadACValueIndex(nullptr, pActiveScheme, &GUID_DISPLAY_SUBGROUP, &GUID_VIDEO_DIM_TIMEOUT, &value, &dataSize) == ERROR_SUCCESS) { std::cout << "AC - Dim display after: " << value << " seconds" << std::endl; } else { std::cerr << "Failed to read AC dim timeout. Error code: " << GetLastError() << std::endl; } // 读取DC状态下的变暗超时 dataSize = sizeof(DWORD); if (PowerReadDCValueIndex(nullptr, pActiveScheme, &GUID_DISPLAY_SUBGROUP, &GUID_VIDEO_DIM_TIMEOUT, &value, &dataSize) == ERROR_SUCCESS) { std::cout << "DC - Dim display after: " << value << " seconds" << std::endl; } else { std::cerr << "Failed to read DC dim timeout. Error code: " << GetLastError() << std::endl; } // 读取AC状态下的变暗亮度(0-100,百分比) dataSize = sizeof(DWORD); if (PowerReadACValueIndex(nullptr, pActiveScheme, &GUID_DISPLAY_SUBGROUP, &GUID_VIDEO_DIM_BRIGHTNESS, &value, &dataSize) == ERROR_SUCCESS) { std::cout << "AC - Display dimming brightness: " << value << "%" << std::endl; } else { std::cerr << "Failed to read AC dim brightness. Error code: " << GetLastError() << std::endl; } // 读取DC状态下的变暗亮度 dataSize = sizeof(DWORD); if (PowerReadDCValueIndex(nullptr, pActiveScheme, &GUID_DISPLAY_SUBGROUP, &GUID_VIDEO_DIM_BRIGHTNESS, &value, &dataSize) == ERROR_SUCCESS) { std::cout << "DC - Display dimming brightness: " << value << "%" << std::endl; } else { std::cerr << "Failed to read DC dim brightness. Error code: " << GetLastError() << std::endl; } // 释放资源 CloseHandle(pActiveScheme); return 0; }
设置这两项设置
设置流程和查询类似,只是把读取函数换成PowerWriteACValueIndex和PowerWriteDCValueIndex。修改完成后,需要调用PowerSetActiveScheme提交更改,确保设置生效。
以下是完整的设置示例代码:
#include <Windows.h> #include <PowrProf.h> #include <iostream> #pragma comment(lib, "PowrProf.lib") int main() { // 获取当前活动电源计划句柄 GUID* pActiveScheme = nullptr; if (PowerGetActiveScheme(nullptr, &pActiveScheme) != ERROR_SUCCESS) { std::cerr << "Failed to get active power scheme. Error code: " << GetLastError() << std::endl; return 1; } // -------------------------- // 设置AC状态下的参数 // -------------------------- // 10分钟后变暗(600秒) const DWORD acDimTimeout = 600; if (PowerWriteACValueIndex(nullptr, pActiveScheme, &GUID_DISPLAY_SUBGROUP, &GUID_VIDEO_DIM_TIMEOUT, acDimTimeout) != ERROR_SUCCESS) { std::cerr << "Failed to set AC dim timeout. Error code: " << GetLastError() << std::endl; CloseHandle(pActiveScheme); return 1; } // 变暗后亮度为50% const DWORD acDimBrightness = 50; if (PowerWriteACValueIndex(nullptr, pActiveScheme, &GUID_DISPLAY_SUBGROUP, &GUID_VIDEO_DIM_BRIGHTNESS, acDimBrightness) != ERROR_SUCCESS) { std::cerr << "Failed to set AC dim brightness. Error code: " << GetLastError() << std::endl; CloseHandle(pActiveScheme); return 1; } // -------------------------- // 设置DC状态下的参数 // -------------------------- // 5分钟后变暗(300秒) const DWORD dcDimTimeout = 300; if (PowerWriteDCValueIndex(nullptr, pActiveScheme, &GUID_DISPLAY_SUBGROUP, &GUID_VIDEO_DIM_TIMEOUT, dcDimTimeout) != ERROR_SUCCESS) { std::cerr << "Failed to set DC dim timeout. Error code: " << GetLastError() << std::endl; CloseHandle(pActiveScheme); return 1; } // 变暗后亮度为30% const DWORD dcDimBrightness = 30; if (PowerWriteDCValueIndex(nullptr, pActiveScheme, &GUID_DISPLAY_SUBGROUP, &GUID_VIDEO_DIM_BRIGHTNESS, dcDimBrightness) != ERROR_SUCCESS) { std::cerr << "Failed to set DC dim brightness. Error code: " << GetLastError() << std::endl; CloseHandle(pActiveScheme); return 1; } // 提交更改,确保设置生效 if (PowerSetActiveScheme(nullptr, pActiveScheme) != ERROR_SUCCESS) { std::cerr << "Failed to apply power scheme changes. Error code: " << GetLastError() << std::endl; CloseHandle(pActiveScheme); return 1; } std::cout << "Power settings updated successfully!" << std::endl; // 释放资源 CloseHandle(pActiveScheme); return 0; }
关键注意事项
- 权限要求:修改电源计划设置需要管理员权限,如果你的应用没有以管理员身份运行,调用写入函数会返回
ERROR_ACCESS_DENIED错误。 - 参数范围:
- 超时值:0表示从不变暗,其他值为秒数,Windows UI通常限制在1分钟到5小时,但API支持更大的数值。
- 亮度值:0-100的整数,代表百分比,0是最暗,100表示变暗后亮度和原始亮度一致(相当于不变暗)。
- 编译链接:必须链接
PowrProf.lib库,代码里的#pragma comment(lib, "PowrProf.lib")已经自动处理了这一点,也可以在项目设置中手动添加该库。
内容的提问来源于stack exchange,提问作者Willy K.
相关产品推荐
相关产品推荐

