NotifyIcon 類別

定義

指定在通知區域中建立圖示的元件。 此類別無法獲得繼承。

public ref class NotifyIcon sealed : System::ComponentModel::Component
public sealed class NotifyIcon : System.ComponentModel.Component
type NotifyIcon = class
    inherit Component
Public NotInheritable Class NotifyIcon
Inherits Component
繼承

範例

下列程式碼範例示範如何使用 NotifyIcon 類別,在通知區域中顯示應用程式的圖示。 此範例示範如何設定 IconContextMenuText 和 屬性及 Visible 處理 DoubleClick 事件。 ContextMenu具有Exit專案的 ,會指派給 NotifyIcon.ContextMenu 屬性,讓使用者能夠關閉應用程式。 DoubleClick事件發生時,會呼叫 Form.Activate 方法來啟動應用程式表單。

#using <System.dll>
#using <System.Windows.Forms.dll>
#using <System.Drawing.dll>

using namespace System;
using namespace System::Drawing;
using namespace System::Windows::Forms;
public ref class Form1: public System::Windows::Forms::Form
{
private:
   System::Windows::Forms::NotifyIcon^ notifyIcon1;
   System::Windows::Forms::ContextMenu^ contextMenu1;
   System::Windows::Forms::MenuItem^ menuItem1;
   System::ComponentModel::IContainer^ components;

public:
   Form1()
   {
      this->components = gcnew System::ComponentModel::Container;
      this->contextMenu1 = gcnew System::Windows::Forms::ContextMenu;
      this->menuItem1 = gcnew System::Windows::Forms::MenuItem;
      
      // Initialize contextMenu1
      array<System::Windows::Forms::MenuItem^>^temp0 = {this->menuItem1};
      this->contextMenu1->MenuItems->AddRange( temp0 );
      
      // Initialize menuItem1
      this->menuItem1->Index = 0;
      this->menuItem1->Text = "E&xit";
      this->menuItem1->Click += gcnew System::EventHandler( this, &Form1::menuItem1_Click );
      
      // Set up how the form should be displayed.
      this->ClientSize = System::Drawing::Size( 292, 266 );
      this->Text = "Notify Icon Example";
      
      // Create the NotifyIcon.
      this->notifyIcon1 = gcnew System::Windows::Forms::NotifyIcon( this->components );
      
      // The Icon property sets the icon that will appear
      // in the systray for this application.
      notifyIcon1->Icon = gcnew System::Drawing::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 += gcnew System::EventHandler( this, &Form1::notifyIcon1_DoubleClick );
   }

protected:
   ~Form1()
   {
      if ( components != nullptr )
      {
         delete components;
      }
   }

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

   void menuItem1_Click( Object^ /*Sender*/, EventArgs^ /*e*/ )
   {
      
      // Close the form, which closes the application.
      this->Close();
   }

};

[STAThread]
int main()
{
   Application::Run( gcnew Form1 );
}
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();
    }
}
Imports System.Drawing
Imports System.Windows.Forms

