Excerpted from http://blog.csdn.net/li0531/article/details/8818243
A Label control is displayed only in the upper-right corner, so the amount of code is reduced a lot.
[DllImport ("Coredll")]Private Static extern intGetsystempowerstatusex (SYSTEM_POWER_STATUS_EX Lpsystempowerstatus,BOOLfupdate); [DllImport ("Coredll")] Public Static extern voidSystemidletimerreset ();PrivateSYSTEM_POWER_STATUS_EX STATUS =NewSYSTEM_POWER_STATUS_EX ();/// <summary> /////Whether the charge status///// </summary> //private state astate = State.normal;/// <summary> ///Battery current status Charge: charging; Undercharge: low power; normal: battery is in use. /// </summary> Public enumstate{/// <summary> ///in charge/// </summary> Charge,/// <summary> ///Insufficient charge/// </summary> Undercharge,/// <summary> ///Normal State/// </summary> Normal,/// <summary> ///Charging Complete/// </summary> chargefinally};Private classsystem_power_status_ex{ Public byteACLineStatus =0; Public byteBatteryflag =0; Public byteBatteryLifePercent =0; Public byteReserved1 =0; Public UINTBatterylifetime =0; Public UINTBatteryFullLifetime =0; Public byteReserved2 =0; Public byteBackupbatteryflag =0; Public byteBackupbatterylifepercent =0; Public byteRESERVED3 =0; Public UINTBackupbatterylifetime =0; Public UINTBackupbatteryfulllifetime =0;}/// <summary>///To create a power indicator/// </summary>Private voidgetbatterystate () {if(Getsystempowerstatusex (Status,false) ==1) { if(Status. BatteryLifePercent >= the) {Labbattery.text=" -"; Labbattery.forecolor=Color.green; } Else if(Status. BatteryLifePercent <= -) { if(Status. ACLineStatus = =1) Labbattery.text= status. Batterylifepercent.tostring () +" +"; ElseLabbattery.text="! "+status. Batterylifepercent.tostring (); Labbattery.forecolor=color.red; } Else{Labbattery.text= status. Batterylifepercent.tostring () + (status. ACLineStatus = =1?" +":""); Labbattery.forecolor=Color.Blue; } }}
I call this method at a frequency of 10m under the Timer control.
Win CE 5.0 increases battery power display