繁体   English   中英

使用 Win32 API 获取 Windows 屏幕保护程序超时

[英]Get the Windows screen saver timeout using Win32 API

我想在 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;
}

但是当我使用此代码时,时间始终为零,并且在我的窗口设置中,我将窗口设置为在 5 分钟后关闭显示

我自己尝试了一些解决方案我将时间类型更改为 long long 并且我得到了一个非常大的数字,所以我做错了什么让屏幕关闭时间。

操作系统:Windows 10

编译器:Mingw32,我用 MSVC 2015 测试

屏幕保护程序超时显示器关机超时是两个不同的事情。

SPI_GETSCREENSAVETIMEOUT返回屏幕保护程序超时- 屏幕保护程序激活的时间。 如果从未配置过屏幕保护程序,则值为 0。

显示器电源关闭超时是屏幕电源被切断后的时间,并且是电源配置文件的一部分(并且可以不同,例如电池与交流电源)。

使用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(powerPolicy));

    if (ret == ERROR_SUCCESS) {
        std::cout << "Display power-off timeout : " << powerPolicy.VideoTimeout << "s \n";
    }
    else {
        std::cerr << "Error 0x" << std::hex << ret << std::endl;
    }

}

示例输出:

Display power-off timeout : 600 s

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM