1. 保持背光
public enum DevicePowerState : int
{
Unspecified = -1,
D0 = 0, // Full On: full power, full functionality
D1, // Low Power On: fully functional at low power/performance
D2, // Standby: partially powered with automatic wake
D3, // Sleep: partially powered with device initiated wake
D4, // Off: unpowered
}
[DllImport("CoreDll", SetLastError=true)]
static extern IntPtr SetPowerRequirement(string DeviceName, DevicePowerState State, uint dwDeviceFlags, string Name, ulong Reserved);
[DllImport("CoreDll", SetLastError = true)]
static extern int ReleasePowerRequirement(IntPtr hPowerReq);
示例:
//保持背光
hPower = SetPowerRequirement("BKL1:", DevicePowerState.D0, 1, null, 0);
//取消保持背光
if (hPower != null)
ReleasePowerRequirement(hPower);
2. 设置背光
修改注册表
//CurrentUser/ContorlPanel/BackLight/ACBrightness
//CurrentUser/ContorlPanel/BackLight/Brightness
生成事件生效
[DllImport("coredll.dll", SetLastError = true, CallingConvention = CallingConvention.Winapi, CharSet = CharSet.Auto)]
public 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, CallingConvention = CallingConvention.Winapi, CharSet = CharSet.Auto)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool CloseHandle(IntPtr hObject);
[DllImport("coredll.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool EventModify(IntPtr hEvent, [In, MarshalAs(UnmanagedType.U4)] int dEvent);
//...
IntPtr hBackLightEvent = CreateEvent(IntPtr.Zero , false, true, "BackLightChangeEvent");
if (hBackLightEvent != IntPtr.Zero )
{
EventModify(hBackLightEvent, 3);
CloseHandle(hBackLightEvent);
}
//
部分HTC机器,修改注册表无效,直接调用HTCdll(我的T3238 wm6.1测试通过)
[DllImport("HTCUtil.dll")]
private static extern int HTCUtilGetOnPowerBrightnessLevel(ref int pValue);
[DllImport("HTCUtil.dll")]
private static extern int HTCUtilGetOnBatteryBrightnessLevel(ref int pValue);
[DllImport("HTCUtil.dll")]
private static extern int HTCUtilSetOnPowerBrightnessLevel(ref int pValue);
[DllImport("HTCUtil.dll")]
private static extern int HTCUtilSetOnBatteryBrightnessLevel(ref int pValue);
[DllImport("HTCUtil.dll")]
private static extern int HTCUtilGetBacklightMinBrightness(ref int pValue);
[DllImport("HTCUtil.dll")]
private static extern int HTCUtilGetBacklightMaxBrightness(ref int pValue);
3.禁止系统待机
[DllImport("CoreDll")]
public static extern void SystemIdleTimerReset();
在合适的时间内,调用SystemIdleTimerReset() 清除计时器,使系统不会进入待机状态.