Sunday, November 27, 2016

Hide Program using C#

Hi this post show how to to hide your C# Application in system tray.To hide your program in system tray you need "notifyicon" control from toolbox.Create a new windows application and design a form like this




NotifyIcon:

Move to your toolbox scroll down and double click on NotifyIcon. The NotifyIcon Control present in System.Windows.Form.Control.


Set the Visibility to false for the NotifyIcon so that notifiy icon wont appear on the system tray when program runs.



Then create a event for the from "SizeChanged" and MouseDoubleClick for notifyIcon and Paste the following code.

//MouseDouble Click event for NotifyIcon
private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            try
            {
                Show();
                this.WindowState = FormWindowState.Normal;
                notifyIcon1.Visible = false;
            }
            catch (Exception ex)
            {
                MessageBox.Show("Exception:" + ex.ToString());
            }
        }
        
//SizeChanged event for form
        private void Form1_SizeChanged(object sender, EventArgs e)
        {
            try
            {
                if (this.WindowState == FormWindowState.Minimized)
                {
                    Hide();
                    notifyIcon1.Visible = true;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Exception:" + ex.ToString());
            }
        }



Complete Source Code:


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace HideProgram
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            try
            {
                Show();
                this.WindowState = FormWindowState.Normal;
                notifyIcon1.Visible = false;
            }
            catch (Exception ex)
            {
                MessageBox.Show("Exception:" + ex.ToString());
            }
        }
        
        private void Form1_SizeChanged(object sender, EventArgs e)
        {
            try
            {
                if (this.WindowState == FormWindowState.Minimized)
                {
                    Hide();
                    notifyIcon1.Visible = true;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Exception:" + ex.ToString());
            }
        }
    }
}


Output:




Download Source Code

Source Code



No comments:

Post a Comment