網站首頁 編程語言 正文
實踐過程
效果
代碼
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private Thread td;
private TcpListener tcpListener;
string message="";
private void Form1_Load(object sender, EventArgs e)
{
td = new Thread(new ThreadStart(this.StartListen));
td.Start();
}
private void StartListen()
{
tcpListener = new TcpListener(888);
tcpListener.Start();
while (true)
{
TcpClient tclient = tcpListener.AcceptTcpClient(); //接受連接請求
NetworkStream nstream = tclient.GetStream(); //獲取數據流
byte[] mbyte = new byte[1024]; //建立緩存
int i = nstream.Read(mbyte, 0, mbyte.Length); //將數據流寫入緩存
message = Encoding.Default.GetString(mbyte, 0, i);
}
}
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
if (this.tcpListener != null)
{
tcpListener.Stop();
}
if (td != null)
{
if (td.ThreadState == ThreadState.Running)
{
td.Abort();
}
}
}
private void button1_Click(object sender, EventArgs e)
{
try
{
IPAddress[] ip = Dns.GetHostAddresses(Dns.GetHostName());
string message ="你好兄弟";
TcpClient client = new TcpClient(txtAdd.Text, 888);
NetworkStream netstream = client.GetStream();
StreamWriter wstream = new StreamWriter(netstream, Encoding.Default);
wstream.Write(message);
wstream.Flush();
wstream.Close();
client.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
bool k = true;
private void timer1_Tick(object sender, EventArgs e)
{
if (message.Length > 0)
{
if (k)
{
notifyIcon1.Icon = Properties.Resources._1;
k = false;
}
else
{
notifyIcon1.Icon = Properties.Resources._2;
k = true;
}
}
}
private void button2_Click(object sender, EventArgs e)
{
message = "";
notifyIcon1.Icon = Properties.Resources._3;
}
}
partial class Form1
{
/// <summary>
/// 必需的設計器變量。
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// 清理所有正在使用的資源。
/// </summary>
/// <param name="disposing">如果應釋放托管資源,為 true;否則為 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows 窗體設計器生成的代碼
/// <summary>
/// 設計器支持所需的方法 - 不要
/// 使用代碼編輯器修改此方法的內容。
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Form1));
this.notifyIcon1 = new System.Windows.Forms.NotifyIcon(this.components);
this.timer1 = new System.Windows.Forms.Timer(this.components);
this.txtAdd = new System.Windows.Forms.TextBox();
this.button1 = new System.Windows.Forms.Button();
this.label1 = new System.Windows.Forms.Label();
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.button2 = new System.Windows.Forms.Button();
this.groupBox1.SuspendLayout();
this.SuspendLayout();
//
// notifyIcon1
//
this.notifyIcon1.Icon = ((System.Drawing.Icon)(resources.GetObject("notifyIcon1.Icon")));
this.notifyIcon1.Visible = true;
//
// timer1
//
this.timer1.Enabled = true;
this.timer1.Interval = 500;
this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
//
// txtAdd
//
this.txtAdd.Location = new System.Drawing.Point(145, 36);
this.txtAdd.Name = "txtAdd";
this.txtAdd.Size = new System.Drawing.Size(193, 21);
this.txtAdd.TabIndex = 0;
//
// button1
//
this.button1.Location = new System.Drawing.Point(103, 96);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 1;
this.button1.Text = "發送消息";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(26, 39);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(113, 12);
this.label1.TabIndex = 2;
this.label1.Text = "輸入對方主機地址:";
//
// groupBox1
//
this.groupBox1.Controls.Add(this.button2);
this.groupBox1.Controls.Add(this.button1);
this.groupBox1.Controls.Add(this.label1);
this.groupBox1.Controls.Add(this.txtAdd);
this.groupBox1.Location = new System.Drawing.Point(13, 13);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(353, 154);
this.groupBox1.TabIndex = 3;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "發送消息";
//
// button2
//
this.button2.Location = new System.Drawing.Point(184, 96);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(75, 23);
this.button2.TabIndex = 3;
this.button2.Text = "停止閃動";
this.button2.UseVisualStyleBackColor = true;
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(380, 182);
this.Controls.Add(this.groupBox1);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
this.MaximizeBox = false;
this.MinimizeBox = false;
this.Name = "Form1";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "動態托盤圖標";
this.Load += new System.EventHandler(this.Form1_Load);
this.FormClosed += new System.Windows.Forms.FormClosedEventHandler(this.Form1_FormClosed);
this.groupBox1.ResumeLayout(false);
this.groupBox1.PerformLayout();
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.NotifyIcon notifyIcon1;
private System.Windows.Forms.Timer timer1;
private System.Windows.Forms.TextBox txtAdd;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.Button button2;
}
原文鏈接:https://zhima.blog.csdn.net/article/details/128277379
相關推薦
- 2022-04-17 Security前后端分離自定義登錄詳解
- 2022-08-12 Python學習之字典的創建和使用_python
- 2022-07-08 Python數據分析Pandas?Dataframe排序操作_python
- 2022-04-14 Python實現用戶名和密碼登錄_python
- 2022-07-12 解決錯誤:Cannot resolve symbol ‘JdbcTemplate‘
- 2022-04-09 SpringBoot自定義validation注解校驗參數只能為指定的值
- 2022-06-12 C語言詳解float類型在內存中的存儲方式_C 語言
- 2022-11-15 python管理包路徑之pycharm自動解決包路徑注冊_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支