界面渐变设计
添加以下代码:
#region 窗体变色效果
//重写OnPaint函数
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
//获取画布
Graphics g = e.Graphics;
//Rectangle rec = new Rectangle(new Point(0, 0), new Size(this.Width, this.Height));
//获取区域的大小
Rectangle rec = new Rectangle(0, 0, this.Width, this.Height);
//设置笔刷,LinearGradientMode设置笔刷的渐变方向
LinearGradientBrush brush = new LinearGradientBrush(rec, Color.FromArgb(225, 101, 127), Color.FromArgb(93, 127, 128),LinearGradientMode.Vertical);
//填充区域
g.FillRectangle(brush, rec);
}
#endregion
效果展示:
实现无边框拖动
在边框移动的过程中,控件的背景颜色和窗体的背景颜色会有干扰,要将控件的BackColor属性设置为Transparent。
#region 界面拖动
private Point mPoint;
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
mPoint = e.Location;
}
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
this.Location = new Point(this.Location.X + e.X - mPoint.X, this.Location.Y + e.Y - mPoint.Y);
}
}
#endregion
关闭的淡出效果
public Form1()
{
InitializeComponent();
CloseTimer.Interval = 10;
CloseTimer.Tick += CloseTimer_Tick;
}
private void CloseTimer_Tick(object sender, EventArgs e)
{
if (this.Opacity >= 0.025)
{
this.Opacity -= 0.025;
}
else
{
this.CloseTimer.Enabled = false;
this.Close();
}
}
#region 淡出效果
private System.Windows.Forms.Timer CloseTimer = new System.Windows.Forms.Timer();
private void button2_Click(object sender, EventArgs e)
{
CloseTimer.Enabled = true;
}
#endregion
基于socket实现与PLC服务器通信
//创建Socket
private Socket tcpClient;
private CancellationTokenSource cts = new CancellationTokenSource();
private void btn_Connect_Click(object sender, EventArgs e)
{
if (this.btn_Connect.Text == "连接")
{
tcpClient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
EndPoint EP = new IPEndPoint(IPAddress.Parse(this.txt_IP.Text), int.Parse(this.txt_Port.Text));
try
{
tcpClient.Connect(EP);
}
catch (Exception ex)
{
MessageBox.Show("连接失败:" + ex.Message);
return;
}
MessageBox.Show("连接成功");
this.btn_Connect.Text = "断开";
//Task.Run(new Action)(() =>
//{
// GetPLCValue();
//}));
}
else
{
tcpClient?.Close();
this.btn_Connect.Text = "连接";
}
}
服务器: