最在WinCE上搞几个小应用,没有自己定制WinCE系统(一不会,二没空研究),在用户手上使用时老是会没电关机。然后安装的插件什么的都会丢失相关配置(安装ResidentFlash目录下,不知道为什么还会丢失)。后来写了个在WinCE下获取电池状态及电量的控件,在快没电时提示去充电,不充给用用这个方法果然好了很多,当然这是治标不治本的方法。不扯了正文:
获取电池状态及电池电量主要用WINCE下的API:GetSystemPowerStatusEx2 这个函数。下面是头文件、所属库、最低支持版本
Header | winbase.h |
Library | coredll.lib |
Windows Embedded CE | Windows CE 2.12 and later |
Windows Mobile | Windows Mobile Version 5.0 and later |
原型如下
DWORD GetSystemPowerStatusEx2(PSYSTEM_POWER_STATUS_EX2 pSystemPowerStatusEx2,DWORD dwLen, BOOL fUpdate);附 MSDN 地址
C#中声明
[DllImport("coredll.dll")]
public static extern uint GetSystemPowerStatusEx2(SYSTEM_POWER_STATUS_EX2 pSystemPowerStatusEx2, uint dwLen, [MarshalAs(UnmanagedType.Bool)]bool fUpdate);
SYSTEM_POWER_STATUS_EX2 如下
[StructLayout(LayoutKind.Sequential)]
public class SYSTEM_POWER_STATUS_EX2
{
/// <summary>
/// AC电源状态
/// </summary>
public byte ACLineStatus;
/// <summary>
/// 电池充电状态
/// </summary>
public byte BatteryFlag;
/// <summary>
/// 电池剩余的电量百分比
/// </summary>
public byte BatteryLifePercent;
/// <summary>
/// 保留字段
/// </summary>
public byte Reserved1;
/// <summary>
/// 电池的剩余续航时间(秒)
/// </summary>
public uint BatteryLifeTime;
/// <summary>
/// 电池的寿命
/// </summary>
public uint BatteryFullLifeTime;
/// <summary>
/// 保留字段
/// </summary>
public byte Reserved2;
/// <summary>
/// 备用电池充电状态
/// </summary>
public byte BackupBatteryFlag;
/// <summary>
/// 备用电池剩余的电量百分比
/// </summary>
public byte BackupBatteryLifePercent;
/// <summary>
/// 保留字段
/// </summary>
public byte Reserved3;
/// <summary>
/// 备用电池的剩余寿命
/// </summary>
public uint BackupBatteryLifeTime;
/// <summary>
/// 备用电池寿命
/// </summary>
public uint BackupBatteryFullLifeTime;
/// <summary>
/// 电池电压毫伏量
/// </summary>
public uint BatteryVoltage;
/// <summary>
/// 瞬时电流消在毫安量
/// </summary>
public uint BatteryCurrent;
/// <summary>
///
/// </summary>
public uint BatteryAverageCurrent;
/// <summary>
///
/// </summary>
public uint BatteryAverageInterval;
/// <summary>
/// 长期累积平均放电每小时/毫安(MAH)
/// </summary>
public uint BatterymAHourConsumed;
/// <summary>
/// 电池的温度,单位为摄氏度
/// </summary>
public uint BatteryTemperature;
/// <summary>
/// 备用电池电压
/// </summary>
public uint BackupBatteryVoltage;
/// <summary>
///
/// </summary>
public byte BatteryChemistry;
}
使用
uint result = GetSystemPowerStatusEx2(PowerState, (uint)System.Runtime.InteropServices.Marshal.SizeOf(typeof(SYSTEM_POWER_STATUS_EX2)), true);
是否充电ACLineStatus, 0未充电,1正在充电
充电状态BatteryFlag,128已充满,8使用中,1充电中