BalloonTrayIcon and Form Hiding: Avoid Flickering

H

Hector Santos

What do you see in the (main) form properties to implement a
NotifyICON so that the form does not "Flicker" on startup?

I can make it hide, but there is a flicker. Calling Hide() or setting
visible in Form1_Load() doesn't work. So I have it in Form1_Shown().

Do I have to edit the auto-generated form1.designer.cs class to set
the default visible flag to false? The property editor does not have
a Visible property. All I see is a ShowInTrayBar and I turned that
off too.

Basically I have this:

private void Form1_Load(object sender, EventArgs e)
{
Hide();
notifyIcon1.Visible = mySettings.UseSystemTray;
}

private void Form1_Shown(object sender, EventArgs e)
{
if (notifyIcon1.Visible)
{
Hide();
}
else
{
Show();
}
}

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

//
// Notify icon Menu Item: Exit
//

private void miTrayExit_Click(object sender, EventArgs e)
{
notifyIcon1.Visible = false;
Close();
}

//
// Notify icon Menu Item: Status
//

private void miTrayStatus_Click(object sender, EventArgs e)
{
Show();
}

The question is: how to removing the flicking? Hiding on load doe not
work to disable the visibility flag by the time Form1_Shown() is
called. So it seems to be set at that point.
 
P

Peter Duniho

Hector said:
What do you see in the (main) form properties to implement a NotifyICON
so that the form does not "Flicker" on startup?

Typically, if you don't want your form to be visible when the program
starts, the way to address that is to not show it in the first place.

Edit Program.cs in your project so that you create your form instance,
but call Application.Run() without any arguments. Then the form is
shown only when you explicitly call the Show() method (or set Visible to
true, of course). To get the Application.Run() method to return and
allow the program to terminate normally, call Application.ExitThread().

See below for a short example.

Pete


// Program.cs
using System;
using System.Windows.Forms;

namespace TestNotifyIcon
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Form1 form1 = new Form1();
Application.Run();
GC.KeepAlive(form1);
}
}
}


// Form1.cs
using System;
using System.Windows.Forms;

namespace TestNotifyIcon
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private bool _fClose = false;

private void button1_Click(object sender, EventArgs e)
{
_fClose = true;
Application.ExitThread();
}

protected override void OnFormClosing(FormClosingEventArgs e)
{
if (!_fClose)
{
Hide();
e.Cancel = true;
return;
}

base.OnFormClosing(e);
}

private void notifyIcon1_Click(object sender, EventArgs e)
{
Show();
}
}
}


// Form1.Designer.cs
namespace TestNotifyIcon
{
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;

/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be
disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}

#region Windows Form Designer generated code

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
System.ComponentModel.ComponentResourceManager resources =
new System.ComponentModel.ComponentResourceManager(typeof(Form1));
this.button1 = new System.Windows.Forms.Button();
this.notifyIcon1 = new
System.Windows.Forms.NotifyIcon(this.components);
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(13, 13);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 0;
this.button1.Text = "Quit";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new
System.EventHandler(this.button1_Click);
//
// notifyIcon1
//
this.notifyIcon1.Icon =
((System.Drawing.Icon)(resources.GetObject("notifyIcon1.Icon")));
this.notifyIcon1.Text = "TestNotifyIcon App";
this.notifyIcon1.Visible = true;
this.notifyIcon1.Click += new
System.EventHandler(this.notifyIcon1_Click);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(284, 262);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);

}

#endregion

private System.Windows.Forms.Button button1;
private System.Windows.Forms.NotifyIcon notifyIcon1;
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top