Public NotInheritable Class Form1
    Inherits System.Windows.Forms.Form

    Private contextMenu1 As System.Windows.Forms.ContextMenu
    Friend WithEvents menuItem1 As System.Windows.Forms.MenuItem
    Friend WithEvents notifyIcon1 As System.Windows.Forms.NotifyIcon
    Private components As System.ComponentModel.IContainer

    <System.STAThread()> _
    Public Shared Sub Main()
        System.Windows.Forms.Application.Run(New Form1)
    End Sub

    Public Sub New()

        Me.components = New System.ComponentModel.Container
        Me.contextMenu1 = New System.Windows.Forms.ContextMenu
        Me.menuItem1 = New System.Windows.Forms.MenuItem

        ' Initialize contextMenu1
        Me.contextMenu1.MenuItems.AddRange(New System.Windows.Forms.MenuItem() _
                            {Me.menuItem1})

        ' Initialize menuItem1
        Me.menuItem1.Index = 0
        Me.menuItem1.Text = "E&xit"

        ' Set up how the form should be displayed.
        Me.ClientSize = New System.Drawing.Size(292, 266)
        Me.Text = "Notify Icon Example"

        ' Create the NotifyIcon.
        Me.notifyIcon1 = New System.Windows.Forms.NotifyIcon(Me.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 = Me.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
    End Sub
    
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        ' Clean up any components being used.
        If disposing Then
            If (components IsNot Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    Private Sub notifyIcon1_DoubleClick(Sender as object, e as EventArgs) handles notifyIcon1.DoubleClick
        ' Show the form when the user double clicks on the notify icon.

        ' Set the WindowState to normal if the form is minimized.
        if (me.WindowState = FormWindowState.Minimized) then _
            me.WindowState = FormWindowState.Normal

        ' Activate the form.
        me.Activate()
    end sub

    Private Sub menuItem1_Click(Sender as object, e as EventArgs) handles menuItem1.Click
        ' Close the form, which closes the application.
        me.Close()
    end sub

End Class

備註

通知區域中的圖示是電腦背景中執行的處理常式快捷方式,例如病毒防護程式或磁片區控制。 這些程式不會隨附自己的使用者介面。 類別 NotifyIcon 提供在這項功能中程式設計的方法。 屬性 Icon 會定義出現在通知區域中的圖示。 圖示的快顯功能表會以 ContextMenu 屬性定址。 屬性 Text 會指派工具提示文字。 為了讓圖示顯示在通知區域中, Visible 屬性必須設定為 true

建構函式

NotifyIcon()

初始化 NotifyIcon 類別的新執行個體。

NotifyIcon(IContainer)

使用指定的容器,初始化 NotifyIcon 類別的新執行個體。

屬性

BalloonTipIcon

取得或設定與 NotifyIcon 相關聯之氣球提示上顯示的圖示。

BalloonTipText

取得或設定與 NotifyIcon 相關聯之氣球提示的顯示文字。

BalloonTipTitle

取得或設定 NotifyIcon 上顯示的氣球提示標題。

CanRaiseEvents

取得值,指出元件是否能引發事件。

(繼承來源 Component)
Container

取得包含 IContainerComponent

(繼承來源 Component)
ContextMenu

取得或設定圖示的捷徑功能表。

ContextMenuStrip

取得或設定與 NotifyIcon 關聯的捷徑功能表。

DesignMode

取得值,指出 Component 目前是否處於設計模式。

(繼承來源 Component)
Events

取得附加在這個 Component 上的事件處理常式清單。

(繼承來源 Component)
Icon

取得或設定目前的圖示。

Site

取得或設定 ComponentISite

(繼承來源 Component)
Tag

取得或設定包含 NotifyIcon 相關資料的物件。

Text

取得或設定工具提示文字,當滑鼠指標停留在通知區域圖示上時顯示。

Visible

取得或設定值,指出是否在工作列的通知區域中顯示圖示。

方法

CreateObjRef(Type)

建立包含所有相關資訊的物件,這些資訊是產生用來與遠端物件通訊的所需 Proxy。

(繼承來源 MarshalByRefObject)
Dispose()

釋放 Component 所使用的所有資源。

(繼承來源 Component)
Dispose(Boolean)

釋放 Component 所使用的 Unmanaged 資源,並選擇性地釋放 Managed 資源。

(繼承來源 Component)
Equals(Object)

判斷指定的物件是否等於目前的物件。

(繼承來源 Object)
GetHashCode()

做為預設雜湊函式。

(繼承來源 Object)
GetLifetimeService()
已淘汰.

擷取控制這個執行個體存留期 (Lifetime) 原則的目前存留期服務物件。

(繼承來源 MarshalByRefObject)
GetService(Type)

傳回表示 Component 或其 Container 所提供之服務的物件。

(繼承來源 Component)
GetType()

取得目前執行個體的 Type

(繼承來源 Object)
InitializeLifetimeService()
已淘汰.

取得存留期服務物件,以控制這個執行個體的存留期原則。

(繼承來源 MarshalByRefObject)
MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
MemberwiseClone(Boolean)

建立目前 MarshalByRefObject 物件的淺層複本。

(繼承來源 MarshalByRefObject)
ShowBalloonTip(Int32)

在指定時段內,於工作列中顯示氣球提示。

ShowBalloonTip(Int32, String, String, ToolTipIcon)

在指定時段內,於工作列中顯示具有指定標題、文字和圖示的氣球提示。

ToString()

傳回任何包含 Component 名稱的 String。 不應覆寫此方法。

(繼承來源 Component)

事件

BalloonTipClicked

當按一下氣球提示時發生。

BalloonTipClosed

當使用者關閉氣球提示時發生。

BalloonTipShown

螢幕上出現氣球提示時發生。

Click

使用者按一下通知區域中的圖示時發生。

Disposed

Dispose() 方法的呼叫處置元件時,就會發生。

(繼承來源 Component)
DoubleClick

使用者按兩下工作列通知區域中的圖示時發生。

MouseClick

發生於使用者用滑鼠按一下 NotifyIcon 時。

MouseDoubleClick

發生於使用者用滑鼠按兩下 NotifyIcon 時。

MouseDown

發生於指標位於工作列的通知區域之圖示上,使用者按下滑鼠按鈕時。

MouseMove

發生於指標位於工作列的通知區域之圖示上,使用者移動滑鼠時。

MouseUp

發生於指標位於工作列之通知區域的圖示上,使用者放開滑鼠按鈕時。

適用於