PDA背光和电源管理

PDA为了省电,会自动挂起,如果我们的程序需要长时间工作,就需要对其背光和电源进行管理。
控制代码如下:

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using Microsoft.Win32;

namespace Streamsea.CommModel
{
public class PowerEx
{
private const string POWER_TIMEOUT = @"System/CurrentControlSet/Control/Power/Timeouts";
private const string POWER_STATE_SUSPEND = @"System/CurrentControlSet/Control/Power/State/Suspend";

/// <summary>
/// 重设Windows CE用来监视用户操作的定时器
/// </summary>
[DllImport("CoreDll")]
public static extern void SystemIdleTimerReset();

/// <summary>
/// 设置电源的关闭时间,0为不关闭
/// </summary>
public int PowerOffTime
{
set
{
//系统的闲置关闭时间设置,对应 "设置->系统->电源->高级" 中的两个选择项。
RegistryKey rktimeout = Registry.LocalMachine.OpenSubKey(POWER_TIMEOUT, true);
if (rktimeout != null)
{
//外接电源的闲置关闭时间,为0时系统将不会挂起。
rktimeout.Setvalue("ACSuspendTimeout", value, RegistryvalueKind.DWord);
//电池电源的闲置关闭时间
rktimeout.Setvalue("BattSuspendTimeout", value, RegistryvalueKind.DWord);
rktimeout.Close();
}
//在设置闲置关闭时间为0时,如果将注册表做如下修改那么硬件上的挂起按钮将无效。
RegistryKey rksuspend = Registry.LocalMachine.OpenSubKey(POWER_STATE_SUSPEND, true);
if (rksuspend != null)
{
rksuspend.Setvalue("Default", value == 0 ? 0 : 3, RegistryvalueKind.DWord);
rksuspend.Setvalue("Flags", value == 0 ? 0 : 209152, RegistryvalueKind.DWord);
rksuspend.Close();
}
}
}
}
}
使用方法:
在程序启动时设置PowerOffTime属性的值,设为0时系统将不会自动挂起,但最好每隔几分钟调用一次SystemIdleTimerReset函数,因为我在我的Panda E88上设为0后会随机的发生一点击屏幕就挂起的情况。这样系统就不会自动挂起了。
退出系统时再设置为以前的值,如180

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;

namespace Streamsea.CommModel
{
public class ScreenEx
{
// GDI Escapes for ExtEscape()
private const uint QUERYESCSUPPORT = 8;

// The following are unique to CE
private const uint GETVFRAMEPHYSICAL = 6144;
private const uint GETVFRAMELEN = 6145;
private const uint DBGDRIVERSTAT = 6146;
private const uint SETPOWERMANAGEMENT = 6147;
private const uint GETPOWERMANAGEMENT = 6148;

private const int SHFS_SHOWSTARTICON = 0x0010;
private const int SHFS_HIDESTARTICON = 0x0020;

/// <summary>
/// 关闭屏幕背光
/// </summary>
public static void PowerOff()
{
IntPtr hdc = GetDC(IntPtr.Zero);

uint size = 12;
byte[] vpm = new byte[size];

//structure size
BitConverter.GetBytes(size).CopyTo(vpm, 0);
//dpms version
BitConverter.GetBytes(0x0001).CopyTo(vpm, 4);
//power state
BitConverter.GetBytes((uint)ScreenPowerState.ScreenPowerOff).CopyTo(vpm, 8);

ExtEscapeSet(hdc, SETPOWERMANAGEMENT, size, vpm, 0, IntPtr.Zero);
}

/// <summary>
/// 打开屏幕背光
/// </summary>
public static void PowerOn()
{
IntPtr hdc = GetDC(IntPtr.Zero);

uint size = 12;
byte[] vpm = new byte[size];

//structure size
BitConverter.GetBytes(size).CopyTo(vpm, 0);
//dpms version
BitConverter.GetBytes(0x0001).CopyTo(vpm, 4);
//power state
BitConverter.GetBytes((uint)ScreenPowerState.ScreenPowerOn).CopyTo(vpm, 8);

ExtEscapeSet(hdc, SETPOWERMANAGEMENT, size, vpm, 0, IntPtr.Zero);
}

[DllImport("coredll", EntryPoint = "ExtEscape")]
private static extern int ExtEscapeSet(
IntPtr hdc,
uint nEscape,
uint cbInput,
byte[] lpszInData,
int cbOutput,
IntPtr lpszOutData
);

[DllImport("coredll")]
private static extern IntPtr GetDC(IntPtr hwnd);
}

/// <summary>
/// 屏幕电源状态
/// </summary>
public enum ScreenPowerState : uint
{
/// <summary>
/// 打开状态
/// </summary>
ScreenPowerOn = 1,
/// <summary>
/// 标准状态
/// </summary>
ScreenPowerStandBy,
/// <summary>
/// 挂起状态
/// </summary>
ScreenPowerSuspend,
/// <summary>
/// 关闭状态
/// </summary>
ScreenPowerOff
}
}
如果让屏幕一直开着,是很耗电的,如果程序在运行时不需要观看屏幕,可以用上面这个类提供的方法关闭屏幕,要注意的是PowerOff只是关闭屏幕,如果点击屏幕上的按钮,点击操作还是会有效的。在这种关闭状态下,按硬件的挂起按钮会打开屏幕背光。 PDA为了省电,会自动挂起,如果我们的程序需要长时间工作,就需要对其背光和电源进行管理。
控制代码如下:

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using Microsoft.Win32;

