private void SetPowerPlanButton_Click(object sender, EventArgs e)
        {
            Guid powerPlanGUID = new Guid(GUID_BALANCED);

            switch (powerPlanComboBox.SelectedItem)
            {
            case NAME_ULTIMATE_PERFORMANCE:
                if (ultimatePerformanceGUIDIndex == 2)
                {
                    powerPlanGUID = new Guid(GUID_ULTIMATE_PERFORMANCE_2);
                }
                else
                {
                    powerPlanGUID = new Guid(GUID_ULTIMATE_PERFORMANCE);
                }
                break;

            case NAME_MAX_PERFORMANCE:
                powerPlanGUID = new Guid(GUID_MAX_PERFORMANCE);
                break;

            case NAME_BALANCED:
                powerPlanGUID = new Guid(GUID_BALANCED);
                break;

            case NAME_POWER_SAVER:
                powerPlanGUID = new Guid(GUID_POWER_SAVER);
                break;
            }

            WinApiCalls.PowerSetActiveScheme(IntPtr.Zero, ref powerPlanGUID);

            setPowerPlanButton.Enabled = false;
        }
        public SystemPerformanceManagerForm(ModManagerForm form)
        {
            InitializeComponent();

            modManager = form;

            timer = new Timer()
            {
                Enabled  = true,
                Interval = 1000
            };
            timer.Tick += new EventHandler(TimerTick);

            // Use the same icon as executable
            Icon = Icon.ExtractAssociatedIcon(Application.ExecutablePath);

            TimerCaps caps = WinApiCalls.QueryTimerResolution();

            minimumTimerResolutionTextBox.Text = caps.PeriodMax / 10000.0 + " ms";
            maximumTimerResolutionTextBox.Text = caps.PeriodMin / 10000.0 + " ms";
            currentTimerResolutionTextBox.Text = caps.PeriodCurrent / 10000.0 + " ms";

            if (caps.PeriodCurrent / 10000.0 < 0.8)
            {
                modManager.IsTimerResolutionLowered = true;
            }

            REG_DOW_PATH = modManager.CurrentDir + "\\" + modManager.CurrentGameEXE;

            // We are checking for Compatibility settings in Registry
            using (RegistryKey key = Registry.CurrentUser.OpenSubKey(REG_COMPATIBILITY_PATH, false))
            {
                if (key != null)
                {
                    object valueObject = key.GetValue(REG_DOW_PATH);

                    if (valueObject != null)
                    {
                        string value = valueObject.ToString();

                        if (value.Contains(REG_VALUE_FULLSCREEN_OPTIMIZATIONS))
                        {
                            fullscreenOptimizationsCheckBox.Checked = true;
                        }
                        if (value.Contains(REG_VALUE_RUN_AS_ADMIN))
                        {
                            runAsAdministratorCheckBox.Checked = true;
                        }
                        if (value.Contains(REG_VALUE_HDPI_AWARE))
                        {
                            HDPIiScalingCheckBox.Checked = true;
                        }
                        if (value.Contains(REG_VALUE_COMPATIBILITY_WITH))
                        {
                            comatibilityModeCheckBox.Checked = true;
                        }
                    }
                }
            }

            // We are checking for Power Settings in Registry
            using (RegistryKey key = Registry.LocalMachine.OpenSubKey(REG_POWER_SCHEMES_PATH + "\\" + GUID_ULTIMATE_PERFORMANCE_2, false))
            {
                if (key != null)
                {
                    powerPlanComboBox.Items.Add(NAME_ULTIMATE_PERFORMANCE);
                    unlockUltimatePerformanceButton.Enabled = false;
                    ultimatePerformanceGUIDIndex            = 2;
                }
            }
            using (RegistryKey key = Registry.LocalMachine.OpenSubKey(REG_POWER_SCHEMES_PATH + "\\" + GUID_ULTIMATE_PERFORMANCE, false))
            {
                if (key != null && ultimatePerformanceGUIDIndex == 0)
                {
                    powerPlanComboBox.Items.Add(NAME_ULTIMATE_PERFORMANCE);
                    unlockUltimatePerformanceButton.Enabled = false;
                    ultimatePerformanceGUIDIndex            = 1;
                }
            }
            using (RegistryKey key = Registry.LocalMachine.OpenSubKey(REG_POWER_SCHEMES_PATH + "\\" + GUID_MAX_PERFORMANCE, false))
            {
                if (key != null)
                {
                    powerPlanComboBox.Items.Add(NAME_MAX_PERFORMANCE);
                }
            }
            using (RegistryKey key = Registry.LocalMachine.OpenSubKey(REG_POWER_SCHEMES_PATH + "\\" + GUID_BALANCED, false))
            {
                if (key != null)
                {
                    powerPlanComboBox.Items.Add(NAME_BALANCED);
                }
            }
            using (RegistryKey key = Registry.LocalMachine.OpenSubKey(REG_POWER_SCHEMES_PATH + "\\" + GUID_POWER_SAVER, false))
            {
                if (key != null)
                {
                    powerPlanComboBox.Items.Add(NAME_POWER_SAVER);
                }
            }
            using (RegistryKey key = Registry.LocalMachine.OpenSubKey(REG_POWER_SCHEMES_PATH, false))
            {
                if (key != null)
                {
                    object valueObject = key.GetValue("ActivePowerScheme");

                    if (valueObject != null)
                    {
                        string value = valueObject.ToString();

                        switch (value)
                        {
                        case GUID_ULTIMATE_PERFORMANCE:
                        case GUID_ULTIMATE_PERFORMANCE_2:
                            powerPlanComboBox.SelectedItem = NAME_ULTIMATE_PERFORMANCE;
                            break;

                        case GUID_MAX_PERFORMANCE:
                            powerPlanComboBox.SelectedItem = NAME_MAX_PERFORMANCE;
                            break;

                        case GUID_BALANCED:
                            powerPlanComboBox.SelectedItem = NAME_BALANCED;
                            break;

                        case GUID_POWER_SAVER:
                            powerPlanComboBox.SelectedItem = NAME_POWER_SAVER;
                            break;
                        }
                    }
                }
            }

            // We have to add those methods to the EventHandler here so we could avoid accidental firing of those methods after we would change the state of the CheckBox
            runAsAdministratorCheckBox.CheckedChanged      += new EventHandler(CheckBox_CheckedChanged);
            HDPIiScalingCheckBox.CheckedChanged            += new EventHandler(CheckBox_CheckedChanged);
            comatibilityModeCheckBox.CheckedChanged        += new EventHandler(CheckBox_CheckedChanged);
            fullscreenOptimizationsCheckBox.CheckedChanged += new EventHandler(CheckBox_CheckedChanged);

            powerPlanComboBox.SelectedIndexChanged += new EventHandler(PowerPlanComboBox_SelectedIndexChanged);
        }
 private void OpenDoWPropertiesButton_Click(object sender, EventArgs e)
 {
     WinApiCalls.ShowFileProperties(modManager.CurrentGameEXE);
 }
 private void DefaultTimerResolutionButton_Click(object sender, EventArgs e)
 {
     WinApiCalls.SetTimerResolution(0, false);
     modManager.IsTimerResolutionLowered = false;
 }
 private void SetTimerResolutionButton_Click(object sender, EventArgs e)
 {
     WinApiCalls.SetTimerResolution((uint)(0.5 * 10000));
     modManager.IsTimerResolutionLowered = true;
 }
        private void TimerTick(object sender, EventArgs e)
        {
            TimerCaps caps = WinApiCalls.QueryTimerResolution();

            currentTimerResolutionTextBox.Text = caps.PeriodCurrent / 10000.0 + " ms";
        }