How to Start Winform App Minimized to Tray

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.

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 and Hide Winform on Form Load

It might be best to change your logic slightly, and instead of showing the form at all, create an instance of it (var foo = new MyForm()) but don't call .Show() so it will never be shown until triggered from your system tray icon.

Minimize Windows form on system Start up

try this.... put this in your form:

 protected override void SetVisibleCore(bool value)
{
base.SetVisibleCore(false);
}

this will always now make the form invisible.

you will need some logic though to determine if it should be shown or not from other parts of your app. for example, set a global bool value and modify the code above to use that.

alternatively, you can use this:

protected override void OnVisibleChanged(EventArgs e)
{
base.OnVisibleChanged(e);
this.Visible = false;
}

but you will see a bit of a flash when you run the app straight away. you then, again, need to control when to make it visible so check the global bool value for the visible property so you can eventually display the form

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 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;
}

Minimizing a system windows form in tray in C# without seeing it hanging where taskbar

All you have to do is call Hide() and Show() when you want to hide and show your form.
NOTE: Hide() will hide from taskbar as well.

private void Form1_Resize(object sender, EventArgs e)
{
if (WindowState == FormWindowState.Minimized)
Hide();
}

You may hide and show the NotifyIcon opposite to the form to not have a icon when the form is shown.

Obviously you need a NotifyIcon to display your app in the system tray.

Finally your code will look like this:

private void Form1_Resize(object sender, EventArgs e)
{
if (WindowState == FormWindowState.Minimized)
{
Hide();
notifyIcon1.Visible = true;
}
}

private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
{
Show();
notifyIcon1.Visible = false;
WindowState = FormWindowState.Normal;
}


Related Topics



Leave a reply



Submit