locked
如何用DeviceIoControl 设置 电脑背光亮度 RRS feed

  • 问题

  • 我有一段代码是获取屏幕亮度的

    internal class MonitorDevice
      {
        const uint LMEM_FIXED = 0x0000;
        const uint LMEM_ZEROINIT = 0x0040;
        const uint LPTR = (LMEM_FIXED | LMEM_ZEROINIT);
        internal const Int32 INVALID_HANDLE_VALUE = -1;
        internal const uint OPEN_EXISTING = 3;
        internal const uint GENERIC_READ = 0x80000000;
        internal const uint GENERIC_WRITE = 0x40000000;
        internal uint access = GENERIC_WRITE | GENERIC_READ;
        internal const uint METHOD_BUFFERED = 0;
        internal const uint FILE_WRITE_ACCESS = 0x0002;
        internal const uint FILE_DEVICE_VIDEO = 0x00000023;
        internal const uint FILE_ANY_ACCESS = 0;
    
        public int GetBrightness()
        {
          // This code is from the following link:
          // http://social.msdn.microsoft.com/Forums/en-US/netfxcompact/thread/06e93e3e-d620-495a-83c6-407f38d25d09
    
          string lpFileName = "\\\\.\\LCD"; // "BKL1:";
    
          IntPtr hDrv = CreateFile(lpFileName, access, 0, IntPtr.Zero, OPEN_EXISTING, 0, IntPtr.Zero);
    
          if (hDrv.ToInt32() == INVALID_HANDLE_VALUE)
            throw new Exception("Can't open Backlight Driver");
    
          uint IOCTL_BACKLIGHT_GET = CTL_CODE(FILE_DEVICE_VIDEO, 0x126 /*2048*/, METHOD_BUFFERED, FILE_ANY_ACCESS);
          uint IOCTL_BACKLIGHT_SET = CTL_CODE(FILE_DEVICE_VIDEO, 2049, METHOD_BUFFERED, FILE_ANY_ACCESS);
          uint IOCTL_BACKLIGHT_UP = CTL_CODE(FILE_DEVICE_VIDEO, 2050, METHOD_BUFFERED, FILE_ANY_ACCESS);
          uint IOCTL_BACKLIGHT_DOWN = CTL_CODE(FILE_DEVICE_VIDEO, 2051, METHOD_BUFFERED, FILE_ANY_ACCESS);
          uint IOCTL_BACKLIGHT_DEFAULT = CTL_CODE(FILE_DEVICE_VIDEO, 2052, METHOD_BUFFERED, FILE_ANY_ACCESS);
          uint IOCTL_BACKLIGHT_MAX = CTL_CODE(FILE_DEVICE_VIDEO, 2053, METHOD_BUFFERED, FILE_ANY_ACCESS);
          uint IOCTL_BACKLIGHT_ON = CTL_CODE(FILE_DEVICE_VIDEO, 2054, METHOD_BUFFERED, FILE_ANY_ACCESS);
          uint IOCTL_BACKLIGHT_OFF = CTL_CODE(FILE_DEVICE_VIDEO, 2055, METHOD_BUFFERED, FILE_ANY_ACCESS);
    
          //Get the backlight value
          IntPtr getMemory = new IntPtr();
    
          byte[] backlightGetValue = new byte[4]; //DWORD to hold 0-255 value
          getMemory = LocalAlloc(LPTR, (uint)backlightGetValue.Length);
    
          Marshal.Copy(backlightGetValue, 0, getMemory, (int)backlightGetValue.Length);
    
          uint getCnt = 0;
          int myGetResult = DeviceIoControl(hDrv, IOCTL_BACKLIGHT_GET, IntPtr.Zero, 0, getMemory, (uint)backlightGetValue.Length, ref getCnt, IntPtr.Zero);
    
          Marshal.Copy(getMemory, backlightGetValue, 0, (int)backlightGetValue.Length);
    
          if (getMemory != IntPtr.Zero)
            LocalFree(getMemory);
    
          byte ucDisplayPolicy = backlightGetValue[0];
          byte ucACBrightness = backlightGetValue[1];
          byte ucDCBrightness = backlightGetValue[2];
    
          if (ucDisplayPolicy == 0x00000001) // AC power
            return ucACBrightness;
          else if (ucDisplayPolicy == 0x00000002) // DC power
            return ucDCBrightness;
    
          return -1;
        }
        
        public uint CTL_CODE(uint DeviceType, uint Function, uint Method, uint Access)
        {
          return ((DeviceType << 16) | (Access << 14) | (Function << 2) | Method);
        }
    
    
        [DllImport("Kernel32.dll")]
        internal static extern IntPtr LocalAlloc(
        uint uFlags,
        uint uBytes);
    
    
        [DllImport("Kernel32.dll")]
        internal static extern IntPtr LocalFree(IntPtr hMem);
    
    
        [DllImport("Kernel32.dll", EntryPoint = "CreateFile", SetLastError = true)]
        private static extern IntPtr CreateFile(
        String lpFileName, UInt32 dwDesiredAccess, UInt32 dwShareMode,
        IntPtr lpSecurityAttributes, UInt32 dwCreationDisposition, UInt32 dwFlagsAndAttributes,
        IntPtr hTemplateFile);
    
    
        [DllImport("Kernel32.dll", SetLastError = true)]
        public static extern int DeviceIoControl(
        IntPtr hDevice,
        uint dwIoControlCode,
        IntPtr lpInBuffer,
        uint nInBufferSize,
        IntPtr lpOutBuffer,
        uint nOutBufferSize,
        ref uint lpBytesReturned,
        IntPtr lpOverlapped);
      }
    
    但是,我该怎么修改才能变成设置背光亮度呢。? Help...
    2011年4月1日 3:43