首 页文章中心下载中心繁體中文
设为首页
加入收藏
联系我们
您当前的位置:开源盛世-源代码下载网 -> 文章中心 -> PDA 专区 -> 文章内容 退出登录 用户管理
栏目导航
· VC++专区 · V B 专区
· GIS 专区 · PDA 专区
· 其他编程 · 网站开发类
· 数据库类 · 软件应用
· 网络安全 · 论文专区
· 综合资讯
热门文章
· Tab Control控件使用...
· 学生档案管理系统
· [图文] 排列组合公式
· UTF-8与GB2312之间的...
· DirectShow下载安装...
· Virtual PC 在PAE模...
· Windows2000终端服务...
· MapInfo上的GIS系统...
· Mapbasic参考手册索...
· MapX应用开发中文讲...
相关文章
控制PDA的背光和电源
作者:不详  来源:vscodes.com整理  发布时间:2007-8-23 15:32:38  发布人:Polaris

减小字体 增大字体

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只是关闭屏幕,如果点击屏幕上的按钮,点击操作还是会有效的。在这种关闭状态下,按硬件的挂起按钮会打开屏幕背光。
我做了一下测试,我的E88 待机时间大概为2天,开着屏幕时可用5个小时,关闭屏幕可用14个小时,而且还是每隔1分钟就通过GPRS发送数据。所以还是很有用的。

End of《控制PDA的背光和电源》

[] [返回上一页] [打 印] [收 藏]
 
∷相关“控制PDA的背光和电源”文章评论∷
(评论内容只代表网友观点,与本站立场无关!) [更多评论...]
关于本站 - 网站帮助 - 广告合作 - 下载声明 - 友情连接 - 网站地图 网站目录 鄂ICP备06007162
开源盛世 版权所有Copyright © 2003-2005 VSCodes.Com. All Rights Reserved.