WINCE 电池状态(C#)

WINCE 电池状态(C#)

分类:             电量              2013-04-18 12:08     397人阅读     评论(1)     收藏     举报    

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

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

  1. WinCE获取电池充电状态
  2. SYSTEM_POWER_STATUS_EX2 powerState;
  3. memset(&powerState, 0, sizeof(powerState));
  4. DWORD dwResult = GetSystemPowerStatusEx2(&powerState, sizeof(powerState), TRUE);
  5. if(dwResult != 0)
  6. {
  7. if(powerState.ACLineStatus == AC_LINE_ONLINE)
  8. {
  9. //MessageBox(_T("交流电"));
  10. if(powerState.BatteryFlag != BATTERY_FLAG_HIGH)
  11. {
  12. // 正在充电
  13. //SetPowerStepBmp(200);
  14. MessageBox(_T("充电..."));
  15. }
  16. else
  17. {
  18. // 充电结束
  19. //SetPowerStepBmp(300);
  20. MessageBox(_T("充电结束..."));
  21. }
  22. }
  23. else
  24. {
  25. MessageBox(_T("直流电"));
  26. //INT pow = GetPowerStep((INT)powerState.BatteryLifePercent);
  27. //SetPowerStepBmp((INT)powerState.BatteryLifePercent);
  28. }
  29. }
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#)

上一篇:过河问题--nyoj题目47


下一篇:Xilinx 常用模块汇总(verilog)【03】