Projects

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

quickGamma

Browsing frmMain.cs (11.54 KB)

using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace quickGamma
{
    ///*************************************************************************
    /// <summary>
    /// Quickly adjust the brightness for computer monitor(s).
    /// </summary>
    /// <remarks>
    /// <para>
    /// It uses the Interop <see cref="SetDeviceGammaRamp"/> call to set the
    /// Gamma component to adjust the screen contrast.
    /// </para>
    /// <para>
    /// The <see cref="SetDeviceGammaRamp"/> function sets the gamma ramp on
    /// direct color display boards having drivers that support downloadable
    /// gamma ramps in hardware.
    /// </para>
    /// <para>
    /// Direct color display modes do not use color lookup tables and are
    /// usually 16, 24, or 32 bit. Not all direct color video boards support
    /// loadable gamma ramps. SetDeviceGammaRamp succeeds only for devices with
    /// drivers that support downloadable gamma ramps in hardware.
    /// </para>
    /// </remarks>
    public class frmMain : Form
    {
        // *********************************************************************
        /// <summary>
        /// A structure of gamma corrections.
        /// </summary>
        [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Ansi)]
        private struct RAMP
        {
            [ MarshalAs(UnmanagedType.ByValArray, SizeConst=256)]
            public UInt16[] Red;
            [ MarshalAs(UnmanagedType.ByValArray, SizeConst=256)]
            public UInt16[] Green;
            [ MarshalAs(UnmanagedType.ByValArray, SizeConst=256)]
            public UInt16[] Blue;
        }

        [DllImport("gdi32.dll")]
        private static extern bool SetDeviceGammaRamp(IntPtr hDC, ref RAMP lpRamp);

        [DllImport("user32.dll")]
        static extern IntPtr GetDC(IntPtr hWnd);

        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        static extern bool AppendMenu(IntPtr hMenu, uint wFlags, uint uIDNewItem, string lpNewItem);

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

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

        [DllImport("user32.dll")]
        static extern IntPtr GetSystemMenu(IntPtr hWnd, bool bRevert);
        
        [DllImport("shell32.dll")]
        static extern int ShellAbout(IntPtr hWnd, string szApp, string szOtherStuff, IntPtr hIcon);

        /// <summary> Required designer variable.</summary>
        private System.ComponentModel.IContainer components = null;
        private TrackBar m_trackBar;
        private static RAMP s_ramp = new RAMP();

        private const String VALUE_CMDLINEARG = "/value";
        private string[] appArgs;

        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 mnuAbout = 5000;
        private const int mnuAlwaysOnTop = 5001;

        // *********************************************************************
        /// <summary>
        /// Initializes a new instance of <see cref="frmMain"/> class.
        /// </summary>
        public frmMain(string[] args)
        {
            InitializeComponent();

            appArgs = args;
        }
        // *********************************************************************
        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be
        /// disposed; otherwise, false.</param>
        protected override void Dispose (bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }
        // *********************************************************************
        /// <summary>
        /// Handles the <see cref="TrackBar.ValueChanged"/> event for the TrackBar
        /// control. The value of the track bar control sets the Gamma value for
        /// the device.
        /// </summary>
        /// <param name="sender">The object that initiated the event.</param>
        /// <param name="e">An <see cref="EventArgs"/> object containing event
        /// data</param>
        private void HandleValueChanged (object sender, EventArgs e)
        {
            SetGamma(m_trackBar.Value);
            this.Text = "quickGamma (" + m_trackBar.Value.ToString() + ")";
        }
        // *********************************************************************
        /// <summary>
        /// Main entry point for the application.
        /// </summary>
        /// <param name="args">An array of optional arguments.</param>
        static void Main (string[] args)
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            frmMain mainForm = new frmMain(args);
            Application.Run(mainForm);
        }
        // *********************************************************************
        /// <summary>
        /// Sets the Gamma corection at the specified <paramref name="gamma"/>
        /// value.
        /// </summary>
        /// <param name="gamma">New Gamma value to be set.</param>
        private static void SetGamma(int gamma)
        {
            s_ramp.Red = new ushort[256];
            s_ramp.Green = new ushort[256];
            s_ramp.Blue = new ushort[256];
            for( int i=1; i<256; i++ )
            {
                // gamma is a value between 3 and 44
                s_ramp.Red[i] = s_ramp.Green[i] = s_ramp.Blue[i] =
                    (ushort)(Math.Min(65535,
                    Math.Max(0, Math.Pow((i+1) / 256.0, gamma*0.1) * 65535 + 0.5)));
            }
            // Now set the value.
            SetDeviceGammaRamp(GetDC(IntPtr.Zero), ref s_ramp);
        }

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

            if (m.Msg == WM_SYSCOMMAND)
            {
                if ((int)m.WParam == mnuAbout)
                {
                    Assembly assembly = Assembly.GetExecutingAssembly();
                    FileVersionInfo fvi = FileVersionInfo.GetVersionInfo(assembly.Location);
                    string productName = fvi.ProductName;
                    string productVersion = fvi.ProductVersion;

                    ShellAbout(m.HWnd, productName + " " + productVersion,
                        productName + " " + productVersion + "\r\n" +
                        "Developed by JaviteSoft ( www.javitesoft.com )",
                        this.Icon.Handle);
                }
                else 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);
                }
            }
        }

        private void frmMain_Load(object sender, EventArgs e)
        {
            bool forceQuit = false;

            // check for any cmdline flags
            for (int i = 0; i < appArgs.Length; i++)
            {
                if (appArgs[i] == VALUE_CMDLINEARG && i < appArgs.Length - 1)
                {
                    int value = 0;

                    if (int.TryParse(appArgs[i + 1], out value) && value > 0)
                    {
                        // check the value range
                        if (value >= m_trackBar.Minimum && value <= m_trackBar.Maximum)
                        {
                            // set gamma to specified value
                            SetGamma(value);

                            forceQuit = true;
                        }
                    }

                    break;
                }
            }

            if (forceQuit)
            {
                // gamme set via cmdline so terminate app
                Application.Exit();
            }
            else
            {
                // prepare the interface for display
                // load custom menu items

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

                AppendMenu(sysMenuHwnd, MF_SEPARATOR, 0, null);
                AppendMenu(sysMenuHwnd, MF_STRING | MF_CHECKED, mnuAlwaysOnTop, "Always On Top");
                AppendMenu(sysMenuHwnd, MF_STRING, mnuAbout, "About...");
            }
        }

        #region Windows Form Designer generated code
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent ()
        {
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(frmMain));
            this.m_trackBar = new System.Windows.Forms.TrackBar();
            ((System.ComponentModel.ISupportInitialize)(this.m_trackBar)).BeginInit();
            this.SuspendLayout();
            // 
            // m_trackBar
            // 
            this.m_trackBar.Dock = System.Windows.Forms.DockStyle.Fill;
            this.m_trackBar.Location = new System.Drawing.Point(0, 0);
            this.m_trackBar.Maximum = 44;
            this.m_trackBar.Minimum = 3;
            this.m_trackBar.Name = "m_trackBar";
            this.m_trackBar.RightToLeft = System.Windows.Forms.RightToLeft.Yes;
            this.m_trackBar.RightToLeftLayout = true;
            this.m_trackBar.Size = new System.Drawing.Size(277, 42);
            this.m_trackBar.TabIndex = 0;
            this.m_trackBar.Value = 18;
            this.m_trackBar.ValueChanged += new System.EventHandler(this.HandleValueChanged);
            // 
            // frmMain
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(277, 42);
            this.Controls.Add(this.m_trackBar);
            this.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
            this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
            this.MaximizeBox = false;
            this.Name = "frmMain";
            this.SizeGripStyle = System.Windows.Forms.SizeGripStyle.Hide;
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            this.Text = "quickGamma";
            this.TopMost = true;
            this.Load += new System.EventHandler(this.frmMain_Load);
            ((System.ComponentModel.ISupportInitialize)(this.m_trackBar)).EndInit();
            this.ResumeLayout(false);
            this.PerformLayout();

        }
        #endregion
    } // end class frmMain
} // end namespace quickGamma

Download frmMain.cs

Back to file list


Back to project page