我想在Windows上创建一个简单的C++应用程序,检查显示关闭时间.
经过一些搜索,我发现这个函数使用的是windows.h
int time;
bool check;
check = SystemParametersInfo(SPI_GETSCREENSAVETIMEOUT, 0, &time, 0);
if (check) {
cout << "The Screen Saver time is : " << time << endl;
}
else {
cout << "Sorry dude the windows api can't do it" << endl;
}
Run Code Online (Sandbox Code Playgroud)
但是当我使用这段代码时,时间总是为零,在我的Windows设置中,我将窗口设置为在5分钟后关闭显示
我尝试了一些自己的解决方案我将时间类型更改为long long并且垃圾编号非常大,所以我做错了让屏幕关闭时间.
操作系统:Windows 10
编译器:Mingw32和我测试MSVC 2015
SPI_GETSCREENSAVETIMEOUT是过时的API(太糟糕的微软从未提及它).屏幕保护程序超时现在是功率配置文件的一部分,并且可能不同,例如电池与交流电源.
使用CallNtPowerInformation
来获取屏幕保护程序超时:
#include <iostream>
#include <windows.h>
#include <powerbase.h>
#pragma comment(lib, "PowrProf.lib")
int main() {
SYSTEM_POWER_POLICY powerPolicy;
DWORD ret;
ret = CallNtPowerInformation(SystemPowerPolicyCurrent, nullptr, 0, &powerPolicy, sizeof(SYSTEM_POWER_POLICY));
if (ret == ERROR_SUCCESS) {
std::cout << "The Screen Saver time is : " << powerPolicy.VideoTimeout << std::endl;
}
else {
std::cerr << "Error 0x" << std::hex << ret << std::endl;
}
}
Run Code Online (Sandbox Code Playgroud)
输出:
The Screen Saver time is : 600
Run Code Online (Sandbox Code Playgroud)