C# Minimize to system tray on close

前端 未结 7 1127
再見小時候
再見小時候 2020-12-28 09:10

Hi In my c# application I am trying to minimize application to systems tray, when the form is closed. Here is the code I have tried.

   public void MinimizeT         


        
相关标签:
7条回答
  • 2020-12-28 09:20

    You need to use the FormClosing-Event.

    private void Form1_FormClosing(Object sender, FormClosingEventArgs e) {
        e.Cancel = true;
        MinimizeToTray();
    }
    
    0 讨论(0)
  • 2020-12-28 09:22

    Write a event in Form Closing event.

     private void Form1_FormClosing(object sender, FormClosingEventArgs e)
     {
            e.Cancel = true;                         
            Hide();
     }
    

    And write using Custom menu strip for notification icon for to show.

    0 讨论(0)
  • 2020-12-28 09:22

    e.Cancel = true; code will be always cancelling the event even if you shut the computer down, but here is a code that helps you:

     private void Form1_FormClosing(object sender, FormClosingEventArgs e)
     {
         if (e.CloseReason == CloseReason.UserClosing)
         {
              myNotifyIcon.Visible = true;
              this.Hide();
              e.Cancel = true;
         }
     }
    

    It will allow closing the form programmaticaly.

    0 讨论(0)
  • 2020-12-28 09:25

    You should cancel the FormClosing event and then call your MinimizeToTray() function.

    This is done through the Cancel property of the FormClosingEventArgs.

    Also, consider using a bool somewhere to allow closing the Form in some conditions, such as if you're using a File > Exit menu or something:

    private void Form1_FormClosing(object sender, FormClosingEventArgs e)
    {
        if(!allowClosing)
        {
            e.Cancel = true;
            MinimizeToTray();
        }
    }
    
    0 讨论(0)
  • 2020-12-28 09:31
       namespace MinimizeTrayNotification
         {
          public partial class Form1 : Form
           {
        public Form1()
        {
            InitializeComponent();
        }
    
        private void MinimzedTray()
        {
            notifyIcon1.Visible = true;
            notifyIcon1.Icon = SystemIcons.Application;
    
            notifyIcon1.BalloonTipText = "Minimized";
            notifyIcon1.BalloonTipTitle = "Your Application is Running in BackGround";
            notifyIcon1.ShowBalloonTip(500);
    
        }
    
        private void MaxmizedFromTray()
        {
            notifyIcon1.Visible = true;
            notifyIcon1.BalloonTipText = "Maximized";
            notifyIcon1.BalloonTipTitle = "Application is Running in Foreground";
            notifyIcon1.ShowBalloonTip(500);
    
    
        }
    
    
    
        private void Form1_Resize(object sender, EventArgs e)
        {
            if(FormWindowState.Minimized==this.WindowState)
            {
            MinimzedTray();
            }
          else  if (FormWindowState.Normal == this.WindowState)
            {
    
                MaxmizedFromTray();
            }
            }
    
        private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            this.WindowState = FormWindowState.Normal;
            Form1 frm = new Form1();
            frm.Show();
            MaxmizedFromTray();
    
    
        }
    
        private void notifyIcon1_MouseClick(object sender, MouseEventArgs e)
        {
            this.WindowState = FormWindowState.Normal;
        }
    
        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (e.CloseReason == CloseReason.UserClosing)
            {
                e.Cancel = true;
                this.WindowState = FormWindowState.Minimized;
                this.ShowInTaskbar = false;
                this.Hide();
    
            }
    
    
        }
    
        private void notifyIcon1_Click(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Normal;
            notifyIcon1.BalloonTipText = "Normal";
            notifyIcon1.ShowBalloonTip(500);
        }
     }
    

    }

    0 讨论(0)
  • 2020-12-28 09:31

    To minimize when closing set WindowState to Minimized

    private void Form1_FormClosing(Object sender, FormClosingEventArgs e) {
        e.Cancel = true;
        WindowState = FormWindowState.Minimized;
    }
    
    0 讨论(0)
提交回复
热议问题