NotifyIcon (Clase)
Actualización: noviembre 2007
Especifica un componente que crea un icono en el área de notificación. Esta clase no se puede heredar.
Ensamblado: System.Windows.Forms (en System.Windows.Forms.dll)
Los iconos del área de notificación son accesos directos a procesos que se ejecutan en segundo plano como, por ejemplo, un programa de protección antivirus o un control de volumen. Estos procesos no incluyen sus propias interfaces de usuario. La clase NotifyIcon proporciona una manera de programar en esta funcionalidad. La propiedad Icon define el icono que aparece en el área de notificación. Los menús emergentes de un icono se definen con la propiedad ContextMenu. La propiedad Text asigna texto de información sobre herramientas. Para que el icono aparezca en el área de notificación, la propiedad Visible debe establecerse en true.
En el ejemplo de código siguiente se muestra la forma de utilizar la clase NotifyIcon para que se muestre un icono en el área de notificación para una aplicación. En el siguiente ejemplo se muestra la forma de establecer las propiedades Icon, ContextMenu, Text y Visible y la forma de administrar el evento DoubleClick. Se asigna un control ContextMenu con un elemento Exit a la propiedad NotifyIcon.ContextMenu que permite al usuario cerrar la aplicación. Cuando se produce el evento DoubleClick, se activa el formulario de la aplicación mediante una llamada al método Form.Activate.
using System; using System.Drawing; using System.Windows.Forms; public class Form1 : System.Windows.Forms.Form { private System.Windows.Forms.NotifyIcon notifyIcon1; private System.Windows.Forms.ContextMenu contextMenu1; private System.Windows.Forms.MenuItem menuItem1; private System.ComponentModel.IContainer components; [STAThread] static void Main() { Application.Run(new Form1()); } public Form1() { this.components = new System.ComponentModel.Container(); this.contextMenu1 = new System.Windows.Forms.ContextMenu(); this.menuItem1 = new System.Windows.Forms.MenuItem(); // Initialize contextMenu1 this.contextMenu1.MenuItems.AddRange( new System.Windows.Forms.MenuItem[] {this.menuItem1}); // Initialize menuItem1 this.menuItem1.Index = 0; this.menuItem1.Text = "E&xit"; this.menuItem1.Click += new System.EventHandler(this.menuItem1_Click); // Set up how the form should be displayed. this.ClientSize = new System.Drawing.Size(292, 266); this.Text = "Notify Icon Example"; // Create the NotifyIcon. this.notifyIcon1 = new System.Windows.Forms.NotifyIcon(this.components); // The Icon property sets the icon that will appear // in the systray for this application. notifyIcon1.Icon = new Icon("appicon.ico"); // The ContextMenu property sets the menu that will // appear when the systray icon is right clicked. notifyIcon1.ContextMenu = this.contextMenu1; // The Text property sets the text that will be displayed, // in a tooltip, when the mouse hovers over the systray icon. notifyIcon1.Text = "Form1 (NotifyIcon example)"; notifyIcon1.Visible = true; // Handle the DoubleClick event to activate the form. notifyIcon1.DoubleClick += new System.EventHandler(this.notifyIcon1_DoubleClick); } protected override void Dispose( bool disposing ) { // Clean up any components being used. if( disposing ) if (components != null) components.Dispose(); base.Dispose( disposing ); } private void notifyIcon1_DoubleClick(object Sender, EventArgs e) { // Show the form when the user double clicks on the notify icon. // Set the WindowState to normal if the form is minimized. if (this.WindowState == FormWindowState.Minimized) this.WindowState = FormWindowState.Normal; // Activate the form. this.Activate(); } private void menuItem1_Click(object Sender, EventArgs e) { // Close the form, which closes the application. this.Close(); } }
import System.*;
import System.Drawing.*;
import System.Windows.Forms.*;
public class Form1 extends System.Windows.Forms.Form
{
private System.Windows.Forms.NotifyIcon notifyIcon1;
private System.Windows.Forms.ContextMenu contextMenu1;
private System.Windows.Forms.MenuItem menuItem1;
private System.ComponentModel.IContainer components;
/** @attribute STAThread()
*/
public static void main(String[] args)
{
Application.Run(new Form1());
} //main
public Form1()
{
this.components = new System.ComponentModel.Container();
this.contextMenu1 = new System.Windows.Forms.ContextMenu();
this.menuItem1 = new System.Windows.Forms.MenuItem();
// Initialize contextMenu1
this.contextMenu1.get_MenuItems().AddRange(
new System.Windows.Forms.MenuItem[] { this.menuItem1 });
// Initialize menuItem1
this.menuItem1.set_Index(0);
this.menuItem1.set_Text("E&xit");
this.menuItem1.add_Click(new System.EventHandler(this.menuItem1_Click));
// Set up how the form should be displayed.
this.set_ClientSize(new System.Drawing.Size(292, 266));
this.set_Text("Notify Icon Example");
// Create the NotifyIcon.
this.notifyIcon1 = new System.Windows.Forms.NotifyIcon(this.components);
// The Icon property sets the icon that will appear
// in the systray for this application.
notifyIcon1.set_Icon(new Icon("appicon.ico"));
// The ContextMenu property sets the menu that will
// appear when the systray icon is right clicked.
notifyIcon1.set_ContextMenu(this.contextMenu1);
// The Text property sets the text that will be displayed,
// in a tooltip, when the mouse hovers over the systray icon.
notifyIcon1.set_Text("Form1 (NotifyIcon example)");
notifyIcon1.set_Visible(true);
// Handle the DoubleClick event to activate the form.
notifyIcon1.add_DoubleClick(
new System.EventHandler(this.notifyIcon1_DoubleClick));
} //Form1
protected void Dispose(boolean disposing)
{
// Clean up any components being used.
if (disposing) {
if (components != null) {
components.Dispose();
}
}
super.Dispose(disposing);
} //Dispose
private void notifyIcon1_DoubleClick(Object Sender, EventArgs e)
{
// Show the form when the user double clicks on the notify icon.
// Set the WindowState to normal if the form is minimized.
if (this.get_WindowState().Equals(FormWindowState.Minimized)) {
this.set_WindowState(FormWindowState.Normal);
}
// Activate the form.
this.Activate();
} //notifyIcon1_DoubleClick
private void menuItem1_Click(Object Sender, EventArgs e)
{
// Close the form, which closes the application.
this.Close();
} //menuItem1_Click
} //Form1
-
UIPermission
para crear el componente NotifyIcon. Enumeración asociada: UIPermissionWindow.AllWindows
System.MarshalByRefObject
System.ComponentModel.Component
System.Windows.Forms.NotifyIcon
Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98
.NET Framework y .NET Compact Framework no admiten todas las versiones de cada plataforma. Para obtener una lista de las versiones compatibles, vea Requisitos de sistema de .NET Framework.