农场设备管理代码(2)

using Dias_Interface;

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.IO;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Windows.Forms;

namespace Dias_Farm

{

public partial class UC_Device_Data : UserControl

{

Dias_Interface.DeviceInfo DEV = null;

Timer t = new Timer() { Interval = 1000,Enabled=false };

public Dias_Interface.DeviceInfo Device

{

get { return DEV; }

set

{

if (value != null && value != DEV) DEV = value;

}

}

public bool IsRuning

{

get

{

return t.Enabled;

}

set

{

t.Enabled = value;

}

}

public UC_Device_Data()

{

InitializeComponent();

this.SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.UserPaint | ControlStyles.ResizeRedraw, true);

this.Paint += UC_Device_Data_Paint;

t.Tick += (sender, e) =>

{

this.Refresh();

};

this.Disposed += (sender, e) =>

{

t.Enabled = false;

t.Dispose();

GC.Collect();

};

this.MouseClick += (sender, e) =>

{

OnDeviceClick?.Invoke(this, new Point(e.X, e.Y), 0);

};

this.MouseDoubleClick += (sender, e) =>

{

OnDeviceClick?.Invoke(this, new Point(e.X, e.Y), 1);

};

}

public event Action<UC_Device_Data,Point, int> OnDeviceClick;

private void UC_Device_Data_Paint(object sender, PaintEventArgs e)

{

if (DEV == null)

{

e.Graphics.DrawString($"农场设备", this.Font, new SolidBrush(this.ForeColor),

new Rectangle(Point.Empty, this.ClientSize),

new StringFormat() { LineAlignment = StringAlignment.Center, Alignment = StringAlignment.Center });

return;

}

Graphics g=e.Graphics;

switch (DEV.Type)

{

case Dias_Interface.DeviceType.Data:

{

DrawDataControls(g,DEV);

}

break;

case DeviceType.Control:

{

DrawControlControls(g,DEV);

}

break;

case DeviceType.Monitor:

{

DrawMonitorControls(g,DEV);

}

break;

default:

{

break;

}

}

}

private void DrawMonitorControls(Graphics g, IDEVICE e)

{

Image img = null;

try

{

img = Image.FromStream(new MemoryStream(e.ByteValue));

}

catch { img = null; }

if (img != null)

{

g.DrawImage(img, new Rectangle(Point.Empty, this.ClientSize),new Rectangle(Point.Empty,img.Size),GraphicsUnit.Pixel);

img.Dispose();

img = null;

}

}

private void DrawControlControls(Graphics g, IDEVICE e)

{

g.DrawString($"{e.Name}【{(e.Value>=1?"开":"关")}】", this.Font, new SolidBrush(this.ForeColor), new Rectangle(Point.Empty, this.ClientSize),

new StringFormat() { LineAlignment = StringAlignment.Center, Alignment = StringAlignment.Center });

}

private void DrawDataControls(Graphics g, IDEVICE e)

{

g.DrawString($"{e.Name}【{e.Value.ToString("0.00")}】{e.Set.Get<string>("Unit")}", this.Font, new SolidBrush(this.ForeColor), new Rectangle(Point.Empty, this.ClientSize),

new StringFormat() { LineAlignment = StringAlignment.Center, Alignment = StringAlignment.Center });

}

}

}

相关推荐
周GZ3 小时前
简单讲解Java中静态方法与 实例方法
java·开发语言
唐青枫4 小时前
别再把命令行参数写成一团:C#.NET System.CommandLine 实战详解
c#·.net
大白同学4214 小时前
初识Qt——创建第一个Qt项目
开发语言·qt
祈禾4 小时前
计算机组成原理之编译、汇编、链接、解释
开发语言·汇编·笔记·学习
hez20104 小时前
让 .NET 11 的泛型虚方法变得更快!
c#·.net·.net core·clr·compiler
吴声子夜歌4 小时前
正则指引——Ruby
开发语言·正则表达式·ruby
nnerddboy4 小时前
Rust教程06:ESP32-rust环境搭建
开发语言·后端·rust
nnerddboy5 小时前
Rust教程03:函数,控制流与所有权
开发语言·后端·rust
linux修理工5 小时前
内存占用 99% 且 Java 程序较多的优化建议
java·开发语言