Jasinski Technical Wiki

Navigation

Home Page
Index
All Pages

Quick Search
»
Advanced Search »

Contributor Links

Create a new Page
Administration
File Management
Login/Logout
Your Profile

Other Wiki Sections

Software

PoweredBy

Minimize to System Tray - C# and Windows Forms

RSS
Modified on Tue, Aug 27, 2019, 1:00 PM by Administrator Categorized as ·Net Framework

Overview

This article explains how to have a Windows Form app minimize to the System Tray when the close button is clicked.

Note: You'll need a NotifyIcon on the form of interest, along with a context menu to quit the application.

Code

// When the user attempts to close the form, this code hides the form instead of closing it
private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
{
    if (e.CloseReason == CloseReason.UserClosing)
    {
        e.Cancel = true;
        Hide();
    }
}
// Clicking the NotifyIcon will restore the form to its normal state
private void uxNotifyIcon_MouseUp(object sender, MouseEventArgs e)
{
    if (e.Button == MouseButtons.Left)
    {
        Show();
        WindowState = FormWindowState.Normal;
        Activate();
    }
}
// This is the code behind the "Quit" item on the NotifyIcon's context menu
private void uxQuitMenuItem_Click(object sender, EventArgs e)
{
    Application.Exit();
}

ScrewTurn Wiki version 3.0.1.400. Some of the icons created by FamFamFam. Except where noted, all contents Copyright © 1999-2024, Patrick Jasinski.