Csharp/CSharp Tutorial/GUI Windows Forms/NotifyIcon — различия между версиями

Материал из .Net Framework эксперт
Перейти к: навигация, поиск
м (1 версия)
 
м (1 версия)
 
(нет различий)

Текущая версия на 12:15, 26 мая 2010

NotifyIcon Sample

using System;        
using System.Drawing;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
namespace NotifyIconExample
{
  /// <summary>
  /// Summary description for NotifyIconForm.
  /// </summary>
  public class NotifyIconForm : System.Windows.Forms.Form
  {
    private System.Windows.Forms.NotifyIcon notifyIcon1;
    private System.Windows.Forms.Button button1;
    private System.ruponentModel.IContainer components;
    public NotifyIconForm()
    {
      //
      // Required for Windows Form Designer support
      //
      InitializeComponent();
      //
      // TODO: Add any constructor code after InitializeComponent call
      //
      this.notifyIcon1.DoubleClick += new System.EventHandler(this.notifyIcon1_DoubleClick);
    }
    /// <summary>
    /// Clean up any resources being used.
    /// </summary>
    protected override void Dispose( bool disposing )
    {
      if( disposing )
      {
        if (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.ruponents = new System.ruponentModel.Container();
      this.notifyIcon1 = new System.Windows.Forms.NotifyIcon(this.ruponents);
      this.button1 = new System.Windows.Forms.Button();
      this.SuspendLayout();
      // 
      // notifyIcon1
      // 
      this.notifyIcon1.Icon = new Icon("yourIcon.ico");
      this.notifyIcon1.Text = "Hello from NotifyIconExample";
      // 
      // button1
      // 
      this.button1.Location = new System.Drawing.Point(109, 122);
      this.button1.Name = "button1";
      this.button1.TabIndex = 0;
      this.button1.Text = "Hide in tray";
      this.button1.Click += new System.EventHandler(this.button1_Click);
      // 
      // NotifyIconForm
      // 
      this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
      this.ClientSize = new System.Drawing.Size(292, 266);
      this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                      this.button1});
      this.Name = "NotifyIconForm";
      this.Text = "NotifyIconForm";
      this.ResumeLayout(false);
    }
    #endregion
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main() 
    {
      Application.Run(new NotifyIconForm());
    }
    private void button1_Click(object sender, System.EventArgs e)
    {
      notifyIcon1.Visible = true;
      this.Visible = false;
    }
    private void notifyIcon1_DoubleClick(object sender, System.EventArgs e)
    {
      notifyIcon1.Visible = false;
      this.Visible = true;
    }
  }
}

Use Timer to update NotifyIcon

using System;
using System.Drawing;
using System.Windows.Forms;
public class NotifyIconTimerUpdate : Form
{
    private Icon[] images = new Icon[8];
    int index = 0;
    public NotifyIconTimerUpdate()
    {
        InitializeComponent();
        images[0] = new Icon("1.ico");
        images[1] = new Icon("2.ico");
        images[2] = new Icon("3.ico");
        images[3] = new Icon("4.ico");
        images[4] = new Icon("5.ico");
        images[5] = new Icon("1.ico");
        images[6] = new Icon("2.ico");
        images[7] = new Icon("3.ico");
    }
    private void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
    {
        notifyIcon.Icon = images[index];
        index++;
        if (index > 7) 
           index = 0;
    }
    [STAThread]
    public static void Main(string[] args)
    {
        Application.Run(new NotifyIconTimerUpdate());
    }
    private System.Windows.Forms.NotifyIcon notifyIcon;
    private System.Timers.Timer timer;
    
    private System.ruponentModel.IContainer components = null;
    private void InitializeComponent()
    {
        this.ruponents = new System.ruponentModel.Container();
        this.notifyIcon = new System.Windows.Forms.NotifyIcon(this.ruponents);
        this.timer = new System.Timers.Timer();
        ((System.ruponentModel.ISupportInitialize)(this.timer)).BeginInit();
        this.SuspendLayout();
        // 
        // notifyIcon
        // 
        this.notifyIcon.Text = "notifyIcon1";
        this.notifyIcon.Visible = true;
        // 
        // timer
        // 
        this.timer.Enabled = true;
        this.timer.Interval = 1000;
        this.timer.SynchronizingObject = this;
        this.timer.Elapsed += new System.Timers.ElapsedEventHandler(this.timer_Elapsed);
        // 
        // NotifyIconTimerUpdate
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(340, 174);
        ((System.ruponentModel.ISupportInitialize)(this.timer)).EndInit();
        this.ResumeLayout(false);
    }
}

Use Timer to update User interface: NotifyIcon

using System;
using System.Drawing;
using System.Windows.Forms;
public class NotifyIconTimerUpdate : Form
{
    private Icon[] images = new Icon[8];
    int index = 0;
    public NotifyIconTimerUpdate()
    {
        InitializeComponent();
        images[0] = new Icon("1.ico");
        images[1] = new Icon("2.ico");
        images[2] = new Icon("3.ico");
        images[3] = new Icon("4.ico");
        images[4] = new Icon("5.ico");
        images[5] = new Icon("1.ico");
        images[6] = new Icon("2.ico");
        images[7] = new Icon("3.ico");
    }
    private void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
    {
        notifyIcon.Icon = images[index];
        index++;
        if (index > 7) 
           index = 0;
    }
    [STAThread]
    public static void Main(string[] args)
    {
        Application.Run(new NotifyIconTimerUpdate());
    }
    private System.Windows.Forms.NotifyIcon notifyIcon;
    private System.Timers.Timer timer;
    
    private System.ruponentModel.IContainer components = null;
    private void InitializeComponent()
    {
        this.ruponents = new System.ruponentModel.Container();
        this.notifyIcon = new System.Windows.Forms.NotifyIcon(this.ruponents);
        this.timer = new System.Timers.Timer();
        ((System.ruponentModel.ISupportInitialize)(this.timer)).BeginInit();
        this.SuspendLayout();
        // 
        // notifyIcon
        // 
        this.notifyIcon.Text = "notifyIcon1";
        this.notifyIcon.Visible = true;
        // 
        // timer
        // 
        this.timer.Enabled = true;
        this.timer.Interval = 1000;
        this.timer.SynchronizingObject = this;
        this.timer.Elapsed += new System.Timers.ElapsedEventHandler(this.timer_Elapsed);
        // 
        // NotifyIconTimerUpdate
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(340, 174);
        ((System.ruponentModel.ISupportInitialize)(this.timer)).EndInit();
        this.ResumeLayout(false);
    }
}