⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 form1.cs

📁 是一个托盘程序的代码,简单明了, 易用易懂
💻 CS
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;




namespace notifyicon
{
	/// <summary>
	/// Form1 的摘要说明。
	/// </summary>
	public class Form1 : System.Windows.Forms.Form
	{
		private System.ComponentModel.IContainer components;
		
		private Icon mNetTrayIcon = new Icon ( "App.ico" ) ;
		private NotifyIcon TrayIcon ;
		private System.Windows.Forms.Panel panel1;
		private System.Windows.Forms.LinkLabel linkLabel1;
		private System.Windows.Forms.ImageList imageList1;
		private System.Windows.Forms.PictureBox pictureBox1;
		private System.Windows.Forms.PictureBox pictureBox2;
		private System.Windows.Forms.ToolTip toolTip1;
		private System.Windows.Forms.PictureBox pictureBox3;
		private System.Windows.Forms.Button button1;
		private System.Windows.Forms.Button button2;
		private ContextMenu notifyiconMnu ;

		public Form1()
		{
			//
			// Windows 窗体设计器支持所必需的
			//

			//InitializeComponent();
			//初始化窗体中使用到的组件
			InitializeComponent ( ) ;
			//初始化托盘程序的各个要素
			 Initializenotifyicon ( ) ;

			//
			// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
			//
			
		}
		private void Initializenotifyicon ( ) 
		{
			///设定托盘程序的各个属性
			TrayIcon = new NotifyIcon ( ) ;
			TrayIcon.Icon = mNetTrayIcon ;
			TrayIcon.Text = "用Visual C#做托盘程序" + "\n" + "作者:马金虎于2001.12.08" ;
			TrayIcon.Visible = true ; 
			TrayIcon.Click += new System.EventHandler ( this.click ) ;

			///定义一个MenuItem数组,并把此数组同时赋值给ContextMenu对象
			MenuItem [ ] mnuItms = new MenuItem [ 3 ] ;
			mnuItms [ 0 ] = new MenuItem ( ) ;
			mnuItms [ 0 ] .Text = "用Visual C#做托盘程序!" ;
			mnuItms [ 0 ] .Click += new System.EventHandler ( this.showmessage ) ;

			mnuItms [ 1 ] = new MenuItem ( "-" ) ;

			mnuItms [ 2 ] = new MenuItem ( ) ;
			mnuItms [ 2 ] .Text = "退出系统" ;
			mnuItms [ 2 ] .Click += new System.EventHandler ( this.ExitSelect ) ;
			mnuItms [ 2 ] .DefaultItem = true ;
			

			notifyiconMnu = new ContextMenu ( mnuItms ) ;
			TrayIcon.ContextMenu = notifyiconMnu ;
			///为托盘程序加入设定好的ContextMenu对象
       }

		public void testnotifyicon(string temp,string fromuserid){
			Form2 other=new Form2();
			Icon mtempIcon = new Icon (temp ) ;
			NotifyIcon fromuserNotifyIcon=new NotifyIcon();
			fromuserNotifyIcon.Icon= mtempIcon;
			fromuserNotifyIcon.Visible=true;
			fromuserNotifyIcon.Text=fromuserid;
			fromuserNotifyIcon.Icon=Icon.FromHandle()
			
			fromuserNotifyIcon.Click += new System.EventHandler ( other.testclick ) ;

		
		}
		public void testclick ( object sender , System.EventArgs e )
		{
			 		string temp=((NotifyIcon)sender).Text;
 			 MessageBox.Show (temp ) ;
		}
		public void click ( object sender , System.EventArgs e )
		{
			 this.ShowInTaskbar = true ;
			//Form2 dd=new Form2();
			//dd.Show();
			
 			//MessageBox.Show ( "Visual C#编写托盘程序中的事件响应" ) ;
		}

		public void showmessage ( object sender , System.EventArgs e )
		{
 			MessageBox.Show ( "你点击了菜单的第一个选项" ) ;
			 this.ShowInTaskbar = false ;
			this.WindowState=System.Windows.Forms.FormWindowState.Minimized;
		}

