使用Win32 API监控电池电量

我正在尝试编写一个小应用程序来监控笔记本电池剩余的电量,我想知道我可以使用哪种Win32function来实现这一目标。

对于Vista及以上版本,您可以使用RegisterPowerSettingNotification

有关早期function,请参阅MSDN页面“电源管理function:Windows Server 2003及更早版本”的本节中的电源管理function。

您可以在codeproject上看到Vista方法的示例代码。

我建议使用Win32 GetSystemPowerStatus函数。 代码段:

int getBatteryLevel() { SYSTEM_POWER_STATUS status; GetSystemPowerStatus(&status); return status.BatteryLifePercent; } 

以下链接中有一篇非常详细的文章,以及一个示例项目(在Win8.1上测试过): http : //www.codeproject.com/Articles/15829/Vista-Goodies-in-C-Monitoring-在-电脑的S-power