private void UpdateUI(int brightnessValue)
        {
            this.Dispatcher.BeginInvoke((Action)(() =>
            {
                // 0 <= value <= 100

                ignoreValueChanged = true;
                this.BrightnessSlider.Value = brightnessValue;
                ignoreValueChanged = false;
                this.percentageLabel.Content = brightnessValue.ToString() + "%";
                DrawIcon.updateNotifyIcon(NotifyIcon, brightnessValue);
            }));
        }
        /// <summary>
        /// Change the monitor brightness when the slider value changes.
        /// </summary>
        private void Slider_ValueChanged(object sender, RoutedPropertyChangedEventArgs <double> e)
        {
            var newBrightness = (int)e.NewValue;

            // Change the brightness in a background thread to avoid UI blocking
            new Thread((data) =>
            {
                WmiFunctions.SetBrightnessLevel((int)newBrightness);
            }).Start();

            this.percentageLabel.Content = newBrightness.ToString() + "%";
            DrawIcon.updateNotifyIcon(NotifyIcon, newBrightness);
        }
        /// <summary>
        /// Creates notify icon and menu.
        /// </summary>
        private void CreateNotifyIcon()
        {
            System.Windows.Forms.NotifyIcon notifyicon;

            notifyicon = new System.Windows.Forms.NotifyIcon();
            var currentBrightness = WmiFunctions.GetBrightnessLevel();

            //System.Windows.Forms.MenuItem mnuPin = new System.Windows.Forms.MenuItem("Pin", new EventHandler(this.PinMenuEventHandler));
            System.Windows.Forms.MenuItem mnuMonitorOff  = new System.Windows.Forms.MenuItem("Power off display", new EventHandler(this.MonitorOffMenuEventHandler));
            System.Windows.Forms.MenuItem mnuScreenSaver = new System.Windows.Forms.MenuItem("Start screen saver", new EventHandler(this.StartScreenSaverMenuEventHandler));
            System.Windows.Forms.MenuItem mnuSleep       = new System.Windows.Forms.MenuItem("Enter sleep mode", new EventHandler(this.SleepMenuEventHandler));
            System.Windows.Forms.MenuItem mnuCaffeine    = new System.Windows.Forms.MenuItem("Caffeine", new EventHandler(this.CaffeineMenuEventHandler));
            mnuAutostart         = new System.Windows.Forms.MenuItem("Autostart", new EventHandler(this.AutostartMenuEventHandler));
            mnuAutostart.Checked = Autostart.CheckStartupFolderShortcutsExists();

            System.Windows.Forms.MenuItem mnuExit = new System.Windows.Forms.MenuItem("Close", new EventHandler(this.ExitMenuEventHandler));
            mnuLabel         = new System.Windows.Forms.MenuItem("");
            mnuLabel.Enabled = false; // greyed-out style label

            System.Windows.Forms.MenuItem[] menuitems = new System.Windows.Forms.MenuItem[]
            {
                mnuLabel, new System.Windows.Forms.MenuItem("-"), mnuMonitorOff, mnuScreenSaver, mnuSleep, new System.Windows.Forms.MenuItem("-"), mnuCaffeine, new System.Windows.Forms.MenuItem("-"), mnuAutostart, new System.Windows.Forms.MenuItem("-"), mnuExit
            };

            System.Windows.Forms.ContextMenu contextmenu = new System.Windows.Forms.ContextMenu(menuitems);
            contextmenu.Popup += this.ContextMenuPopup;

            notifyicon.ContextMenu = contextmenu;

            notifyicon.MouseClick       += this.NotifyIconClick;
            notifyicon.MouseDoubleClick += this.NotifyIconClick;

            notifyicon.Visible = true;

            DrawIcon.updateNotifyIcon(notifyicon, currentBrightness);

            this.NotifyIcon = notifyicon;
        }
        /// <summary>
        /// Update the slider due to a brightness changed event.
        /// </summary>
        private void EventWatcher_BrightnessChanged(object sender, EventWatcherAsync.BrightnessChangedEventArgs e)
        {
            NotifyIcon.Text = "Brightness " + e.newBrightness.ToString() + "%";

            // Only update the slider if the event was generated from an external source
            // - and NOT caused by BrightnessTray.
            // This helps keep the slider free of being jerky when the user is moving it.

            // e.g. ignore if the user is dragging the slider, or using the scroll wheel on the slider, or up/down keys on the slider.
            // needs to be IsMouseOver for entire form in case of scroll wheel.
            if (!IsMouseOver && !isKeyDown)
            {
                this.Dispatcher.BeginInvoke((Action)(() =>
                {
                    int value = int.Parse(e.newBrightness.ToString());

                    // 0 <= value <= 100

                    this.BrightnessSlider.Value = value;
                    this.percentageLabel.Content = value.ToString() + "%";
                    DrawIcon.updateNotifyIcon(NotifyIcon, value);
                }));
            }
        }
 private void RegistryWatcher_OnUpdateStatus(object sender, string e)
 {
     DrawIcon.updateNotifyIcon(NotifyIcon, WmiFunctions.GetBrightnessLevel());
 }