由于工作需要,调研过一段时间的工业控制方面的“组态软件”(SCADA)的开发,组态软件常用于自动化工业控制领域,其中包括实时数据采集、数据储存、设备控制和数据展现等功能。其中工控组件的界面展现的实现类似于Windows系统下的各种开发控件,通过各种控件的组装,和硬件协议的集成,就可以实现对相应设备的控制和实时状态的显示。
每个对应的硬件UI展示都可以用一个自定义控件来实现,如下图的一个温度计,就可以使用UserControl来实现。
对应的实现代码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace HMIControls
{
public partial class ThermometerControl : UserControl
{
///
/// 初始化控件
/// 预设绘图方式:双缓冲、支持透明背景色、自定义绘制
///
public ThermometerControl()
{
SetStyle(ControlStyles.AllPaintingInWmPaint, true);
SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
SetStyle(ControlStyles.ResizeRedraw, true);
SetStyle(ControlStyles.Selectable, true);
SetStyle(ControlStyles.SupportsTransparentBackColor, true);
SetStyle(ControlStyles.UserPaint, true);
InitializeComponent();
}
// 温度
private float temperature = 0;
[Category("温度"), Description("当前温度")]
public float Temperature
{
set { temperature = value; }
get { return temperature; }
}
// 最高温度
private float highTemperature = 50;
[Category("温度"), Description("最高温度")]
public float HighTemperature
{
set { highTemperature = value; }
get { return highTemperature; }
}
// 最低温度
private float lowTemperature = -20;
[Category("温度"), Description("最低温度")]
public float LowTemperature
{
set { lowTemperature = value; }
get { return lowTemperature; }
}
// 当前温度数值的字体
private Font tempFont = new Font("宋体", 12);
[Category("温度"), Description("当前温度数值的字体")]
public Font TempFont
{
set { tempFont = value; }
get { return tempFont; }
}
// 当前温度数值的颜色
private Color tempColor = Color.Black;
[Category("温度"), Description("当前温度数值的颜色")]
public Color TempColor
{
set { tempColor = value; }
get { return tempColor; }
}
// 大刻度线数量
private int bigScale = 5;
[Category("刻度"), Description("大刻度线数量")]
public int BigScale
{
set { bigScale = value; }
get { return bigScale; }
}
// 小刻度线数量
private int smallScale = 5;
[Category("刻度"), Description("小刻度线数量")]
public int SmallScale
{
set { smallScale = value; }
get { return smallScale; }
}
// 刻度字体
private Font drawFont = new Font("Aril", 9);
[Category("刻度"), Description("刻度数字的字体")]
public Font DrawFont
{
get { return drawFont; }
set { drawFont = value; }
}
// 字体颜色
private Color drawColor = Color.Black;
[Category("刻度"), Description("刻度数字的颜色")]
public Color DrawColor
{
set { drawColor = value; }
get { return drawColor; }
}
// 刻度盘最外圈线条的颜色
private Color dialOutLineColor = Color.Gray;
[Category("背景"), Description("刻度盘最外圈线条的颜色")]
public Color DialOutLineColor
{
set { dialOutLineColor = value; }
get { return dialOutLineColor; }
}
// 刻度盘背景颜色
private Color dialBackColor = Color.Gray;
[Category("背景"), Description("刻度盘背景颜色")]
public Color DialBackColor
{
set { dialBackColor = value; }
get { return dialBackColor; }
}
// 大刻度线颜色
private Color bigScaleColor = Color.Black;
[Category("刻度"), Description("大刻度线颜色")]
public Color BigScaleColor
{
set { bigScaleColor = value; }
get { return bigScaleColor; }
}
// 小刻度线颜色
private Color smallScaleColor = Color.Black;
[Category("刻度"), Description("小刻度线颜色")]
public Color SmallScaleColor
{
set { smallScaleColor = value; }
get { return smallScaleColor; }
}