我正在编写一个程序,用于获取笔记本电池的百分比,并显示一个CMD窗口,显示一条消息(例如:10% ->“电量低!”)。
我已经尝试过在Google上搜索,似乎他们都是用C ++或C#尝试的。
有人能帮我用C吗?
编辑:感谢zakinster的回复。它不应该看起来像这样吗?这段代码没有起作用。
编辑:感谢zakinster的回复。它不应该看起来像这样吗?这段代码没有起作用。
#include <Windows.h>
#include <Winbase.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main() {
SYSTEM_POWER_STATUS status;
GetSystemPowerStatus(&status);
unsigned char battery = status.BatteryLifePercent;
printf("%s", battery);
}
winbase.h
,因为它已经被windows.h
包含了。请查看我的最后一次编辑以获取正确的错误检查。此外,status.BatteryLifePercent
是一个 8 位无符号整数,表示电池电量百分比(从 0 到 100),如果未知则为 255,不应该作为字符串字符打印。 - zakinsterstdio.h
和windows.h
就可以了 :) - Xantium