使用 Win32 API 监视电池电量

发布于 2024-07-07 15:57:27 字数 61 浏览 10 评论 0原文

我正在尝试编写一个小应用程序来监视笔记本电池的剩余电量,我想知道可以使用哪个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

心碎的声音 2024-07-14 15:57:27

对于 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.

べ繥欢鉨o。 2024-07-14 15:57:27

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

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

I recommend the use of the Win32 GetSystemPowerStatus function. A code snippet :

int getBatteryLevel()
{
    SYSTEM_POWER_STATUS status;
    GetSystemPowerStatus(&status);
    return status.BatteryLifePercent;
}
如何视而不见 2024-07-14 15:57:27

以下链接中有一篇非常详细的文章,以及一个示例项目(在 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

一指流沙 2024-07-14 15:57:27

您可以监听 PBT_APMPOWERSTATUSCHANGE
该事件在以下3种情况下触发。

  1. 电池寿命下降到不足 5 分钟电量,或
  2. 电池寿命下降到 10% 以下,
  3. 电池寿命变化 3%

理想的方法是致电 GetSystemPowerStatus() 接收此事件时,而不是连续轮询。 SYSTEM_POWER_STATUSBatteryLifePercent 成员将以百分比形式给出电池寿命。

对于此事件,您必须处理 WM_POWERBROADCAST 消息。 对于 PBT_APMPOWERSTATUSCHANGE,wParam 将为 PBT_APMPOWERSTATUSCHANGE

You can listen for PBT_APMPOWERSTATUSCHANGE.
This event is triggered in the following 3 scenarios.

  1. Battery life drops to less than 5 minutes of power, or
  2. Battery life falls below 10%,
  3. Battery life changes by 3%

The ideal way is to call GetSystemPowerStatus() on receiving this event, rather than polling continuously. The BatteryLifePercent member of SYSTEM_POWER_STATUS will give battery life in percentages.

For this event, you will have to handle the WM_POWERBROADCAST message. For PBT_APMPOWERSTATUSCHANGE wParam will be PBT_APMPOWERSTATUSCHANGE

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文