使用 Win32 API 监视电池电量
我正在尝试编写一个小应用程序来监视笔记本电池的剩余电量,我想知道可以使用哪个 Win32 函数来完成此任务。
I'm trying to write a small app that monitors how much power is left in a notebook battery and I'd like to know which Win32 function I could use to accomplish that.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
对于 Vista 及更高版本,您可以使用 RegisterPowerSettingNotification
对于更早版本函数请参阅本节中的电源管理函数 MSDN 页面“电源管理功能:Windows Server 2003 及更早版本”
您可以在 代码项目。
For Vista and up you can use RegisterPowerSettingNotification
For earlier functions see the Power Management Functions in this section of the MSDN page "Power Management Functions: Windows Server 2003 and Earlier"
You can see example code of the Vista method on codeproject.
我建议使用 Win32 GetSystemPowerStatus 函数。 代码片段:
I recommend the use of the Win32 GetSystemPowerStatus function. A code snippet :
以下链接中有一篇非常详细的文章,以及一个示例项目(在 Win8.1 上测试过): http://www.codeproject.com/Articles/15829/Vista-Goodies-in-C-Monitoring-the-Computer-s-Power
There is a very detailed article in the following link, as well as a n example project (tested working on Win8.1): http://www.codeproject.com/Articles/15829/Vista-Goodies-in-C-Monitoring-the-Computer-s-Power
您可以监听
PBT_APMPOWERSTATUSCHANGE
。该事件在以下3种情况下触发。
理想的方法是致电 GetSystemPowerStatus() 接收此事件时,而不是连续轮询。
SYSTEM_POWER_STATUS
的BatteryLifePercent
成员将以百分比形式给出电池寿命。对于此事件,您必须处理
WM_POWERBROADCAST
消息。 对于PBT_APMPOWERSTATUSCHANGE
,wParam 将为PBT_APMPOWERSTATUSCHANGE
You can listen for
PBT_APMPOWERSTATUSCHANGE
.This event is triggered in the following 3 scenarios.
The ideal way is to call GetSystemPowerStatus() on receiving this event, rather than polling continuously. The
BatteryLifePercent
member ofSYSTEM_POWER_STATUS
will give battery life in percentages.For this event, you will have to handle the
WM_POWERBROADCAST
message. ForPBT_APMPOWERSTATUSCHANGE
wParam will bePBT_APMPOWERSTATUSCHANGE