I'm starting my studies in C# and the first goal I defined to myself is writing a small application that starts running on the system tray and shows the CPU temperature on its tip when the user passes the mouse over its icon. I've already found many good questions about what I need (as in here, here, here and here). However, I'd like to do something that I could not find the answer yet.
My application will create its only form during runtime and shows it only if the user clicks in one of the options in the tray icon context menu. It's already working on my project, but I was not able to find a way to hide this form when the user clicks on its minimize button.
I want the application to behave this way: when the user clicks on the form's minimze button, it shall be hidden (and not shown in task bar), but the application shall still be kept running in the system tray.
So, how can I associate a resize or minimize (or whatever) event to a form that is created during runtime?
I'd appreciate any help. Thanks in advance.
P.S.: The form isn't the application's start object, but a customized Application Context.
Edit:
Relevant code excerpt
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MyApplicationContext());
}
}
public class AppForm : System.Windows.Forms.Form
{
public AppForm()
{
this.Size = new System.Drawing.Size(300, 300);
this.StartPosition = FormStartPosition.CenterScreen;
this.MaximizeBox = false;
this.FormBorderStyle = FormBorderStyle.FixedSingle;
this.Icon = CPUTemp.Properties.Resources.AppIcon;
}
}
public class MyApplicationContext : ApplicationContext
{
NotifyIcon trayIcon = new NotifyIcon();
AppForm frmSetup = new AppForm();
public MyApplicationContext()
{
MenuItem setupMenuItem = new MenuItem("Setup", new EventHandler(ShowSetup));
MenuItem exitMenuItem = new MenuItem("Exit", new EventHandler(Exit));
(...)