I have a Winform, which is hidden initially on startup.
Thereafter the user is free to click the notification icon in the bottom and show it if they want, and when it is minimized it needs to go back into the system tray.
It starts in the tray fine - no problems. When you click to show it the first time though it appears, then for a fraction of a second looks like it is disappearing, then comes back. So it looks like it flickers a bit.
Then when you minimize it, it goes into the system tray as it should which is fine. When you click to show it again though (any time after you have done it once) it sort of glides in from either the system tray or the taskbar, I would prefer it to just appear, without the little animation.
public class Program : Form
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Program());
}
private NotifyIcon trayIcon;
private ContextMenu trayMenu;
public Program()
{
trayMenu = new ContextMenu();
trayMenu.MenuItems.Add("Exit", OnExit);
trayMenu.MenuItems.Add("Show", OnShow);
trayIcon = new NotifyIcon();
trayIcon.Text = "MyTrayApp";
trayIcon.Icon = new Icon(SystemIcons.Application, 40, 40);
trayIcon.ContextMenu = trayMenu;
trayIcon.Visible = true;
}
protected override void OnLoad(EventArgs e)
{
Visible = false;
ShowInTaskbar = false;
base.OnLoad(e);
}
protected override void OnResize(EventArgs e)
{
if (this.WindowState == FormWindowState.Minimized)
{
Visible = false;
ShowInTaskbar = false;
}
base.OnResize(e);
}
private void OnExit(object sender, EventArgs e)
{
Application.Exit();
}
private void OnShow(object sender, EventArgs e)
{
Visible = true;
ShowInTaskbar = true;
TopMost = true;
WindowState = FormWindowState.Normal;
}
protected override void Dispose(bool isDisposing)
{
if (isDisposing)
{
trayIcon.Dispose();
}
base.Dispose(isDisposing);
}
}
Any pointers on how to just get it to appear properly would be really appreciated.
Update
I have found the cause of the flickering, it happened when TopMost
is set last, after is has been shown, so it redraws it on top, which makes sense.
So as it stands, it's just getting it to appear and disappear without the animations.
EDIT: You should only set the Visibility
property in Onload()
, just use Show()
and Hide()
to avoid the animations and be sure to change the WindowState
accordingly.
I put your code into a regular Form
(not in the Program.cs file) and deleted all superfluous code. This is what I ended up with and it doesn't show the "double" animation.
public partial class Form1 : Form
{
private NotifyIcon trayIcon;
private ContextMenu trayMenu;
public Form1()
{
InitializeComponent();
this.ClientSize = new System.Drawing.Size(284, 262);
this.Name = "Program";
trayMenu = new ContextMenu();
trayMenu.MenuItems.Add("Exit");
trayMenu.MenuItems.Add("Show", FormShow);
trayIcon = new NotifyIcon();
trayIcon.Text = "MyTrayApp";
trayIcon.Icon = new Icon(SystemIcons.Application, 40, 40);
trayIcon.ContextMenu = trayMenu;
trayIcon.Visible = true;
TopMost = true;
Resize += new EventHandler(Form1_Resize);
}
void Form1_Resize(object sender, EventArgs e)
{
if (WindowState == FormWindowState.Minimized)
{
ShowInTaskbar = false;
Hide();
trayIcon.Visible = true;
}
}
void FormShow(object sender, EventArgs e)
{
if (WindowState == FormWindowState.Minimized)
{
WindowState = FormWindowState.Normal;
}
ShowInTaskbar = true;
Show();
Focus();
trayIcon.Visible = false;
}
protected override void OnLoad(EventArgs e)
{
Visible = false;
ShowInTaskbar = false;
}
}