C# WINCE 下调整屏幕亮度

有2种方法,一是直接调用API,二是修改注册表

直接调用API,需要设备厂家的手册,或者查看注册表的 HKEY_LOCAL_MACHINE\\Drivers\\BuiltIn\\BackLite 项

修改注册表,注册表的键值也是不同的,根据设备来定

废话不多说,直接上代码
注册表方式

引入

    using Microsoft.Win32;

    using System.Runtime.InteropServices;

        public static void SetBright(string strValue)
        {
            ChangeBackLight(strValue);
            ReloadBackLight();
        }

        public static void ChangeBackLight(string strValue)
        {
            try
            {
                // 最大 65535 最小 0
                // [HKEY_CURRENT_USER\ControlPanel\Backlight]
                // "BltBrightness"=dword:00013093
                RegistryKey hkcu = Registry.CurrentUser;
                RegistryKey software = hkcu.OpenSubKey("ControlPanel\\Backlight", true);
                software.SetValue("BltBrightness", strValue, RegistryValueKind.DWord);
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(ex.Message);
            }
        }

        public static bool ReloadBackLight()
        {
            // Pocket PC上面,背光亮度存在注册表的 HKEY_CURRENT_USER\BackLight\BrightNess
            // 或者HKEY_CURRENT_USER\BackLight\BacklightLuminanceLevel 项中,修改这个项的值,
            // 再生成一个BackLightChangeEvent事件就可以使系统改变背光。
            bool ret = false;
            IntPtr scanEvent = NativeWin.CreateEvent(IntPtr.Zero, false, false, "BackLightChangeEvent");
            if (scanEvent == null)
            {
                throw new Exception("CreateEvent失败");
            }
            else
            {
                NativeWin.EventModify(scanEvent, EventFlags.SET);
                NativeWin.CloseHandle(scanEvent);
                ret = true;
            }
            return ret;
        }

    partial class NativeWin
    {
        [DllImport("coredll.dll", SetLastError = true, CharSet = CharSet.Auto)]
        internal static extern IntPtr CreateEvent(IntPtr lpEventAttributes, [In, MarshalAs(UnmanagedType.Bool)] bool bManualReset, [In, MarshalAs(UnmanagedType.Bool)] bool bIntialState, [In, MarshalAs(UnmanagedType.BStr)] string lpName);

        [DllImport("coredll.dll", SetLastError = true, CharSet = CharSet.Auto)]
        [return: MarshalAs(UnmanagedType.Bool)]
        internal static extern bool CloseHandle(IntPtr hObject);

        [DllImport("coredll.dll", SetLastError = true)]
        [return: MarshalAs(UnmanagedType.Bool)]
        internal static extern bool EventModify(IntPtr hEvent, [In, MarshalAs(UnmanagedType.U4)] EventFlags dEvent);
    }

    enum EventFlags : int
    {
        PULSE = 1,
        RESET = 2,
        SET = 3
    }


在MOTO的设备中注册表的位置是

[HKEY_LOCAL_MACHINE\HARDWARE\DEVICEMAP\BACKLIGHT]
"BacklightIntensity"=dword:00000053

但是值改了,亮度没有调整

所以API方式:

        //MOTO
        [DllImport("DispAPI32.DLL")]
        internal static extern uint DISPLAY_SetBacklightIntensity(int lpValue);

        public static void SetBright(int lpValue)
        {
            uint ret = DISPLAY_SetBacklightIntensity(lpValue);
            if (ret != 0)
            {
                System.Diagnostics.Debug.WriteLine("调整MOTO亮度失败.");
            }
        }




  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值