Click here to Skip to main content
15,894,825 members
Articles / Programming Languages / C#

Window Tray Minimizer

Rate me:
Please Sign up or sign in to vote.
4.91/5 (60 votes)
26 Oct 2007CPOL6 min read 180.6K   4.1K   203  
An article showing how to minimize any Window to the system tray
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Tray_minimizer
{
    public partial class Options : Form
    {
        uint showmod;
        uint showkey;
        uint hidemod;
        uint hidekey;
        bool startup;
        bool ignoretitle;

        public bool Ignoretitle
        {
            get { return ignoretitle; }
            set { ignoretitle = value; }
        }

        public bool Startup
        {
            get { return startup; }
            set { startup = value; }
        }

        public uint Showmod
        {
            get { return showmod; }
            set { showmod = value; }
        }

        public uint Showkey
        {
            get { return showkey; }
            set { showkey = value; }
        }

        public uint Hidemod
        {
            get { return hidemod; }
            set { hidemod = value; }
        }

        public uint Hidekey
        {
            get { return hidekey; }
            set { hidekey = value; }
        }

        public Options()
        {
            InitializeComponent();
        }

        private void Options_Load(object sender, EventArgs e)
        {
            for (int i = 65; i < 91; i++)
            {
                showkeycombo.Items.Add((char)i);
                hidekeycombo.Items.Add((char)i);
            }

            if (System.IO.File.Exists("Tray minimizer.exe.config"))
            {
                showctrlcheck.Checked = ((int)winapi.KeyModifiers.Control & showmod) > 0;
                showaltcheck.Checked = ((int)winapi.KeyModifiers.Alt & showmod) > 0;
                showshiftcheck.Checked = ((int)winapi.KeyModifiers.Shift & showmod) > 0;

                hidectrlcheck.Checked = ((int)winapi.KeyModifiers.Control & hidemod) > 0;
                hidealtcheck.Checked = ((int)winapi.KeyModifiers.Alt & hidemod) > 0;
                hideshiftcheck.Checked = ((int)winapi.KeyModifiers.Shift & hidemod) > 0;

                showkeycombo.SelectedIndex = (int)showkey;
                hidekeycombo.SelectedIndex = (int)hidekey; 
            }

            startcheck.Checked = startup;
            ignoretitlecheck.Checked = ignoretitle;
        }

        private void getnewhotkey()
        {
            showmod = 0;
            hidemod = 0;

            if (this.showshiftcheck.Checked)
            {
                showmod = showmod | (uint)winapi.KeyModifiers.Shift;
            }
            if (this.showctrlcheck.Checked)
            {
                showmod = showmod | (uint)winapi.KeyModifiers.Control;
            }
            if (this.showaltcheck.Checked)
            {
                showmod = showmod | (uint)winapi.KeyModifiers.Alt;
            }

            if (this.hideshiftcheck.Checked)
            {
                hidemod = hidemod | (uint)winapi.KeyModifiers.Shift;
            }
            if (this.hidectrlcheck.Checked)
            {
                hidemod = hidemod | (uint)winapi.KeyModifiers.Control;
            }
            if (this.hidealtcheck.Checked)
            {
                hidemod = hidemod | (uint)winapi.KeyModifiers.Alt;
            }

            showkey = (uint)this.showkeycombo.SelectedIndex;
            hidekey = (uint)this.hidekeycombo.SelectedIndex;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            getnewhotkey();

            if (showkeycombo.SelectedIndex > 0 && showkeycombo.SelectedIndex == hidekeycombo.SelectedIndex && showmod==hidemod && showmod>0)
            {
                MessageBox.Show("Hotkeys cannot be the same", "Error", MessageBoxButtons.OKCancel, MessageBoxIcon.Error);
                return;
            }

            this.DialogResult = DialogResult.OK;
            startup = startcheck.Checked;
            ignoretitle = ignoretitlecheck.Checked;
            this.Close();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Georgia Georgia
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions