Frm_Main.cs
View Code
1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Linq; 7 using System.Text; 8 using System.Windows.Forms; 9 using System.Net.Sockets; 10 using System.Threading; 11 using System.IO; 12 using System.Net; 13 namespace DynamicTaskStock 14 { 15 public partial class Frm_Main : Form 16 { 17 public Frm_Main() 18 { 19 InitializeComponent(); 20 } 21 private Thread td;//创建一个线程 22 private TcpListener tcpListener;//实例化侦听对象 23 string message = "";//记录传输的内容 24 private void StartListen() 25 { 26 tcpListener = new TcpListener(888);//创建TcpListener实例 27 tcpListener.Start();//开始监听 28 while (true) 29 { 30 TcpClient tclient = tcpListener.AcceptTcpClient();//接受连接请求 31 NetworkStream nstream = tclient.GetStream();//获取数据流 32 byte[] mbyte = new byte[1024];//建立缓存 33 int i = nstream.Read(mbyte, 0, mbyte.Length);//将数据流写入缓存 34 message = Encoding.Default.GetString(mbyte, 0, i);//获取传输的内容 35 } 36 } 37 private void Frm_Main_Load(object sender, EventArgs e) 38 { 39 td = new Thread(new ThreadStart(this.StartListen));//通过线程调用StartListen方法 40 td.Start();//开始运行线程 41 } 42 43 private void Frm_Main_FormClosed(object sender, FormClosedEventArgs e) 44 { 45 if (this.tcpListener != null) 46 { 47 tcpListener.Stop();//停止侦听对象 48 } 49 if (td != null) 50 { 51 if (td.ThreadState == ThreadState.Running)//判断线程是否运行 52 { 53 td.Abort();//终止线程 54 } 55 } 56 } 57 58 private void button1_Click(object sender, EventArgs e) 59 { 60 try 61 { 62 IPAddress[] ip = Dns.GetHostAddresses(Dns.GetHostName());//获取本机地址 63 string message = "你好兄弟";//传输的内容 64 TcpClient client = new TcpClient(txtAdd.Text, 888);//创建TcpClient实例 65 NetworkStream netstream = client.GetStream();//创建NetworkStream实例 66 StreamWriter wstream = new StreamWriter(netstream, Encoding.Default);//创建StreamWriter实例 67 wstream.Write(message);//将信息写入流 68 wstream.Flush(); 69 wstream.Close();//关闭流 70 client.Close();//关闭TcpClient对象 71 } 72 catch (Exception ex) 73 { 74 MessageBox.Show(ex.Message); 75 } 76 } 77 78 bool k = true;//一个标记,用于控制图标闪动 79 private void timer1_Tick(object sender, EventArgs e) 80 { 81 if (message.Length > 0)//如果网络中传输了数据 82 { 83 if (k)//k为true时 84 { 85 notifyIcon1.Icon = Properties.Resources._1;//托盘图标为1 86 k = false;//设k为false 87 } 88 else//k为false时 89 { 90 notifyIcon1.Icon = Properties.Resources._2;//图盘图标为2,透明的图标 91 k = true;//k为true 92 } 93 } 94 } 95 96 private void button2_Click(object sender, EventArgs e) 97 { 98 message = "";//清空传输内容 99 notifyIcon1.Icon = Properties.Resources._3;//初始化显示的图标 100 } 101 } 102 }
Frm_Main.designer.cs
View Code
1 namespace DynamicTaskStock 2 { 3 partial class Frm_Main 4 { 5 ///6 /// 必需的设计器变量。 7 /// 8 private System.ComponentModel.IContainer components = null; 9 10 ///11 /// 清理所有正在使用的资源。 12 /// 13 /// 如果应释放托管资源,为 true;否则为 false。 14 protected override void Dispose(bool disposing) 15 { 16 if (disposing && (components != null)) 17 { 18 components.Dispose(); 19 } 20 base.Dispose(disposing); 21 } 22 23 #region Windows 窗体设计器生成的代码 24 25 ///26 /// 设计器支持所需的方法 - 不要 27 /// 使用代码编辑器修改此方法的内容。 28 /// 29 private void InitializeComponent() 30 { 31 this.components = new System.ComponentModel.Container(); 32 System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Frm_Main)); 33 this.notifyIcon1 = new System.Windows.Forms.NotifyIcon(this.components); 34 this.timer1 = new System.Windows.Forms.Timer(this.components); 35 this.txtAdd = new System.Windows.Forms.TextBox(); 36 this.button1 = new System.Windows.Forms.Button(); 37 this.label1 = new System.Windows.Forms.Label(); 38 this.groupBox1 = new System.Windows.Forms.GroupBox(); 39 this.button2 = new System.Windows.Forms.Button(); 40 this.groupBox1.SuspendLayout(); 41 this.SuspendLayout(); 42 // 43 // notifyIcon1 44 // 45 this.notifyIcon1.Icon = ((System.Drawing.Icon)(resources.GetObject("notifyIcon1.Icon"))); 46 this.notifyIcon1.Visible = true; 47 // 48 // timer1 49 // 50 this.timer1.Enabled = true; 51 this.timer1.Interval = 500; 52 this.timer1.Tick += new System.EventHandler(this.timer1_Tick); 53 // 54 // txtAdd 55 // 56 this.txtAdd.Location = new System.Drawing.Point(137, 25); 57 this.txtAdd.Name = "txtAdd"; 58 this.txtAdd.Size = new System.Drawing.Size(193, 21); 59 this.txtAdd.TabIndex = 0; 60 // 61 // button1 62 // 63 this.button1.Location = new System.Drawing.Point(174, 52); 64 this.button1.Name = "button1"; 65 this.button1.Size = new System.Drawing.Size(75, 23); 66 this.button1.TabIndex = 1; 67 this.button1.Text = "发送消息"; 68 this.button1.UseVisualStyleBackColor = true; 69 this.button1.Click += new System.EventHandler(this.button1_Click); 70 // 71 // label1 72 // 73 this.label1.AutoSize = true; 74 this.label1.Location = new System.Drawing.Point(18, 28); 75 this.label1.Name = "label1"; 76 this.label1.Size = new System.Drawing.Size(113, 12); 77 this.label1.TabIndex = 2; 78 this.label1.Text = "输入对方主机地址:"; 79 // 80 // groupBox1 81 // 82 this.groupBox1.Controls.Add(this.button2); 83 this.groupBox1.Controls.Add(this.button1); 84 this.groupBox1.Controls.Add(this.label1); 85 this.groupBox1.Controls.Add(this.txtAdd); 86 this.groupBox1.Location = new System.Drawing.Point(8, 7); 87 this.groupBox1.Name = "groupBox1"; 88 this.groupBox1.Size = new System.Drawing.Size(347, 88); 89 this.groupBox1.TabIndex = 3; 90 this.groupBox1.TabStop = false; 91 this.groupBox1.Text = "发送消息"; 92 // 93 // button2 94 // 95 this.button2.Location = new System.Drawing.Point(255, 52); 96 this.button2.Name = "button2"; 97 this.button2.Size = new System.Drawing.Size(75, 23); 98 this.button2.TabIndex = 3; 99 this.button2.Text = "停止闪动"; 100 this.button2.UseVisualStyleBackColor = true; 101 this.button2.Click += new System.EventHandler(this.button2_Click); 102 // 103 // Frm_Main 104 // 105 this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); 106 this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; 107 this.ClientSize = new System.Drawing.Size(363, 100); 108 this.Controls.Add(this.groupBox1); 109 this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle; 110 this.MaximizeBox = false; 111 this.MinimizeBox = false; 112 this.Name = "Frm_Main"; 113 this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; 114 this.Text = "实现动态系统托盘图标"; 115 this.Load += new System.EventHandler(this.Frm_Main_Load); 116 this.FormClosed += new System.Windows.Forms.FormClosedEventHandler(this.Frm_Main_FormClosed); 117 this.groupBox1.ResumeLayout(false); 118 this.groupBox1.PerformLayout(); 119 this.ResumeLayout(false); 120 121 } 122 123 #endregion 124 125 private System.Windows.Forms.NotifyIcon notifyIcon1; 126 private System.Windows.Forms.Timer timer1; 127 private System.Windows.Forms.TextBox txtAdd; 128 private System.Windows.Forms.Button button1; 129 private System.Windows.Forms.Label label1; 130 private System.Windows.Forms.GroupBox groupBox1; 131 private System.Windows.Forms.Button button2; 132 } 133 }
作者: 出处: 关于作者:专注于.Net、WCF和移动互联网开发。 本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,如有问题,可以通过ynbt_wang@163.com联系我,非常感谢。 。