NotifyIcon 构造函数

定义

初始化 NotifyIcon 类的新实例。

重载

NotifyIcon()

初始化 NotifyIcon 类的新实例。

NotifyIcon(IContainer)

使用指定的容器初始化 NotifyIcon 类的新实例。

NotifyIcon()

Source:
NotifyIcon.cs
Source:
NotifyIcon.cs
Source:
NotifyIcon.cs

初始化 NotifyIcon 类的新实例。

C#
public NotifyIcon();

注解

NotifyIcon创建新时, Visible 属性设置为 false。 必须将 属性设置为 Visibletrue 才能使用 NotifyIcon 创建的 。 此实例将一直存在,直到其容器将其释放到垃圾回收。

适用于

.NET Framework 4.8.1 和其他版本
产品 版本
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10

NotifyIcon(IContainer)

Source:
NotifyIcon.cs
Source:
NotifyIcon.cs
Source:
NotifyIcon.cs

使用指定的容器初始化 NotifyIcon 类的新实例。

C#
public NotifyIcon(System.ComponentModel.IContainer container);

参数

container
IContainer

表示 NotifyIcon 控件的容器的 IContainer

示例

下面的代码示例演示如何使用 NotifyIcon 类在通知区域中显示应用程序的图标。 该示例演示如何设置 IconContextMenuTextVisible 属性以及处理 DoubleClick 事件。 ContextMenu具有 Exit 项的 将NotifyIcon.ContextMenu分配给 属性,该属性允许用户关闭应用程序。 DoubleClick事件发生时,通过调用 Form.Activate 方法激活应用程序窗体。

C#
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();
    }
}

注解

NotifyIcon创建新时, Visible 属性设置为 false。 必须将 属性设置为 Visibletrue 才能使用 NotifyIcon 创建的 。 此实例将一直存在,直到其容器将其释放到垃圾回收。

适用于

.NET Framework 4.8.1 和其他版本
产品 版本
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10