WINCE 电池状态(C#)
WINCE 电池状态(C#)
根据网上搜集资料,最后整理 分享下
- 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; }
 - }
 - }
 - }
 
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:
- 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获取电池充电状态
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#)的更多相关文章
- 几个有趣的WEB设备API    前端提高B格必备(一)——电池状态&震动api
		
受到同事启发,突然发现了几个有趣又实用的web api,没想到前端还有这么多有趣的东西可以玩~~简直过分. 1.电池状态API navigator.getBattery():这个api返回的是一个pr ...
 - 与众不同 windows phone (47) - 8.0 其它: 锁屏信息和锁屏背景, 电池状态, 多分辨率, 商店, 内置协议, 快速恢复
		
[源码下载] 与众不同 windows phone (47) - 8.0 其它: 锁屏信息和锁屏背景, 电池状态, 多分辨率, 商店, 内置协议, 快速恢复 作者:webabcd 介绍与众不同 win ...
 - IOS 特定于设备的开发:检查设备接近度和电池状态
		
UIDevice类提供了一些API,使你能够跟踪设备的特征,包括电池的状态和接近度传感器.他们二者都以通知的形式提供更新,可以订阅他们,以便在有重要的更新时通知你的应用程序. 1>启动和禁用接近 ...
 - Android - 电池状态
		
为了解决电池图标的问题,顺带看了看电池信息的获取方法 :自己写了一个小栗子,来验证一下效果 电池的信息,一般都在BatteryManager里面,信息是用广播发出的.我们更新信息需要一个广播接收器 注 ...
 - Android监听电池状态
		
监听电池状态只需要接收Intent.ACTION_BATTERY_CHANGED的广播即可,当电池状态发生变化时会发出广播. 1.运行状态如下图: (1)连接USB时的状态 (2)断开USB时的状态 ...
 - 生活实遇记-Kindle好久没用,屏幕一直处于电池状态,怎么解决?
		
2018-01-02 实遇记-Kindle好久没用,屏幕一直处于电池状态,怎么解决? 今天我翻腾出自己的kindle,好久没用了,屏幕一直是一个电池状态,充电头+线充了2个钟头,按电源键木有反应,也是 ...
 - android  获得电池状态
		
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools= ...
 - Android虚拟机电池状态设置
		
问题描述: 安装SDK后使用AVD配合APPIUM进行测试,此时虚拟机的电池状态为0%充电中:部分APP会对手机电池状态有要求,不符合要求时,无法安装或打开. 解决思路: 1.Android系统设置( ...
 - html5电池状态相关API
		
var battery = navigator.battery || navigator.webkitBattery || navigator.mozBattery || navigator.msBa ...
 
随机推荐
- 为什么使用Hystrix?
			
分布式服务弹性框架“Hystrix”实践与源码研究(一) 文章初衷 为了应对将来在线(特别是无线端)业务量的成倍增长,后端服务的分布式化程度需要不断提高,对于服务的延迟和容错管理将面临更大挑战,公 ...
 - python算法题
			
python几道简单的算法题 最近看了python的语法,但是总感觉不知道怎么使用它,还是先来敲敲一些简单的程序吧. 1.题目:有1.2.3.4个数字,能组成多少个互不相同且无重复数字的三位数?都 ...
 - 前端开发——移动bug整理
			
1.ios下jquery的delegate失效问题? 解决方案: $("body").delegate(...) 改为 $(".item").delegate( ...
 - JSR303 Bean Validation 技术规范特性概述
			
概述 Bean Validation 规范 Bean 是 Java Bean 的缩写,在 Java 分层架构的实际应用中,从表示层到持久化层,每一层都需要对 Java Bean 进行业务符合性验证,如 ...
 - 由一个LED闪烁问题发现的MTK的LED driver中存在的问题
			
今天依据最新的需求要对LED灯的提示闪烁频率进行改动,将之前默认的2000ms改为10000ms,可是改动之后没有产生预料中的效果,而是变成了常量,百思不得其解,最后还是read the fuckin ...
 - ios基金会-XCode温馨提示
			
(一个)代号规格pragma mark 1.定义 #pragma 开头的代码是一条编译器指令,是一个特定于程序或编译器的指令. 不一定适用于其他编译器或其他环境.假设编译器不能识别该指令.则会将其忽略 ...
 - 通过Web Api 和 Angular.js 构建单页面的web 程序
			
通过Web Api 和 Angular.js 构建单页面的web 程序 在传统的web 应用程序中,浏览器端通过向服务器端发送请求,然后服务器端根据这个请求发送HTML到浏览器,这个响应将会影响整个的 ...
 - [推荐]ORACLE PL/SQL编程之四:把游标说透(不怕做不到,只怕想不到)
			
原文:[推荐]ORACLE PL/SQL编程之四:把游标说透(不怕做不到,只怕想不到) [推荐]ORACLE PL/SQL编程之四: 把游标说透(不怕做不到,只怕想不到) 继上两篇:ORACLE PL ...
 - 客户端Webview重定向
			
今天在客户端的网页中写了句alert的代码,发现执行了两次,后来发现网页的地址写的是http://192.168.14.72/app 客户端Webview加载网页,对于不完全路径会重定向到完全路径,导 ...
 - Android学习小Demo(21)ListView的联动选择
			
在日常的App开发中,尤其是在开发生活服务的应用上,非常多时候,我们会须要联动地展现省市区的数据等,需求大概例如以下: 1)展现全部省份 2)当点击某省份的时候,在二级菜单上展现此省份以下所属的城市列 ...