Search code examples
c#compact-frameworkembedded

Windows CE 6.0 backlight brightness control


I searched the internet and found a solution, how to change device brightness from C# code. It looks like:

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

    [DllImport("coredll.Dll")]
    private static extern IntPtr CreateEvent(IntPtr lpEventAttributes, bool bManualReset, bool bInitialState, string lpName);

    [DllImport("coredll.Dll")]
    [return: MarshalAs(UnmanagedType.Bool)]
    private static extern bool CloseHandle(IntPtr hObject);

    private static bool SetEvent(IntPtr hEvent)
    {
        return EventModify(hEvent, (int)EventFlags.SET);
    }

    private void SetBacklightValue(string name, int v)
    {
        RegistryKey key = Registry.CurrentUser.OpenSubKey(@"ControlPanel\Backlight", true);
        if (key != null)
        {
            key.SetValue(name, v);
            key.Close();
        }
    }

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

    private static void RaiseBackLightChangeEvent()
    {
        IntPtr hBackLightEvent = CreateEvent(IntPtr.Zero, false, false, "BackLightChangeEvent");
        if (hBackLightEvent != IntPtr.Zero)
        {
            bool result = SetEvent(hBackLightEvent);
            CloseHandle(hBackLightEvent);
        }

    }

The brightness value in registry is changed succesfully. And after I disconnect device from PC (or connect) the brightness changes too. But not at the moment, when actual value is set. I might be missing something (RaiseBackLightChangeEvent works fine, there are no errors). Mb I need to rise some other event? Or if not, how can I simulate device power state changing without actual changing it? Or how can I force system update values from the registry? Thanks for your help.


Solution

  • Backlight control is not standardized, it varies from one device to the other. In some devices changing the registry is enough, in other devices you need to fire a "named event" (you can use OpenNetcf for this), and even for those you still need to know the name of the event. For other devices there is not way of doing it. I guess your best choice would be to contact the manufacturer of your target device and ask them about the details on how they are dealing with this problem.