Projects

Find all our projects in development below.
All source code is GNU General Public License (GPL)

Nvidia Control Panel Adjust

Browsing frmMain.cs (31.68 KB)

using System;
using System.Collections;
using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Xml;

namespace NvCplAdjust
{
    public partial class frmMain : Form
    {
        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        private static extern bool AppendMenu(IntPtr hMenu, uint wFlags, uint uIDNewItem, string lpNewItem);

        [DllImport("user32.dll")]
        private static extern uint CheckMenuItem(IntPtr hmenu, uint uIDCheckItem, uint uCheck);

        [DllImport("user32.dll")]
        private static extern bool DrawMenuBar(IntPtr hWnd);

        [DllImport("user32.dll")]
        private static extern IntPtr GetSystemMenu(IntPtr hWnd, bool bRevert);

        private const Int32 MF_STRING = 0x0;
        private const Int32 MF_SEPARATOR = 0x800;
        private const Int32 MF_CHECKED = 0x8;
        private const Int32 MF_UNCHECKED = 0x0;
        private const Int32 WM_SYSCOMMAND = 0x112;

        private const int mnuAlwaysOnTop = 5000;
        private const int mnuMinimalView = 5001;

        [DllImport("user32.dll", SetLastError = true)]
        private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

        [DllImport("user32.dll", SetLastError = true)]
        private static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);

        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        private static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, IntPtr lParam);

        private const uint TBM_GETPOS = 0x0400;
        private const uint TBM_SETPOSNOTIFY = 0x0422;

        [DllImport("user32.dll", SetLastError = true)]
        [return: MarshalAs(UnmanagedType.Bool)]
        private static extern bool RegisterHotKey(IntPtr hWnd, int id, uint fsModifiers, uint vk);

        [DllImport("user32.dll", SetLastError = true)]
        [return: MarshalAs(UnmanagedType.Bool)]
        private static extern bool UnregisterHotKey(IntPtr hWnd, int id);

        [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
        private static extern ushort GlobalAddAtom(string lpString);

        [DllImport("kernel32.dll", SetLastError = true, ExactSpelling = true)]
        private static extern ushort GlobalDeleteAtom(ushort nAtom);

        private const int MOD_ALT = 0x1;
        private const int MOD_CONTROL = 0x2;
        private const int MOD_SHIFT = 0x4;
        private const int WM_HOTKEY = 0x312;

        private int keyId_Profile1 = 0;
        private int keyId_Profile2 = 0;
        private int keyId_Profile3 = 0;
        private int keyId_Profile4 = 0;

        private const string XML_FILENAME = "NvCplAdjust_cfg.xml";

        private const String CMDLINEARG_PROFILE = "/profile";
        private const String CMDLINEARG_SILENT = "/silent";
        private string[] appArgs;

        private bool silent = false;
        private bool alwaysOnTop = false;
        private bool minimalView = false;

        private int defProfile = 0;
        private Hashtable profiles = new Hashtable();

        private IntPtr hwnd_brightness = IntPtr.Zero;
        private IntPtr hwnd_contrast = IntPtr.Zero;
        private IntPtr hwnd_gamma = IntPtr.Zero;

        private int val_brightness = 0;
        private int val_contrast = 0;
        private int val_gamma = 0;

        private Process procNvCpl = null;

        private const int PROFILE_BRIGHTNESS = 0;
        private const int PROFILE_CONTRAST = 1;
        private const int PROFILE_GAMMA = 2;
        private const int PROFILE_HOTKEY = 3;
        private const int PROFILE_HOTKEYMOD = 4;

        private const String EXEPATH_64 = "C:\\Program Files\\NVIDIA Corporation\\Control Panel Client\\nvcplui.exe";
        private const String EXEPATH_32 = "C:\\Program Files (x86)\\NVIDIA Corporation\\Control Panel Client\\nvcplui.exe";

        public frmMain(string[] args)
        {
            InitializeComponent();

            appArgs = args;

            // add the system menu items

            IntPtr sysMenuHwnd = GetSystemMenu(this.Handle, false);

            AppendMenu(sysMenuHwnd, MF_SEPARATOR, 0, null);
            AppendMenu(sysMenuHwnd, MF_STRING, mnuAlwaysOnTop, "Always On Top");
            AppendMenu(sysMenuHwnd, MF_STRING, mnuMinimalView, "Minimal View");
        }

        protected override void WndProc(ref Message m)
        {
            base.WndProc(ref m);

            // check for a hotkey press or system menu click

            if (m.Msg == WM_HOTKEY)
            {
                if ((int)m.WParam == keyId_Profile1)
                {
                    radPro1.Checked = true;
                }
                else if ((int)m.WParam == keyId_Profile2)
                {
                    radPro2.Checked = true;
                }
                else if ((int)m.WParam == keyId_Profile3)
                {
                    radPro3.Checked = true;
                }
                else if ((int)m.WParam == keyId_Profile4)
                {
                    radPro4.Checked = true;
                }
            }
            else if (m.Msg == WM_SYSCOMMAND)
            {
                if ((int)m.WParam == mnuAlwaysOnTop)
                {
                    this.TopMost = !this.TopMost;

                    if (this.TopMost)
                    {
                        CheckMenuItem(GetSystemMenu(m.HWnd, false), mnuAlwaysOnTop, MF_CHECKED);
                    }
                    else
                    {
                        CheckMenuItem(GetSystemMenu(m.HWnd, false), mnuAlwaysOnTop, MF_UNCHECKED);
                    }

                    DrawMenuBar(m.HWnd);
                }
                else if ((int)m.WParam == mnuMinimalView)
                {
                    toggleMinimalView();
                }
            }
        }

        private void toggleMinimalView()
        {
            minimalView = !minimalView;

            if (minimalView)
            {
                grpOptions.Height = 48;
                this.Height = 104;

                CheckMenuItem(GetSystemMenu(this.Handle, false), mnuMinimalView, MF_CHECKED);
            }
            else
            {
                grpOptions.Height = 320;
                this.Height = 376;

                CheckMenuItem(GetSystemMenu(this.Handle, false), mnuMinimalView, MF_UNCHECKED);
            }

            DrawMenuBar(this.Handle);
            pnlLoading.Size = this.ClientSize;
        }

        private void registerHotkeys()
        {
            if (keyId_Profile1 != 0)
            {
                UnregisterHotKey(this.Handle, keyId_Profile1);
                GlobalDeleteAtom((ushort)keyId_Profile1);
            }

            int[] profile = (int[])profiles["1"];

            if (profile[PROFILE_HOTKEY] != 0)
            {
                keyId_Profile1 = (int)GlobalAddAtom("Profile1 Hotkey");
                int hotkeymod = 0;

                if (((Keys)profile[PROFILE_HOTKEYMOD] & Keys.Alt) == Keys.Alt)
                {
                    hotkeymod |= MOD_ALT;
                }
                if (((Keys)profile[PROFILE_HOTKEYMOD] & Keys.Control) == Keys.Control)
                {
                    hotkeymod |= MOD_CONTROL;
                }
                if (((Keys)profile[PROFILE_HOTKEYMOD] & Keys.Shift) == Keys.Shift)
                {
                    hotkeymod |= MOD_SHIFT;
                }

                RegisterHotKey(this.Handle, keyId_Profile1, (uint)hotkeymod, (uint)profile[PROFILE_HOTKEY]);
            }

            if (keyId_Profile2 != 0)
            {
                UnregisterHotKey(this.Handle, keyId_Profile2);
                GlobalDeleteAtom((ushort)keyId_Profile2);
            }

            profile = (int[])profiles["2"];

            if (profile[PROFILE_HOTKEY] != 0)
            {
                keyId_Profile2 = (int)GlobalAddAtom("Profile2 Hotkey");
                int hotkeymod = 0;

                if (((Keys)profile[PROFILE_HOTKEYMOD] & Keys.Alt) == Keys.Alt)
                {
                    hotkeymod |= MOD_ALT;
                }
                if (((Keys)profile[PROFILE_HOTKEYMOD] & Keys.Control) == Keys.Control)
                {
                    hotkeymod |= MOD_CONTROL;
                }
                if (((Keys)profile[PROFILE_HOTKEYMOD] & Keys.Shift) == Keys.Shift)
                {
                    hotkeymod |= MOD_SHIFT;
                }

                RegisterHotKey(this.Handle, keyId_Profile2, (uint)hotkeymod, (uint)profile[PROFILE_HOTKEY]);
            }

            if (keyId_Profile3 != 0)
            {
                UnregisterHotKey(this.Handle, keyId_Profile3);
                GlobalDeleteAtom((ushort)keyId_Profile3);
            }

            profile = (int[])profiles["3"];

            if (profile[PROFILE_HOTKEY] != 0)
            {
                keyId_Profile3 = (int)GlobalAddAtom("Profile3 Hotkey");
                int hotkeymod = 0;

                if (((Keys)profile[PROFILE_HOTKEYMOD] & Keys.Alt) == Keys.Alt)
                {
                    hotkeymod |= MOD_ALT;
                }
                if (((Keys)profile[PROFILE_HOTKEYMOD] & Keys.Control) == Keys.Control)
                {
                    hotkeymod |= MOD_CONTROL;
                }
                if (((Keys)profile[PROFILE_HOTKEYMOD] & Keys.Shift) == Keys.Shift)
                {
                    hotkeymod |= MOD_SHIFT;
                }

                RegisterHotKey(this.Handle, keyId_Profile3, (uint)hotkeymod, (uint)profile[PROFILE_HOTKEY]);
            }

            if (keyId_Profile4 != 0)
            {
                UnregisterHotKey(this.Handle, keyId_Profile4);
                GlobalDeleteAtom((ushort)keyId_Profile4);
            }

            profile = (int[])profiles["4"];

            if (profile[PROFILE_HOTKEY] != 0)
            {
                keyId_Profile4 = (int)GlobalAddAtom("Profile4 Hotkey");
                int hotkeymod = 0;

                if (((Keys)profile[PROFILE_HOTKEYMOD] & Keys.Alt) == Keys.Alt)
                {
                    hotkeymod |= MOD_ALT;
                }
                if (((Keys)profile[PROFILE_HOTKEYMOD] & Keys.Control) == Keys.Control)
                {
                    hotkeymod |= MOD_CONTROL;
                }
                if (((Keys)profile[PROFILE_HOTKEYMOD] & Keys.Shift) == Keys.Shift)
                {
                    hotkeymod |= MOD_SHIFT;
                }

                RegisterHotKey(this.Handle, keyId_Profile4, (uint)hotkeymod, (uint)profile[PROFILE_HOTKEY]);
            }
        }

        private void unregisterHotkeys()
        {
            if (keyId_Profile1 != 0)
            {
                UnregisterHotKey(this.Handle, keyId_Profile1);
                GlobalDeleteAtom((ushort)keyId_Profile1);
            }

            if (keyId_Profile2 != 0)
            {
                UnregisterHotKey(this.Handle, keyId_Profile2);
                GlobalDeleteAtom((ushort)keyId_Profile2);
            }

            if (keyId_Profile3 != 0)
            {
                UnregisterHotKey(this.Handle, keyId_Profile3);
                GlobalDeleteAtom((ushort)keyId_Profile3);
            }

            if (keyId_Profile4 != 0)
            {
                UnregisterHotKey(this.Handle, keyId_Profile4);
                GlobalDeleteAtom((ushort)keyId_Profile4);
            }

            keyId_Profile1 = 0;
            keyId_Profile2 = 0;
            keyId_Profile3 = 0;
            keyId_Profile4 = 0;
        }

        private void frmMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            unregisterHotkeys();

            // save the options to xml

            try
            {
                XmlDocument xmlDoc = new XmlDocument();

                XmlNode parentNode = xmlDoc.AppendChild(xmlDoc.CreateElement("NvCplAdjust"));
                if (parentNode != null)
                {
                    XmlAttribute thisAttribute = xmlDoc.CreateAttribute("version");
                    thisAttribute.Value = Application.ProductVersion;

                    parentNode.Attributes.Append(thisAttribute);

                    XmlNode thisNode = parentNode.AppendChild(xmlDoc.CreateElement("options"));
                    if (thisNode != null)
                    {
                        thisAttribute = xmlDoc.CreateAttribute("profile");
                        thisAttribute.Value = defProfile.ToString();

                        thisNode.Attributes.Append(thisAttribute);

                        thisAttribute = xmlDoc.CreateAttribute("alwaysontop");
                        thisAttribute.Value = this.TopMost.ToString();

                        thisNode.Attributes.Append(thisAttribute);

                        thisAttribute = xmlDoc.CreateAttribute("minimalview");
                        thisAttribute.Value = minimalView.ToString();

                        thisNode.Attributes.Append(thisAttribute);

                        thisAttribute = xmlDoc.CreateAttribute("locX");
                        thisAttribute.Value = this.Bounds.Left.ToString();

                        thisNode.Attributes.Append(thisAttribute);

                        thisAttribute = xmlDoc.CreateAttribute("locY");
                        thisAttribute.Value = this.Bounds.Top.ToString();

                        thisNode.Attributes.Append(thisAttribute);
                    }
                    else
                    {
                        throw new Exception("Unable to create the 'options' tag in xml.");
                    }

                    thisNode = parentNode.AppendChild(xmlDoc.CreateElement("profiles"));
                    if (thisNode != null)
                    {
                        for (int i = 0; i < profiles.Count; i++)
                        {
                            int[] profile = (int[])profiles[(i + 1).ToString()];

                            XmlAttribute attribID = xmlDoc.CreateAttribute("id");
                            attribID.Value = (i + 1).ToString();

                            XmlAttribute attribBrightness = xmlDoc.CreateAttribute("brightness");
                            attribBrightness.Value = profile[PROFILE_BRIGHTNESS].ToString();
                                
                            XmlAttribute attribContrast = xmlDoc.CreateAttribute("contrast");
                            attribContrast.Value = profile[PROFILE_CONTRAST].ToString();

                            XmlAttribute attribGamma = xmlDoc.CreateAttribute("gamma");
                            attribGamma.Value = profile[PROFILE_GAMMA].ToString();

                            XmlAttribute attribHotkey = xmlDoc.CreateAttribute("hotkey");
                            attribHotkey.Value = profile[PROFILE_HOTKEY].ToString();

                            XmlAttribute attribHotkeymod = xmlDoc.CreateAttribute("hotkeymod");
                            attribHotkeymod.Value = profile[PROFILE_HOTKEYMOD].ToString();

                            XmlNode childNode = thisNode.AppendChild(xmlDoc.CreateElement("profile"));
                            if (childNode != null)
                            {
                                childNode.Attributes.Append(attribID);
                                childNode.Attributes.Append(attribBrightness);
                                childNode.Attributes.Append(attribContrast);
                                childNode.Attributes.Append(attribGamma);
                                childNode.Attributes.Append(attribHotkey);
                                childNode.Attributes.Append(attribHotkeymod);
                            }
                        }
                    }
                    else
                    {
                        throw new Exception("Unable to create the 'profiles' tag in xml.");
                    }
                }
                else
                {
                    throw new Exception("Unable to create the 'NvCplAdjust' tag in xml.");
                }

                xmlDoc.Save(XML_FILENAME);
            }
            catch
            {
            }

            if (procNvCpl != null)
            {
                try
                {
                    procNvCpl.Kill();
                }
                catch
                {
                }
            }
        }

        private void frmMain_Load(object sender, EventArgs e)
        {
            profiles.Add("1", new int[] { 0, 0, 0, 0, 0 });
            profiles.Add("2", new int[] { 0, 0, 0, 0, 0 });
            profiles.Add("3", new int[] { 0, 0, 0, 0, 0 });
            profiles.Add("4", new int[] { 0, 0, 0, 0, 0 });

            // load the options from xml

            try
            {
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(XML_FILENAME);

                XmlNode parentNode = xmlDoc.SelectSingleNode("NvCplAdjust");
                if (parentNode != null)
                {
                    XmlNode thisNode = parentNode.SelectSingleNode("options");
                    if (thisNode != null)
                    {
                        if (thisNode.Attributes.GetNamedItem("profile") != null)
                        {
                            defProfile = int.Parse(thisNode.Attributes.GetNamedItem("profile").Value);
                        }

                        if (thisNode.Attributes.GetNamedItem("alwaysontop") != null)
                        {
                            alwaysOnTop = bool.Parse(thisNode.Attributes.GetNamedItem("alwaysontop").Value);
                        }

                        if (thisNode.Attributes.GetNamedItem("minimalview") != null)
                        {
                            if (bool.Parse(thisNode.Attributes.GetNamedItem("minimalview").Value))
                            {
                                toggleMinimalView();
                            }
                        }

                        if (thisNode.Attributes.GetNamedItem("locX") != null)
                        {
                            this.Left = int.Parse(thisNode.Attributes.GetNamedItem("locX").Value);
                        }

                        if (thisNode.Attributes.GetNamedItem("locY") != null)
                        {
                            this.Top = int.Parse(thisNode.Attributes.GetNamedItem("locY").Value);
                        }
                    }

                    thisNode = parentNode.SelectSingleNode("profiles");
                    if (thisNode != null)
                    {
                        foreach (XmlNode thisEntry in thisNode.ChildNodes)
                        {
                            if (thisEntry.Attributes.GetNamedItem("id") != null)
                            {
                                int id = int.Parse(thisEntry.Attributes.GetNamedItem("id").Value);

                                if (profiles.ContainsKey(id.ToString()) &&
                                    thisEntry.Attributes.GetNamedItem("brightness") != null &&
                                    thisEntry.Attributes.GetNamedItem("contrast") != null &&
                                    thisEntry.Attributes.GetNamedItem("gamma") != null &&
                                    thisEntry.Attributes.GetNamedItem("hotkey") != null &&
                                    thisEntry.Attributes.GetNamedItem("hotkeymod") != null)
                                {
                                    int brightness = int.Parse(thisEntry.Attributes.GetNamedItem("brightness").Value);
                                    int contrast = int.Parse(thisEntry.Attributes.GetNamedItem("contrast").Value);
                                    int gamma = int.Parse(thisEntry.Attributes.GetNamedItem("gamma").Value);
                                    int hotkey = int.Parse(thisEntry.Attributes.GetNamedItem("hotkey").Value);
                                    int hotkeymod = int.Parse(thisEntry.Attributes.GetNamedItem("hotkeymod").Value);

                                    if (brightness < trkBrightness.Minimum || brightness > trkBrightness.Maximum) brightness = 0;
                                    if (contrast < trkContrast.Minimum || contrast > trkContrast.Maximum) contrast = 0;
                                    if (gamma < trkGamma.Minimum || gamma > trkGamma.Maximum) gamma = 0;

                                    profiles[id.ToString()] = new int[] { brightness, contrast, gamma, hotkey, hotkeymod };
                                }
                            }
                        }
                    }
                }
                else 
                {
                    throw new Exception("Missing the 'NvCplAdjust' tag in xml.");
                }
            }
            catch
            {
            }

            // check for a cmdline parameter
            for (int i = 0; i < appArgs.Length; i++)
            {
                if (appArgs[i] == CMDLINEARG_PROFILE && i < appArgs.Length - 1)
                {
                    // use the specified profile as default
                    if (int.TryParse(appArgs[i + 1], out defProfile))
                    {
                        if (defProfile < 1 || defProfile > 4) defProfile = 1;
                    }
                }
                else if (appArgs[i] == CMDLINEARG_SILENT)
                {
                    silent = true;
                }
            }
        }

        private void frmMain_Shown(object sender, EventArgs e)
        {
            if (alwaysOnTop)
            {
                CheckMenuItem(GetSystemMenu(this.Handle, false), mnuAlwaysOnTop, MF_CHECKED);
                DrawMenuBar(this.Handle);

                this.TopMost = true;
            }

            //grpOptions.Enabled = true;
            //pnlLoading.Visible = false;
            //radPro1.Checked = true;

            // check for nvidia control panel and run if closed

            IntPtr wnd = FindWindow(null, "NVIDIA Control Panel");
            if (wnd == IntPtr.Zero)
            {
                try
                {
                    String exePath = "";

                    if (File.Exists(EXEPATH_64))
                    {
                        exePath = EXEPATH_64;
                    }
                    else if (File.Exists(EXEPATH_32))
                    {
                        exePath = EXEPATH_32;
                    }

                    if (exePath != "")
                    {
                        ProcessStartInfo pInfo = new ProcessStartInfo(exePath);
                        pInfo.WindowStyle = ProcessWindowStyle.Hidden;

                        procNvCpl = Process.Start(pInfo);
                        tmrLoad.Interval = 3000;
                    }
                    else
                    {
                        throw new Exception("Unable to locate nvcplui.exe.");
                    }
                }
                catch (Exception ex)
                {
                    lblLoading.Text = "Error: " + ex.Message;
                    tmrLoad.Stop();
                }
            }
        }

        private void profileChange(int[] profile)
        {
            if (profile[PROFILE_BRIGHTNESS] < trkBrightness.Minimum || profile[PROFILE_BRIGHTNESS] > trkBrightness.Maximum)
                profile[PROFILE_BRIGHTNESS] = val_brightness;

            if (profile[PROFILE_BRIGHTNESS] >= trkBrightness.Minimum && profile[PROFILE_BRIGHTNESS] <= trkBrightness.Maximum)
            {
                trkBrightness.Value = profile[PROFILE_BRIGHTNESS];
                trkBrightness_Scroll(null, null);
            }

            if (profile[PROFILE_CONTRAST] < trkContrast.Minimum || profile[PROFILE_CONTRAST] > trkContrast.Maximum)
                profile[PROFILE_CONTRAST] = val_contrast;

            if (profile[PROFILE_CONTRAST] >= trkContrast.Minimum && profile[PROFILE_CONTRAST] <= trkContrast.Maximum)
            {
                trkContrast.Value = profile[PROFILE_CONTRAST];
                trkContrast_Scroll(null, null);
            }

            if (profile[PROFILE_GAMMA] < trkGamma.Minimum || profile[PROFILE_GAMMA] > trkGamma.Maximum)
                profile[PROFILE_GAMMA] = val_gamma;

            if (profile[PROFILE_GAMMA] >= trkGamma.Minimum && profile[PROFILE_GAMMA] <= trkGamma.Maximum)
            {
                trkGamma.Value = profile[PROFILE_GAMMA];
                trkGamma_Scroll(null, null);
            }
        }

        private void radPro1_CheckedChanged(object sender, EventArgs e)
        {
            defProfile = 1;
            profileChange((int[])profiles["1"]);

            if (silent) this.Close();
        }

        private void radPro1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (minimalView) toggleMinimalView();
        }

        private void radPro2_CheckedChanged(object sender, EventArgs e)
        {
            defProfile = 2;
            profileChange((int[])profiles["2"]);

            if (silent) this.Close();
        }

        private void radPro2_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (minimalView) toggleMinimalView();
        }

        private void radPro3_CheckedChanged(object sender, EventArgs e)
        {
            defProfile = 3;
            profileChange((int[])profiles["3"]);

            if (silent) this.Close();
        }

        private void radPro3_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (minimalView) toggleMinimalView();
        }

        private void radPro4_CheckedChanged(object sender, EventArgs e)
        {
            defProfile = 4;
            profileChange((int[])profiles["4"]);

            if (silent) this.Close();
        }

        private void radPro4_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (minimalView) toggleMinimalView();
        }

        private void trkBrightness_Scroll(object sender, EventArgs e)
        {
            grpBrightness.Text = String.Format("&Brightness ({0}%)", Math.Round((double)((double)(trkBrightness.Value - trkBrightness.Minimum) / (double)(trkBrightness.Maximum - trkBrightness.Minimum)) * 100));

            int[] profile = (int[])profiles[defProfile.ToString()];
            profile[PROFILE_BRIGHTNESS] = trkBrightness.Value;

            SendMessage(hwnd_brightness, TBM_SETPOSNOTIFY, new IntPtr(1), new IntPtr(trkBrightness.Value));
        }

        private void trkContrast_Scroll(object sender, EventArgs e)
        {
            grpContrast.Text = String.Format("&Contrast ({0}%)", Math.Round((double)((double)(trkContrast.Value - trkContrast.Minimum) / (double)(trkContrast.Maximum - trkContrast.Minimum)) * 100));

            int[] profile = (int[])profiles[defProfile.ToString()];
            profile[PROFILE_CONTRAST] = trkContrast.Value;

            SendMessage(hwnd_contrast, TBM_SETPOSNOTIFY, new IntPtr(1), new IntPtr(trkContrast.Value));
        }

        private void trkGamma_Scroll(object sender, EventArgs e)
        {
            grpGamma.Text = String.Format("&Gamma ({0})", Math.Round(0.3 + (double)((double)(trkGamma.Value - trkGamma.Minimum) / (double)(trkGamma.Maximum - trkGamma.Minimum)) * 2.5, 2).ToString("#0.00"));

            int[] profile = (int[])profiles[defProfile.ToString()];
            profile[PROFILE_GAMMA] = trkGamma.Value;

            SendMessage(hwnd_gamma, TBM_SETPOSNOTIFY, new IntPtr(1), new IntPtr(trkGamma.Value));
        }

        private void lnkHotkey_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            int[] profile = (int[])profiles[defProfile.ToString()];

            frmHotkey objHk = new frmHotkey((Keys)profile[PROFILE_HOTKEY], (Keys)profile[PROFILE_HOTKEYMOD], hotkey_callback);
            objHk.ShowDialog(this);
        }

        private void hotkey_callback(Keys Hotkey, Keys HotkeyModifiers)
        {
            int[] profile = (int[])profiles[defProfile.ToString()];

            profile[PROFILE_HOTKEY] = (int)Hotkey;
            profile[PROFILE_HOTKEYMOD] = (int)HotkeyModifiers;

            registerHotkeys();
        }

        private void lnkReset_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            if (val_brightness >= trkBrightness.Minimum && val_brightness <= trkBrightness.Maximum)
            {
                trkBrightness.Value = val_brightness;
                trkBrightness_Scroll(null, null);
            }

            if (val_contrast >= trkContrast.Minimum && val_contrast <= trkContrast.Maximum)
            {
                trkContrast.Value = val_contrast;
                trkContrast_Scroll(null, null);
            }

            if (val_gamma >= trkGamma.Minimum && val_gamma <= trkGamma.Maximum)
            {
                trkGamma.Value = val_gamma;
                trkGamma_Scroll(null, null);
            }
        }

        private void tmrLoad_Tick(object sender, EventArgs e)
        {
            tmrLoad.Stop();
            if (tmrLoad.Interval != 1000) tmrLoad.Interval = 1000;

            // find the nvidia control panel window

            IntPtr wnd = FindWindow(null, "NVIDIA Control Panel");
            if (wnd != IntPtr.Zero)
            {
                wnd = FindWindowEx(wnd, IntPtr.Zero, "AfxFrameOrView100su", "{73BCA54E-6AEB-4597-8F27-E1284FF12722}:{B53EBC0C-2251-4AE2-9818-FD6AAF843EC2}");
                if (wnd != IntPtr.Zero)
                {
                    wnd = FindWindowEx(wnd, IntPtr.Zero, "#32770", null);
                    if (wnd != IntPtr.Zero)
                    {
                        // find the trackbar controls

                        hwnd_brightness = FindWindowEx(wnd, IntPtr.Zero, "msctls_trackbar32", null);
                        hwnd_contrast = FindWindowEx(wnd, hwnd_brightness, "msctls_trackbar32", null);
                        hwnd_gamma = FindWindowEx(wnd, hwnd_contrast, "msctls_trackbar32", null);
                    }

                    if (hwnd_brightness != IntPtr.Zero && hwnd_contrast != IntPtr.Zero && hwnd_gamma != IntPtr.Zero)
                    {
                        // get the current values

                        val_brightness = (int)SendMessage(hwnd_brightness, TBM_GETPOS, IntPtr.Zero, IntPtr.Zero);
                        val_contrast = (int)SendMessage(hwnd_contrast, TBM_GETPOS, IntPtr.Zero, IntPtr.Zero);
                        val_gamma = (int)SendMessage(hwnd_gamma, TBM_GETPOS, IntPtr.Zero, IntPtr.Zero);

                        grpOptions.Enabled = true;
                        pnlLoading.Visible = false;

                        if (defProfile == 1) radPro1.Checked = true;
                        else if (defProfile == 2) radPro2.Checked = true;
                        else if (defProfile == 3) radPro3.Checked = true;
                        else if (defProfile == 4) radPro4.Checked = true;
                        else radPro1.Checked = true;

                        registerHotkeys();
                    }
                    else
                    {
                        lblLoading.Text = "Error: Unable to find the color adjustment controls.";
                    }
                }
                else
                {
                    lblLoading.Text = "Please close this app and open the Nvidia Control Panel. Click on 'Adjust desktop color settings'.";
                }
            }
            else
            {
                tmrLoad.Start();
            }
        }
    }
}

Download frmMain.cs

Back to file list


Back to project page