#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