What's the proper way to minimize to tray a C# WinForms app?
There is actually no managed way to do that form of animation to the tray in native winforms, however you can P/Invoke shell32.dll to do it:
Some good info here (In the comments not the post):
http://blogs.msdn.com/jfoscoding/archive/2005/10/20/483300.aspx
And here it is in C++:
http://www.codeproject.com/KB/shell/minimizetotray.aspx
You can use that to figure out what stuff to Pinvoke for your C# version.
minimize app to system tray
- C# System Tray Minimize To Tray With NotifyIcon
- Minimize window to system tray
Handle the form’s Resize event. In this handler, you override the
basic functionality of the Resize event to make the form minimize to
the system tray and not to the taskbar. This can be done by doing the
following in your form’s Resize event handler: Check whether the
form’s WindowState property is set to FormWindowState.Minimized. If
yes, hide your form, enable the NotifyIcon object, and show the
balloon tip that shows some information. Once the WindowState becomes
FormWindowState.Normal, disable the NotifyIcon object by setting its
Visible property to false. Now, you want the window to reappear when
you double click on the NotifyIcon object in the taskbar. For this,
handle the NotifyIcon’s MouseDoubleClick event. Here, you show the
form using the Show() method.
private void frmMain_Resize(object sender, EventArgs e)
{
if (FormWindowState.Minimized == this.WindowState)
{
mynotifyicon.Visible = true;
mynotifyicon.ShowBalloonTip(500);
this.Hide();
}
else if (FormWindowState.Normal == this.WindowState)
{
mynotifyicon.Visible = false;
}
}
How to start WinForm app minimized to tray?
The right way to do this is to prevent the form from getting visible in the first place. That requires overriding SetVisibleCore(). Let's assume a context menu for the NotifyIcon with a Show and Exit command. You can implement it like this:
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
notifyIcon1.ContextMenuStrip = contextMenuStrip1;
this.showToolStripMenuItem.Click += showToolStripMenuItem_Click;
this.exitToolStripMenuItem.Click += exitToolStripMenuItem_Click;
}
private bool allowVisible; // ContextMenu's Show command used
private bool allowClose; // ContextMenu's Exit command used
protected override void SetVisibleCore(bool value) {
if (!allowVisible) {
value = false;
if (!this.IsHandleCreated) CreateHandle();
}
base.SetVisibleCore(value);
}
protected override void OnFormClosing(FormClosingEventArgs e) {
if (!allowClose) {
this.Hide();
e.Cancel = true;
}
base.OnFormClosing(e);
}
private void showToolStripMenuItem_Click(object sender, EventArgs e) {
allowVisible = true;
Show();
}
private void exitToolStripMenuItem_Click(object sender, EventArgs e) {
allowClose = true;
Application.Exit();
}
}
Note a wrinkle with the Load event, it won't fire until the main form is first shown. So be sure to do initialization in the form's constructor, not the Load event handler.
Minimize to tray
Handle the form’s Resize event. In this handler, you override the
basic functionality of the Resize event to make the form minimize to
the system tray and not to the taskbar. This can be done by doing the
following in your form’s Resize event handler:
- Check whether the form’s WindowState property is set to FormWindowState.Minimized. If yes, hide your form, enable the NotifyIcon object, and show the balloon tip that shows some information.
- Once the WindowState becomes FormWindowState.Normal, disable the NotifyIcon object by setting its Visible property to false.
- Now, you want the window to reappear when you double click on the NotifyIcon object in the taskbar. For this, handle the NotifyIcon’s
MouseDoubleClick
event. Here, you show the form using theShow()
method.
In the form resize event, do the check there and hide the form
private void Form_Resize(object sender, EventArgs e)
{
if (WindowState == FormWindowState.Minimized)
{
this.Hide();
}
}
Then when clicking on the taskbar icon just restore it.
private void notifyIcon_Click(object sender, EventArgs e)
{
this.Show();
this.WindowState = FormWindowState.Normal;
}
Refer:
How do I minimize a WinForms application to the notification area?
minimize app to system tray
How do I minimize a WinForms application to the notification area?
What about the option of hiding the form when minimized then showing once you click on the tray icon?
In the form resize event, do the check there and hide the form
private void Form_Resize(object sender, EventArgs e)
{
if (WindowState == FormWindowState.Minimized)
{
this.Hide();
}
}
Then when clicking on the taskbar icon just restore it.
private void notifyIcon_Click(object sender, EventArgs e)
{
this.Show();
this.WindowState = FormWindowState.Normal;
}
C# Minimize to system tray on close
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.
Minimize my winform application into system tray
Try this:
private void MainForm_Resize(object sender, EventArgs e)
{
switch (this.WindowState)
{
case FormWindowState.Maximized:
this.ShowInTaskbar = true;
break;
case FormWindowState.Minimized:
this.ShowInTaskbar = false;
break;
case FormWindowState.Normal:
this.ShowInTaskbar = true;
break;
default:
break;
}
}
Related Topics
Using the Wpf Dispatcher in Unit Tests
Webclient Accessing Page with Credentials
How to Implement a Read Only Property
How to Run a Task on a Custom Taskscheduler Using Await
How to Add a Delay for a 2 or 3 Seconds
C# Conditional and (&&) or (||) Precedence
How to Delete a Directory with Read-Only Files in C#
How to Implement Smooth Scroll in a Wpf Listview
Does List<T> Guarantee Insertion Order
In C# Differencebetween Toupper() and Toupperinvariant()
Fastest Way to Convert Image to Byte Array
Determine If Uploaded File Is Image (Any Format) on MVC
No Connection String Named 'Myentities' Could Be Found in the Application Config File