Widow – Rev 21

Subversion Repositories:
Rev:
using System;
using System.Reflection;
using System.Windows.Forms;
using Microsoft.Win32;

namespace Widow
{
    public static class LaunchOnBoot
    {
        #region Public Methods

        /// <summary>
        ///     Enable double buffering for an arbitrary control.
        /// </summary>
        /// <param name="control">the control to enable double buffering for</param>
        /// <returns>true on success</returns>
        /// <remarks>Do not enable double buffering on RDP: https://devblogs.microsoft.com/oldnewthing/20060103-12/?p=32793</remarks>
        public static bool SetDoubleBuffered(this Control control)
        {
            if (SystemInformation.TerminalServerSession)
            {
                return false;
            }

            var dgvType = control.GetType();
            var pi = dgvType.GetProperty("DoubleBuffered",
                BindingFlags.Instance | BindingFlags.NonPublic);
            if (pi == null)
            {
                return false;
            }

            pi.SetValue(control, true, null);

            return true;
        }

        public static void InvokeIfRequired<T>(this T control, Action<T> action) where T : Control
        {
            if (control.InvokeRequired)
            {
                control.BeginInvoke((MethodInvoker) delegate { action(control); });
                return;
            }

            action(control);
        }

        public static bool Set(bool enable)
        {
            using (var key = Registry.CurrentUser.OpenSubKey
                ("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true))
            {
                if (key == null)
                {
                    return false;
                }

                switch (enable)
                {
                    case true:
                        key.SetValue(Constants.AssemblyName, Assembly.GetEntryAssembly().Location);
                        break;
                    default:
                        key.DeleteValue(Constants.AssemblyName, false);
                        break;
                }
            }

            return true;
        }

        public static bool Get()
        {
            using (var key = Registry.CurrentUser.OpenSubKey
                ("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true))
            {
                return key?.GetValue(Constants.AssemblyName) != null;
            }
        }

        #endregion
    }
}

Generated by GNU Enscript 1.6.5.90.