WINCE 电池状态(C#)

时间:2021-07-06 17:58:57

WINCE 电池状态(C#)              

分类:             电量                   397人阅读     评论(1)     收藏     举报     Wince电量

 

根据网上搜集资料,最后整理 分享下

[csharp] view plaincopyprint?
  1. using System;  
  2. using System.Linq;  
  3. using System.Collections.Generic;  
  4. using System.Text;  
  5. using System.Runtime.InteropServices;  
  6. using System.Drawing;  
  7.   
  8. namespace PDAeffect  
  9. {  
  10.    public class BatteryControl  
  11.     {  
  12.        /// <summary>  
  13.         /// 外边框  
  14.         /// </summary>  
  15.         private Point[] BatteryPolygon = new Point[8];  
  16.         /// <summary>  
  17.         /// 是否充电状态  
  18.         /// </summary>  
  19.         private State AState = State.Normal;  
  20.         private int Percent = 0;  
  21.         private int iLeft = 30 + 2, iTop = 178, iWidth = 20, iHeight = 10;  
  22.         private int iRectWidth = 0;  
  23.         private Rectangle Rect = new Rectangle();  
  24.         private string text = "";  
  25.         private SYSTEM_POWER_STATUS_EX status = new SYSTEM_POWER_STATUS_EX();  
  26.         /// <summary>  
  27.         /// 电池当前状态 Charge:充电中;UnderCharge:电量不足;Normal:电池正常使用.  
  28.         /// </summary>  
  29.         public enum State  
  30.         {  
  31.             /// <summary>  
  32.             /// 充电中  
  33.             /// </summary>  
  34.             Charge,  
  35.             /// <summary>  
  36.             /// 充电不足  
  37.             /// </summary>  
  38.             UnderCharge,  
  39.             /// <summary>  
  40.             /// 正常状态  
  41.             /// </summary>  
  42.             Normal,  
  43.             /// <summary>  
  44.             /// 充电完成  
  45.             /// </summary>  
  46.             ChargeFinally  
  47.         };  
  48.         private class SYSTEM_POWER_STATUS_EX  
  49.         {  
  50.             public byte ACLineStatus = 0;  
  51.             public byte BatteryFlag = 0;  
  52.             public byte BatteryLifePercent = 0;  
  53.             public byte Reserved1 = 0;  
  54.             public uint BatteryLifeTime = 0;  
  55.             public uint BatteryFullLifeTime = 0;  
  56.             public byte Reserved2 = 0;  
  57.             public byte BackupBatteryFlag = 0;  
  58.             public byte BackupBatteryLifePercent = 0;  
  59.             public byte Reserved3 = 0;  
  60.             public uint BackupBatteryLifeTime = 0;  
  61.             public uint BackupBatteryFullLifeTime = 0;  
  62.         }  
  63.         [DllImport("coredll")]  
  64.         private static extern int GetSystemPowerStatusEx(SYSTEM_POWER_STATUS_EX lpSystemPowerStatus, bool fUpdate);  
  65.         [DllImport("coredll")]  
  66.         public static extern void SystemIdleTimerReset();  
  67.         /// <summary>  
  68.         /// 构造函数 在屏幕默认位置构建电池形状  
  69.         /// </summary>  
  70.         public void Battery()  
  71.         {  
  72.             SetPolygon();  
  73.         }  
  74.         /// <summary>  
  75.         /// 构造函数 电池形状的X和Y坐标  
  76.         /// </summary>  
  77.         /// <param name="StartLeft">电池形状的X坐标</param>  
  78.         /// <param name="StartTop">电池形状的Y坐标</param>  
  79.         public void Battery(int StartLeft, int StartTop)  
  80.         {  
  81.             iLeft = StartLeft;  
  82.             iTop = StartTop;  
  83.             SetPolygon();  
  84.         }  
  85.         /// <summary>  
  86.         /// 构造函数 根据X坐标、Y坐标、宽度、高度构造电池形状  
  87.         /// </summary>  
  88.         /// <param name="StartLeft">电池形状的X坐标</param>  
  89.         /// <param name="StartTop">电池形状的Y坐标</param>  
  90.         /// <param name="StartWidth">电池形状的宽度</param>  
  91.         /// <param name="StartHeight">电池形状的高度</param>  
  92.         public void  Battery(int StartLeft, int StartTop, int StartWidth, int StartHeight)  
  93.         {  
  94.             iLeft = StartLeft;  
  95.             iTop = StartTop;  
  96.             iWidth = StartWidth;  
  97.             iHeight = StartHeight;  
  98.             SetPolygon();  
  99.         }  
  100.         /// <summary>  
  101.         /// 设置电池形状  
  102.         /// </summary>  
  103.         void SetPolygon()  
  104.         {  
  105.             int Head = 2;  
  106.             int HeightLowHalf = (Height - Head) / 2;  
  107.             //外边框  
  108.             BatteryPolygon[0].X = iLeft;  
  109.             BatteryPolygon[0].Y = iTop;  
  110.             BatteryPolygon[1].X = iLeft + iWidth;  
  111.             BatteryPolygon[1].Y = iTop;  
  112.             BatteryPolygon[2].X = iLeft + iWidth;  
  113.             BatteryPolygon[2].Y = iTop + HeightLowHalf;  
  114.             BatteryPolygon[3].X = iLeft + iWidth + Head;  
  115.             BatteryPolygon[3].Y = iTop + HeightLowHalf;  
  116.             BatteryPolygon[4].X = iLeft + iWidth + Head;  
  117.             BatteryPolygon[4].Y = iTop + HeightLowHalf + Head;  
  118.             BatteryPolygon[5].X = iLeft + iWidth;  
  119.             BatteryPolygon[5].Y = iTop + HeightLowHalf + Head;  
  120.             BatteryPolygon[6].X = iLeft + iWidth;  
  121.             BatteryPolygon[6].Y = iTop + HeightLowHalf + Head + HeightLowHalf;  
  122.             BatteryPolygon[7].X = iLeft;  
  123.             BatteryPolygon[7].Y = iTop + HeightLowHalf + Head + HeightLowHalf;  
  124.             //内矩形  
  125.             Rect.X = BatteryPolygon[0].X + 2;  
  126.             Rect.Y = BatteryPolygon[0].Y + 2;  
  127.             Rect.Width = BatteryPolygon[6].X - BatteryPolygon[0].X - 3;  
  128.             Rect.Height = BatteryPolygon[6].Y - BatteryPolygon[0].Y - 3;  
  129.             iRectWidth = Rect.Width;  
  130.             GetBatteryState();  
  131.         }  
  132.         /// <summary>  
  133.         /// 获取电池状态  
  134.         /// </summary>  
  135.        public void GetBatteryState()  
  136.         {  
  137.             if (GetSystemPowerStatusEx(status, false) == 1)  
  138.             {  
  139.                 if (status.ACLineStatus == 1)  
  140.                 {  
  141.                     //BatteryFlag = 128  充电完成  
  142.                     if(status.BatteryFlag == 128)  
  143.                     //if (status.BatteryLifePercent >= 100)  
  144.                     {  
  145.                         //status.BatteryLifePercent = 100;  //.BatteryFullLifeTime  
  146.                         text = "充电完成...";  
  147.                         AState = State.ChargeFinally;  
  148.                     }  //BatteryFlag = 8 正在充电  
  149.                     else  
  150.                     {  
  151.                         AState = State.Charge;  
  152.                         text = "充电中...";  
  153.                     }  
  154.                 }  
  155.                 else  
  156.                 {  
  157.                     //BatteryFlag = 1 正在使用电池  
  158.                     AState = State.Normal;                     
  159.                     if (status.BatteryLifePercent > 100) status.BatteryLifePercent = 100;  
  160.                     text = status.BatteryLifePercent.ToString() + "%";  
  161.                 }  
  162.                 Percent = status.BatteryLifePercent;  
  163.                 if (Percent <= 20)  
  164.                 {  
  165.                     AState = State.UnderCharge;  
  166.                     text = "电量不足...";  
  167.                 }  
  168.                 Rect.Width = iRectWidth * ((Percent + 5) > 100 ? 100 : Percent + 8) / 100;  
  169.             }  
  170.         }  
  171.         /// <summary>  
  172.         /// 电池形状X坐标  
  173.         /// </summary>  
  174.         public int Left  
  175.         {  
  176.             get { return iLeft; }  
  177.             set { iLeft = value; SetPolygon(); }  
  178.         }  
  179.         /// <summary>  
  180.         /// 电池形状Y坐标  
  181.         /// </summary>  
  182.         public int Top  
  183.         {  
  184.             get { return iTop; }  
  185.             set { iTop = value; SetPolygon(); }  
  186.         }  
  187.         /// <summary>  
  188.         /// 电池形状宽度  
  189.         /// </summary>  
  190.         public int Width  
  191.         {  
  192.             get { return iWidth; }  
  193.             set { iWidth = value; SetPolygon(); }  
  194.         }  
  195.         /// <summary>  
  196.         /// 电池形状高度  
  197.         /// </summary>  
  198.         public int Height  
  199.         {  
  200.             get { return iHeight; }  
  201.             set { iHeight = value; SetPolygon(); }  
  202.         }  
  203.         /// <summary>  
  204.         /// 电池电量百分比  
  205.         /// </summary>  
  206.         public int BatteryPercent  
  207.         {  
  208.             get { return Percent; }  
  209.         }  
  210.         /// <summary>  
  211.         /// 电池状态  
  212.         /// </summary>  
  213.         public Rectangle BatteryState  
  214.         {  
  215.             get  
  216.             {  
  217.                 GetBatteryState();  
  218.                 return Rect;  
  219.             }  
  220.         }  
  221.         /// <summary>  
  222.         /// 电池外边框  
  223.         /// </summary>  
  224.         public Point[] BatteryStateRect  
  225.         {  
  226.             get { return BatteryPolygon; }  
  227.             set { BatteryPolygon = value; }  
  228.         }  
  229.         /// <summary>  
  230.         /// 当前电池状态,有充电、充电不足、正常 三种状态  
  231.         /// </summary>  
  232.         public State Status  
  233.         {  
  234.             get { return AState; }  
  235.         }  
  236.         /// <summary>  
  237.         /// 电池显示的内容  
  238.         /// </summary>  
  239.         public string Text  
  240.         {  
  241.             get { return text; }  
  242.         }  
  243.     }  
  244. }  
using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Drawing;

namespace PDAeffect
{
public class BatteryControl
{
/// <summary>
/// 外边框
/// </summary>
private Point[] BatteryPolygon = new Point[8];
/// <summary>
/// 是否充电状态
/// </summary>
private State AState = State.Normal;
private int Percent = 0;
private int iLeft = 30 + 2, iTop = 178, iWidth = 20, iHeight = 10;
private int iRectWidth = 0;
private Rectangle Rect = new Rectangle();
private string text = "";
private SYSTEM_POWER_STATUS_EX status = new SYSTEM_POWER_STATUS_EX();
/// <summary>
/// 电池当前状态 Charge:充电中;UnderCharge:电量不足;Normal:电池正常使用.
/// </summary>
public enum State
{
/// <summary>
/// 充电中
/// </summary>
Charge,
/// <summary>
/// 充电不足
/// </summary>
UnderCharge,
/// <summary>
/// 正常状态
/// </summary>
Normal,
/// <summary>
/// 充电完成
/// </summary>
ChargeFinally
};
private class SYSTEM_POWER_STATUS_EX
{
public byte ACLineStatus = 0;
public byte BatteryFlag = 0;
public byte BatteryLifePercent = 0;
public byte Reserved1 = 0;
public uint BatteryLifeTime = 0;
public uint BatteryFullLifeTime = 0;
public byte Reserved2 = 0;
public byte BackupBatteryFlag = 0;
public byte BackupBatteryLifePercent = 0;
public byte Reserved3 = 0;
public uint BackupBatteryLifeTime = 0;
public uint BackupBatteryFullLifeTime = 0;
}
[DllImport("coredll")]
private static extern int GetSystemPowerStatusEx(SYSTEM_POWER_STATUS_EX lpSystemPowerStatus, bool fUpdate);
[DllImport("coredll")]
public static extern void SystemIdleTimerReset();
/// <summary>
/// 构造函数 在屏幕默认位置构建电池形状
/// </summary>
public void Battery()
{
SetPolygon();
}
/// <summary>
/// 构造函数 电池形状的X和Y坐标
/// </summary>
/// <param name="StartLeft">电池形状的X坐标</param>
/// <param name="StartTop">电池形状的Y坐标</param>
public void Battery(int StartLeft, int StartTop)
{
iLeft = StartLeft;
iTop = StartTop;
SetPolygon();
}
/// <summary>
/// 构造函数 根据X坐标、Y坐标、宽度、高度构造电池形状
/// </summary>
/// <param name="StartLeft">电池形状的X坐标</param>
/// <param name="StartTop">电池形状的Y坐标</param>
/// <param name="StartWidth">电池形状的宽度</param>
/// <param name="StartHeight">电池形状的高度</param>
public void Battery(int StartLeft, int StartTop, int StartWidth, int StartHeight)
{
iLeft = StartLeft;
iTop = StartTop;
iWidth = StartWidth;
iHeight = StartHeight;
SetPolygon();
}
/// <summary>
/// 设置电池形状
/// </summary>
void SetPolygon()
{
int Head = 2;
int HeightLowHalf = (Height - Head) / 2;
//外边框
BatteryPolygon[0].X = iLeft;
BatteryPolygon[0].Y = iTop;
BatteryPolygon[1].X = iLeft + iWidth;
BatteryPolygon[1].Y = iTop;
BatteryPolygon[2].X = iLeft + iWidth;
BatteryPolygon[2].Y = iTop + HeightLowHalf;
BatteryPolygon[3].X = iLeft + iWidth + Head;
BatteryPolygon[3].Y = iTop + HeightLowHalf;
BatteryPolygon[4].X = iLeft + iWidth + Head;
BatteryPolygon[4].Y = iTop + HeightLowHalf + Head;
BatteryPolygon[5].X = iLeft + iWidth;
BatteryPolygon[5].Y = iTop + HeightLowHalf + Head;
BatteryPolygon[6].X = iLeft + iWidth;
BatteryPolygon[6].Y = iTop + HeightLowHalf + Head + HeightLowHalf;
BatteryPolygon[7].X = iLeft;
BatteryPolygon[7].Y = iTop + HeightLowHalf + Head + HeightLowHalf;
//内矩形
Rect.X = BatteryPolygon[0].X + 2;
Rect.Y = BatteryPolygon[0].Y + 2;
Rect.Width = BatteryPolygon[6].X - BatteryPolygon[0].X - 3;
Rect.Height = BatteryPolygon[6].Y - BatteryPolygon[0].Y - 3;
iRectWidth = Rect.Width;
GetBatteryState();
}
/// <summary>
/// 获取电池状态
/// </summary>
public void GetBatteryState()
{
if (GetSystemPowerStatusEx(status, false) == 1)
{
if (status.ACLineStatus == 1)
{
//BatteryFlag = 128 充电完成
if(status.BatteryFlag == 128)
//if (status.BatteryLifePercent >= 100)
{
//status.BatteryLifePercent = 100; //.BatteryFullLifeTime
text = "充电完成...";
AState = State.ChargeFinally;
} //BatteryFlag = 8 正在充电
else
{
AState = State.Charge;
text = "充电中...";
}
}
else
{
//BatteryFlag = 1 正在使用电池
AState = State.Normal;
if (status.BatteryLifePercent > 100) status.BatteryLifePercent = 100;
text = status.BatteryLifePercent.ToString() + "%";
}
Percent = status.BatteryLifePercent;
if (Percent <= 20)
{
AState = State.UnderCharge;
text = "电量不足...";
}
Rect.Width = iRectWidth * ((Percent + 5) > 100 ? 100 : Percent + 8) / 100;
}
}
/// <summary>
/// 电池形状X坐标
/// </summary>
public int Left
{
get { return iLeft; }
set { iLeft = value; SetPolygon(); }
}
/// <summary>
/// 电池形状Y坐标
/// </summary>
public int Top
{
get { return iTop; }
set { iTop = value; SetPolygon(); }
}
/// <summary>
/// 电池形状宽度
/// </summary>
public int Width
{
get { return iWidth; }
set { iWidth = value; SetPolygon(); }
}
/// <summary>
/// 电池形状高度
/// </summary>
public int Height
{
get { return iHeight; }
set { iHeight = value; SetPolygon(); }
}
/// <summary>
/// 电池电量百分比
/// </summary>
public int BatteryPercent
{
get { return Percent; }
}
/// <summary>
/// 电池状态
/// </summary>
public Rectangle BatteryState
{
get
{
GetBatteryState();
return Rect;
}
}
/// <summary>
/// 电池外边框
/// </summary>
public Point[] BatteryStateRect
{
get { return BatteryPolygon; }
set { BatteryPolygon = value; }
}
/// <summary>
/// 当前电池状态,有充电、充电不足、正常 三种状态
/// </summary>
public State Status
{
get { return AState; }
}
/// <summary>
/// 电池显示的内容
/// </summary>
public string Text
{
get { return text; }
}
}
}

 

方法1:

[csharp] view plaincopyprint?
  1. WinCE获取电池充电状态  
  2.   
  3. SYSTEM_POWER_STATUS_EX2 powerState;  
  4.   
  5.     memset(&powerState, 0, sizeof(powerState));  
  6.   
  7.     DWORD dwResult = GetSystemPowerStatusEx2(&powerState, sizeof(powerState), TRUE);  
  8.   
  9.     if(dwResult != 0)  
  10.   
  11.     {  
  12.   
  13.         if(powerState.ACLineStatus == AC_LINE_ONLINE)  
  14.   
  15.         {  
  16.   
  17.             //MessageBox(_T("交流电"));  
  18.   
  19.             if(powerState.BatteryFlag != BATTERY_FLAG_HIGH)  
  20.   
  21.             {  
  22.   
  23.                 // 正在充电  
  24.   
  25.                 //SetPowerStepBmp(200);  
  26.   
  27.                 MessageBox(_T("充电..."));  
  28.   
  29.             }  
  30.   
  31.             else  
  32.   
  33.             {  
  34.   
  35.                 // 充电结束  
  36.   
  37.                 //SetPowerStepBmp(300);  
  38.   
  39.                 MessageBox(_T("充电结束..."));  
  40.   
  41.             }  
  42.   
  43.         }  
  44.   
  45.         else  
  46.   
  47.         {  
  48.   
  49.             MessageBox(_T("直流电"));  
  50.   
  51.             //INT pow = GetPowerStep((INT)powerState.BatteryLifePercent);  
  52.   
  53.             //SetPowerStepBmp((INT)powerState.BatteryLifePercent);  
  54.   
  55.         }  
  56.   
  57.     }     
WinCE获取电池充电状态

SYSTEM_POWER_STATUS_EX2 powerState;

memset(&powerState, 0, sizeof(powerState));

DWORD dwResult = GetSystemPowerStatusEx2(&powerState, sizeof(powerState), TRUE);

if(dwResult != 0)

{

if(powerState.ACLineStatus == AC_LINE_ONLINE)

{

//MessageBox(_T("交流电"));

if(powerState.BatteryFlag != BATTERY_FLAG_HIGH)

{

// 正在充电

//SetPowerStepBmp(200);

MessageBox(_T("充电..."));

}

else

{

// 充电结束

//SetPowerStepBmp(300);

MessageBox(_T("充电结束..."));

}

}

else

{

MessageBox(_T("直流电"));

//INT pow = GetPowerStep((INT)powerState.BatteryLifePercent);

//SetPowerStepBmp((INT)powerState.BatteryLifePercent);

}

}

效果图(见右上角):

WINCE 电池状态(C#)

     (部分桌面内容,受保护已抹掉)

 WINCE 电池状态(C#)