namespace Streamsea.CommModel
{
public class PowerEx
{
private const string POWER_TIMEOUT = @"System/CurrentControlSet/Control/Power/Timeouts";
private const string POWER_STATE_SUSPEND = @"System/CurrentControlSet/Control/Power/State/Suspend";

/// <summary>
/// 重设Windows CE用来监视用户操作的定时器
/// </summary>
[DllImport("CoreDll")]
public static extern void SystemIdleTimerReset();

/// <summary>
/// 设置电源的关闭时间,0为不关闭
/// </summary>
public int PowerOffTime
{
set
{
//系统的闲置关闭时间设置,对应 "设置->系统->电源->高级" 中的两个选择项。
RegistryKey rktimeout = Registry.LocalMachine.OpenSubKey(POWER_TIMEOUT, true);
if (rktimeout != null)
{
//外接电源的闲置关闭时间,为0时系统将不会挂起。
rktimeout.Setvalue("ACSuspendTimeout", value, RegistryvalueKind.DWord);
//电池电源的闲置关闭时间
rktimeout.Setvalue("BattSuspendTimeout", value, RegistryvalueKind.DWord);
rktimeout.Close();
}
//在设置闲置关闭时间为0时,如果将注册表做如下修改那么硬件上的挂起按钮将无效。
RegistryKey rksuspend = Registry.LocalMachine.OpenSubKey(POWER_STATE_SUSPEND, true);
if (rksuspend != null)
{
rksuspend.Setvalue("Default", value == 0 ? 0 : 3, RegistryvalueKind.DWord);
rksuspend.Setvalue("Flags", value == 0 ? 0 : 209152, RegistryvalueKind.DWord);
rksuspend.Close();
}
}
}
}
}
使用方法:
在程序启动时设置PowerOffTime属性的值,设为0时系统将不会自动挂起,但最好每隔几分钟调用一次SystemIdleTimerReset函数,因为我在我的Panda E88上设为0后会随机的发生一点击屏幕就挂起的情况。这样系统就不会自动挂起了。
退出系统时再设置为以前的值,如180

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;

namespace Streamsea.CommModel
{
public class ScreenEx
{
// GDI Escapes for ExtEscape()
private const uint QUERYESCSUPPORT = 8;

// The following are unique to CE
private const uint GETVFRAMEPHYSICAL = 6144;
private const uint GETVFRAMELEN = 6145;
private const uint DBGDRIVERSTAT = 6146;
private const uint SETPOWERMANAGEMENT = 6147;
private const uint GETPOWERMANAGEMENT = 6148;

private const int SHFS_SHOWSTARTICON = 0x0010;
private const int SHFS_HIDESTARTICON = 0x0020;

/// <summary>
/// 关闭屏幕背光
/// </summary>
public static void PowerOff()
{
IntPtr hdc = GetDC(IntPtr.Zero);

uint size = 12;
byte[] vpm = new byte[size];

//structure size
BitConverter.GetBytes(size).CopyTo(vpm, 0);
//dpms version
BitConverter.GetBytes(0x0001).CopyTo(vpm, 4);
//power state
BitConverter.GetBytes((uint)ScreenPowerState.ScreenPowerOff).CopyTo(vpm, 8);

ExtEscapeSet(hdc, SETPOWERMANAGEMENT, size, vpm, 0, IntPtr.Zero);
}

/// <summary>
/// 打开屏幕背光
/// </summary>
public static void PowerOn()
{
IntPtr hdc = GetDC(IntPtr.Zero);

uint size = 12;
byte[] vpm = new byte[size];

//structure size
BitConverter.GetBytes(size).CopyTo(vpm, 0);
//dpms version
BitConverter.GetBytes(0x0001).CopyTo(vpm, 4);
//power state
BitConverter.GetBytes((uint)ScreenPowerState.ScreenPowerOn).CopyTo(vpm, 8);

ExtEscapeSet(hdc, SETPOWERMANAGEMENT, size, vpm, 0, IntPtr.Zero);
}

[DllImport("coredll", EntryPoint = "ExtEscape")]
private static extern int ExtEscapeSet(
IntPtr hdc,
uint nEscape,
uint cbInput,
byte[] lpszInData,
int cbOutput,
IntPtr lpszOutData
);

[DllImport("coredll")]
private static extern IntPtr GetDC(IntPtr hwnd);
}

/// <summary>
/// 屏幕电源状态
/// </summary>
public enum ScreenPowerState : uint
{
/// <summary>
/// 打开状态
/// </summary>
ScreenPowerOn = 1,
/// <summary>
/// 标准状态
/// </summary>
ScreenPowerStandBy,
/// <summary>
/// 挂起状态
/// </summary>
ScreenPowerSuspend,
/// <summary>
/// 关闭状态
/// </summary>
ScreenPowerOff
}
}
如果让屏幕一直开着,是很耗电的,如果程序在运行时不需要观看屏幕,可以用上面这个类提供的方法关闭屏幕,要注意的是PowerOff只是关闭屏幕,如果点击屏幕上的按钮,点击操作还是会有效的。在这种关闭状态下,按硬件的挂起按钮会打开屏幕背光。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值