		public void ExitSelect ( object sender , System.EventArgs e )
		{
 			///隐藏托盘程序中的图标
 			TrayIcon.Visible = false ;
 			///关闭系统
 			this.Close ( ) ;
		}
		private void InitializeComponent ( )
		{
			this.components = new System.ComponentModel.Container();
			this.panel1 = new System.Windows.Forms.Panel();
			this.linkLabel1 = new System.Windows.Forms.LinkLabel();
			this.pictureBox3 = new System.Windows.Forms.PictureBox();
			this.imageList1 = new System.Windows.Forms.ImageList(this.components);
			this.pictureBox1 = new System.Windows.Forms.PictureBox();
			this.pictureBox2 = new System.Windows.Forms.PictureBox();
			this.toolTip1 = new System.Windows.Forms.ToolTip(this.components);
			this.button1 = new System.Windows.Forms.Button();
			this.button2 = new System.Windows.Forms.Button();
			this.panel1.SuspendLayout();
			this.SuspendLayout();
			// 
			// panel1
			// 
			this.panel1.BackColor = System.Drawing.SystemColors.ActiveCaptionText;
			this.panel1.Controls.Add(this.linkLabel1);
			this.panel1.Controls.Add(this.pictureBox3);
			this.panel1.Location = new System.Drawing.Point(144, 48);
			this.panel1.Name = "panel1";
			this.panel1.Size = new System.Drawing.Size(232, 104);
			this.panel1.TabIndex = 0;
			this.panel1.Visible = false;
			this.panel1.MouseLeave += new System.EventHandler(this.panel1_MouseLeave);
			// 
			// linkLabel1
			// 
			this.linkLabel1.Location = new System.Drawing.Point(24, 16);
			this.linkLabel1.Name = "linkLabel1";
			this.linkLabel1.TabIndex = 0;
			this.linkLabel1.TabStop = true;
			this.linkLabel1.Text = "linkLabel1";
			this.linkLabel1.LinkClicked += new System.Windows.Forms.LinkLabelLinkClickedEventHandler(this.linkLabel1_LinkClicked);
			// 
			// pictureBox3
			// 
			this.pictureBox3.BackColor = System.Drawing.SystemColors.ActiveBorder;
			this.pictureBox3.Location = new System.Drawing.Point(8, 16);
			this.pictureBox3.Name = "pictureBox3";
			this.pictureBox3.Size = new System.Drawing.Size(208, 72);
			this.pictureBox3.TabIndex = 4;
			this.pictureBox3.TabStop = false;
			// 
			// imageList1
			// 
			this.imageList1.ImageSize = new System.Drawing.Size(16, 16);
			this.imageList1.TransparentColor = System.Drawing.Color.Transparent;
			// 
			// pictureBox1
			// 
			this.pictureBox1.BackColor = System.Drawing.SystemColors.ActiveBorder;
			this.pictureBox1.Location = new System.Drawing.Point(16, 24);
			this.pictureBox1.Name = "pictureBox1";
			this.pictureBox1.Size = new System.Drawing.Size(64, 56);
			this.pictureBox1.TabIndex = 1;
			this.pictureBox1.TabStop = false;
			this.pictureBox1.MouseHover += new System.EventHandler(this.pictureBox1_MouseHover);
			// 
			// pictureBox2
			// 
			this.pictureBox2.BackColor = System.Drawing.SystemColors.ActiveCaption;
			this.pictureBox2.Cursor = System.Windows.Forms.Cursors.AppStarting;
			this.pictureBox2.Location = new System.Drawing.Point(16, 128);
			this.pictureBox2.Name = "pictureBox2";
			this.pictureBox2.Size = new System.Drawing.Size(64, 48);
			this.pictureBox2.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
			this.pictureBox2.TabIndex = 2;
			this.pictureBox2.TabStop = false;
			// 
			// toolTip1
			// 
			this.toolTip1.ShowAlways = true;
			// 
			// button1
			// 
			this.button1.Location = new System.Drawing.Point(256, 184);
			this.button1.Name = "button1";
			this.button1.TabIndex = 3;
			this.button1.Text = "button1";
			this.button1.Click += new System.EventHandler(this.button1_Click);
			// 
			// button2
			// 
			this.button2.Location = new System.Drawing.Point(160, 184);
			this.button2.Name = "button2";
			this.button2.Size = new System.Drawing.Size(72, 23);
			this.button2.TabIndex = 4;
			this.button2.Text = "button2";
			this.button2.Click += new System.EventHandler(this.button2_Click);
			// 
			// Form1
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
			this.BackColor = System.Drawing.SystemColors.Control;
			this.ClientSize = new System.Drawing.Size(384, 229);
			this.ControlBox = false;
			this.Controls.Add(this.button2);
			this.Controls.Add(this.button1);
			this.Controls.Add(this.pictureBox2);
			this.Controls.Add(this.panel1);
			this.Controls.Add(this.pictureBox1);
			this.MaximizeBox = false;
			this.MinimizeBox = false;
			this.Name = "Form1";
			this.ShowInTaskbar = false;
			this.Text = "用Visual C#做托盘程序!";
			this.TransparencyKey = System.Drawing.Color.FromArgb(((System.Byte)(128)), ((System.Byte)(64)), ((System.Byte)(0)));
			this.WindowState = System.Windows.Forms.FormWindowState.Minimized;
			this.panel1.ResumeLayout(false);
			this.ResumeLayout(false);

		}


		/// <summary>
		/// 清理所有正在使用的资源。
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		

		/// <summary>
		/// 应用程序的主入口点。
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new Form1());
		}

		private void linkLabel1_LinkClicked(object sender, System.Windows.Forms.LinkLabelLinkClickedEventArgs e)
		{
		
		}

		private void pictureBox1_MouseHover(object sender, System.EventArgs e)
		{
			if(this.panel1.Visible==false)
			{
				this.panel1.Visible=true;
			}
			//if(this.panel1.Visible==false)
			//{
			//	this.panel1.Visible=true;
		//	}

		}

		private void panel1_MouseLeave(object sender, System.EventArgs e)
		{
			this.panel1.Visible=false;
		
		}

		private void button1_Click(object sender, System.EventArgs e)
		{
			for(int i=1;i<50;i++)
			{
				testnotifyicon("test1.ico","iloveyou"+i.ToString());
			}
		}

		private void button2_Click(object sender, System.EventArgs e)
		{
			System.IO.MemoryStream mStream = new System.IO.MemoryStream();///创建内存流 
		}
		/***************/
		//ilFrendIcon.Images[i_ImageIndex].Save(mStream, System.Drawing.Imaging.ImageFormat.Gif); 
		//m_trayIcon.Icon = Icon.FromHandle(new Bitmap(mStream).GetHicon());
		//6803010 
		//m_bFlag = true;



	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -