C++获取电池信息

#include <iostream>
#include <Windows.h>
#include <powerbase.h>
#include <string>

using namespace std;

int main(int argc, char* argv[]) {


    /*SYSTEM_POWER_STATUS sysPower = { 0 };
    GetSystemPowerStatus(&sysPower);
    cout << "       ACLineStatus: " << (int)sysPower.ACLineStatus << endl;
    cout << "        BatteryFlag: " << (int)sysPower.BatteryFlag << endl;
    cout << " BatteryLifePercent: " << (int)sysPower.BatteryLifePercent << " %" << endl;
    cout << "    BatteryLifeTime: " << sysPower.BatteryLifeTime << " S" << endl;
    cout << "BatteryFullLifeTime: " << sysPower.BatteryFullLifeTime << " S" << endl;
    system("pause");

    SYSTEM_BATTERY_STATE bat;
    CallNtPowerInformation(SystemBatteryState, NULL, 0, &bat, sizeof(SYSTEM_BATTERY_STATE));
    cout << "AcOnLine:" << (int)bat.AcOnLine << endl;
    cout << "BatteryPresent:" << (int)bat.BatteryPresent << endl;
    cout << "Charging:" << (int)bat.Charging << endl;
    cout << "DefaultAlert1:" << (int)bat.DefaultAlert1 << endl;
    cout << "DefaultAlert2:" << (int)bat.DefaultAlert2 << endl;
    cout << "Discharging:" << (int)bat.Discharging << endl;
    cout << "EstimatedTime:" << (int)bat.EstimatedTime << endl;
    cout << "MaxCapacity:" << (int)bat.MaxCapacity << endl;
    cout << "Rate:" << (int)bat.Rate << endl;
    cout << "RemainingCapacity:" << (int)bat.RemainingCapacity << endl;
    cout << "Spare1:" << (int)bat.Spare1 << endl;
    cout << "Tag:" << (int)bat.Tag << endl;
    system("pause");*/

    int temp_BatteryLifePercent, temp_AcOnLine, temp_Rate, temp_Charging, temp_Discharging;
    string str_AC, str_Charge;
    SYSTEM_POWER_STATUS sysPower = { 0 };
    int i = 0, j = 0;
    i = atoi(argv[1]);
    j = atoi(argv[2]);
    int temp = 1;

    while (temp)
    {
        GetSystemPowerStatus(&sysPower);
        SYSTEM_BATTERY_STATE sysBat = { 0 };
        CallNtPowerInformation(SystemBatteryState, NULL, 0, &sysBat, sizeof(SYSTEM_BATTERY_STATE));

        temp_BatteryLifePercent = (int)sysPower.BatteryLifePercent;
        temp_AcOnLine = (int)sysBat.AcOnLine;
        temp_Rate = (int)sysBat.Rate;
        temp_Charging = (int)sysBat.Charging;
        temp_Discharging = (int)sysBat.Discharging;

        if (1 == temp_AcOnLine) {
            str_AC = "AcOnLine";
        }
        else {
            str_AC = "AcOffLine";
        }

        if (1 == temp_Charging) {
            str_Charge = "Charging";
        }
        else if (1 == temp_Discharging) {
            str_Charge = "Discharging";
        }
        else {
            str_Charge = "Discharging";
        }

        system("cls");
        system("COLOR 07");
        cout << "Circulation :" << atoi(argv[1]) << endl;
        cout << "Level Rate  :" << atoi(argv[2]) << endl;
        cout << "==============================" << endl;

        if (1 == temp_Charging)
        {
            system("COLOR 02");
            if (temp_Rate <= j)
            {
                cout << "AC          :" << str_AC << endl;
                cout << "Charge      :" << str_Charge << endl;
                cout << "BatteryLevel:" << temp_BatteryLifePercent << "%" << endl;
                cout << "Current Rate:" << temp_Rate << endl;
                system("COLOR 04");
                system("pause");
                return 1;
            }
        }

        if (0 == i) {
            cout << "AC          :" << str_AC << endl;
            cout << "Charge      :" << str_Charge << endl;
            cout << "BatteryLevel:" << temp_BatteryLifePercent << "%" << endl;
            cout << "Current Rate:" << temp_Rate << endl;
            temp = 0;
        }
        else
        {
            cout << "AC          :" << str_AC << endl;
            cout << "Charge      :" << str_Charge << endl;
            cout << "BatteryLevel:" << temp_BatteryLifePercent << "%" << endl;
            cout << "Current Rate:" << temp_Rate << endl;
            system("Sleep /s 3");
        }
    }

    return 0;
}

GetSystemPowerStatus函数信息:https://docs.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-getsystempowerstatus

CallNtPowerInformation函数信息:https://docs.microsoft.com/en-us/windows/win32/api/powerbase/nf-powerbase-callntpowerinformation

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值