简体   繁体   English

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

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

I want to create a simple C++ application on windows which check the display turn off time.我想在 Windows 上创建一个简单的 C++ 应用程序来检查显示关闭时间。

After some search I found this function using windows.h经过一番搜索,我使用 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;
}

but when I use this code the time is always zero and in my windows setting i set the windows to turn off display on 5 minutes但是当我使用此代码时,时间始终为零,并且在我的窗口设置中,我将窗口设置为在 5 分钟后关闭显示

I tried some solution my self I changed the time type to long long and I got garbage number a very big number, so what I made wrong to get the screen turn off time.我自己尝试了一些解决方案我将时间类型更改为 long long 并且我得到了一个非常大的数字,所以我做错了什么让屏幕关闭时间。

OS: Windows 10操作系统:Windows 10

Compiler: Mingw32 and i test with MSVC 2015编译器:Mingw32,我用 MSVC 2015 测试

Screen saver timeout and display power-off timeout are two different things.屏幕保护程序超时显示器关机超时是两个不同的事情。

SPI_GETSCREENSAVETIMEOUT returns the screen saver timeout - the time after which the Screen Saver is activated. SPI_GETSCREENSAVETIMEOUT返回屏幕保护程序超时- 屏幕保护程序激活的时间。 If a screen saver was never configured, the value is 0.如果从未配置过屏幕保护程序,则值为 0。

The display power-off timeout is the time after which the power to the screen is cut, and is part of the power profile (and can differ eg for battery vs. AC power).显示器电源关闭超时是屏幕电源被切断后的时间,并且是电源配置文件的一部分(并且可以不同,例如电池与交流电源)。

Use CallNtPowerInformation to get the display power-off timeout:使用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;
    }

}

Example output:示例输出:

Display power-off timeout : 600 s

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

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