You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
lj_plc/Controls/Mesnac.Controls.ChemicalWei.../HslBottle.cs

346 lines
13 KiB
C#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing.Drawing2D;
using System.Drawing.Text;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Mesnac.Controls.ChemicalWeighing
{
/// <summary>一个类似瓶子的控件对象模型</summary>
[Description("一个类似瓶子,罐子的控件,支持液位显示,支持设置渐变的颜色及文本标题")]
public class HslBottle : UserControl
{
private Color foreColorTop = Color.FromArgb(243, 245, 139);
private Color foreColorEdge = Color.FromArgb(194, 190, 77);
private Color foreColorCenter = Color.FromArgb(226, 221, 98);
private Color backColorTop = Color.FromArgb(151, 232, 244);
private Color backColorEdge = Color.FromArgb(142, 196, 216);
private Color backColorCenter = Color.FromArgb(240, 240, 240);
private double value = 50.0;
private bool isOpen = false;
private string bottleTag = "";
private StringFormat sf;
private string headTag = "原料1";
private float dockHeight = 30f;
/// <summary>必需的设计器变量。</summary>
private IContainer components = (IContainer)null;
/// <summary>实例化一个新的控件对象</summary>
public HslBottle()
{
this.InitializeComponent();
this.sf = new StringFormat();
this.sf.Alignment = StringAlignment.Center;
this.sf.LineAlignment = StringAlignment.Center;
this.SetStyle(ControlStyles.UserPaint | ControlStyles.SupportsTransparentBackColor, true);
this.SetStyle(ControlStyles.ResizeRedraw, true);
this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
}
/// <summary>获取或设置控件的背景色</summary>
[Browsable(true)]
[Description("获取或设置控件的背景色")]
[Category("HslControls")]
[DefaultValue(typeof(Color), "Transparent")]
[EditorBrowsable(EditorBrowsableState.Always)]
public override Color BackColor
{
get => base.BackColor;
set => base.BackColor = value;
}
/// <summary>获取或设置瓶子的液位值。</summary>
[Browsable(true)]
[Description("获取或设置瓶子的液位值。")]
[DefaultValue(typeof(double), "60")]
[Category("HslControls")]
public virtual double Value
{
get => this.value;
set
{
if (value < 0.0 || value > 100.0)
return;
this.value = value;
this.Invalidate();
}
}
/// <summary>获取或设置瓶子是否处于打开的状态。</summary>
[Browsable(true)]
[Description("获取或设置瓶子是否处于打开的状态。")]
[DefaultValue(typeof(bool), "false")]
[Category("HslControls")]
public virtual bool IsOpen
{
get => this.isOpen;
set
{
this.isOpen = value;
this.Invalidate();
}
}
/// <summary>获取或设置瓶子的标签信息,用于绘制在瓶子上的信息。</summary>
[Browsable(true)]
[Description("获取或设置瓶子的标签信息,用于绘制在瓶子上的信息。")]
[DefaultValue(typeof(string), "")]
[Category("HslControls")]
public virtual string BottleTag
{
get => this.bottleTag;
set
{
this.bottleTag = value;
this.Invalidate();
}
}
/// <summary>获取或设置瓶子的备注信息,用于绘制在瓶子顶部的信息。</summary>
[Browsable(true)]
[Description("获取或设置瓶子的备注信息,用于绘制在瓶子顶部的信息。")]
[DefaultValue(typeof(string), "原料1")]
[Category("HslControls")]
public virtual string HeadTag
{
get => this.headTag;
set
{
this.headTag = value;
this.Invalidate();
}
}
/// <summary>获取或设置控件底座的高度</summary>
[Browsable(true)]
[DefaultValue(30f)]
[Description("获取或设置控件底座的高度")]
[Category("HslControls")]
public virtual float DockHeight
{
get => this.dockHeight;
set
{
this.dockHeight = value;
this.Invalidate();
}
}
/// <summary>获取或设置前景色的边缘颜色内容</summary>
[Browsable(true)]
[DefaultValue(typeof(Color), "[194, 190, 77]")]
[Description("获取或设置前景色的边缘颜色内容")]
[Category("HslControls")]
public virtual Color ForeColorEdge
{
get => this.foreColorEdge;
set
{
this.foreColorEdge = value;
this.Invalidate();
}
}
/// <summary>获取或设置前景色的中心颜色内容</summary>
[Browsable(true)]
[DefaultValue(typeof(Color), "[226, 221, 98]")]
[Description("获取或设置前景色的中心颜色内容")]
[Category("HslControls")]
public virtual Color ForeColorCenter
{
get => this.foreColorCenter;
set
{
this.foreColorCenter = value;
this.Invalidate();
}
}
/// <summary>获取或设置前景色的顶部颜色内容</summary>
[Browsable(true)]
[DefaultValue(typeof(Color), "[243, 245, 139]")]
[Description("获取或设置前景色的顶部颜色内容")]
[Category("HslControls")]
public virtual Color ForeColorTop
{
get => this.foreColorTop;
set
{
this.foreColorTop = value;
this.Invalidate();
}
}
/// <summary>获取或设置背景色的边缘颜色内容</summary>
[Browsable(true)]
[DefaultValue(typeof(Color), "[142, 196, 216]")]
[Description("获取或设置背景色的边缘颜色内容")]
[Category("HslControls")]
public virtual Color BackColorEdge
{
get => this.backColorEdge;
set
{
this.backColorEdge = value;
this.Invalidate();
}
}
/// <summary>获取或设置背景色的中心颜色内容</summary>
[Browsable(true)]
[DefaultValue(typeof(Color), "[240, 240, 240]")]
[Description("获取或设置背景色的中心颜色内容")]
[Category("HslControls")]
public virtual Color BackColorCenter
{
get => this.backColorCenter;
set
{
this.backColorCenter = value;
this.Invalidate();
}
}
/// <summary>获取或设置背景色的顶部颜色内容</summary>
[Browsable(true)]
[DefaultValue(typeof(Color), "[151, 232, 244]")]
[Description("获取或设置背景色的顶部颜色内容")]
[Category("HslControls")]
public virtual Color BackColorTop
{
get => this.backColorTop;
set
{
this.backColorTop = value;
this.Invalidate();
}
}
/// <summary>重绘控件的外观</summary>
/// <param name="e">事件</param>
protected override void OnPaint(PaintEventArgs e)
{
Graphics graphics = e.Graphics;
graphics.SmoothingMode = SmoothingMode.HighQuality;
graphics.TextRenderingHint = TextRenderingHint.ClearTypeGridFit;
this.PaintHslControls(graphics, this.Width, this.Height);
base.OnPaint(e);
}
/// <inheritdoc cref="M:HslControls.HslArrow.PaintHslControls(System.Drawing.Graphics,System.Single,System.Single)" />
public void PaintHslControls(Graphics g, int width, int height)
{
if (width < 15 || this.Height < 15)
return;
int num1 = (double)this.dockHeight < 1.0 ? (int)((double)this.dockHeight * (double)height) : (int)this.dockHeight;
int y1 = 20;
float x = (float)width / 2f;
float y2 = (float)(height - num1) - (float)((double)(height - num1 - y1) * (double)Convert.ToSingle(this.value) / 100.0);
int num2 = width / 50 + 3;
if (string.IsNullOrEmpty(this.headTag))
y1 = num2;
GraphicsPath path = new GraphicsPath();
path.AddPolygon(new PointF[6]
{
new PointF(0.0f, (float) y1),
new PointF(0.0f, (float) (height - num1)),
new PointF(x, (float) (height - 1)),
new PointF((float) (width - 1), (float) (height - num1)),
new PointF((float) (width - 1), (float) y1),
new PointF(0.0f, (float) y1)
});
LinearGradientBrush linearGradientBrush = new LinearGradientBrush(new Point(0, y1), new Point(width - 1, y1), Color.FromArgb(142, 196, 216), Color.FromArgb(240, 240, 240));
ColorBlend colorBlend = new ColorBlend();
colorBlend.Positions = new float[3] { 0.0f, 0.5f, 1f };
colorBlend.Colors = new Color[3]
{
this.backColorEdge,
this.backColorCenter,
this.backColorEdge
};
linearGradientBrush.InterpolationColors = colorBlend;
g.FillPath((Brush)linearGradientBrush, path);
using (Brush brush = (Brush)new SolidBrush(this.backColorTop))
g.FillEllipse(brush, 1, y1 - num2, width - 2, num2 * 2);
path.Reset();
path.AddPolygon(new PointF[6]
{
new PointF(0.0f, y2),
new PointF(0.0f, (float) (height - num1)),
new PointF(x, (float) (height - 1)),
new PointF((float) (width - 1), (float) (height - num1)),
new PointF((float) (width - 1), y2),
new PointF(0.0f, y2)
});
colorBlend.Colors = new Color[3]
{
this.foreColorEdge,
this.foreColorCenter,
this.foreColorEdge
};
linearGradientBrush.InterpolationColors = colorBlend;
g.FillPath((Brush)linearGradientBrush, path);
linearGradientBrush.Dispose();
using (Brush brush = (Brush)new SolidBrush(this.foreColorTop))
g.FillEllipse(brush, 1f, y2 - (float)num2, (float)(width - 2), (float)(num2 * 2));
path.Reset();
path.AddPolygon(new PointF[3]
{
new PointF(0.0f, (float) (height - num1)),
new PointF(x, (float) (height - 1)),
new PointF((float) (width - 1), (float) (height - num1))
});
path.AddArc(0, height - num1 - num2, width, num2 * 2, 0.0f, 180f);
using (Brush brush = (Brush)new SolidBrush(this.foreColorEdge))
g.FillPath(brush, path);
path.Reset();
path.AddLines(new PointF[4]
{
new PointF((float) width / 4f, (float) (height - num1 / 2)),
new PointF((float) width / 4f, (float) (height - 1)),
new PointF((float) ((double) width * 3.0 / 4.0), (float) (height - 1)),
new PointF((float) ((double) width * 3.0 / 4.0), (float) (height - num1 / 2))
});
path.AddArc((float)width / 4f, (float)(height - num1 / 2 - num2 - 1), (float)width / 2f, (float)(num2 * 2), 0.0f, 180f);
g.FillPath(Brushes.DimGray, path);
using (Brush brush = (Brush)new SolidBrush(this.ForeColor))
{
if (!string.IsNullOrEmpty(this.bottleTag))
g.DrawString(this.bottleTag, this.Font, brush, (RectangleF)new Rectangle(-10, 26, width + 20, 20), this.sf);
if (!string.IsNullOrEmpty(this.headTag))
g.DrawString(this.headTag, this.Font, brush, (RectangleF)new Rectangle(-10, 0, width + 20, 20), this.sf);
}
path.Dispose();
}
/// <summary>清理所有正在使用的资源。</summary>
/// <param name="disposing">如果应释放托管资源,为 true否则为 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && this.components != null)
this.components.Dispose();
base.Dispose(disposing);
}
/// <summary>
/// 设计器支持所需的方法 - 不要修改
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.SuspendLayout();
this.AutoScaleMode = AutoScaleMode.None;
this.BackColor = Color.Transparent;
this.Name = nameof(HslBottle);
this.ResumeLayout(false);
}
}
}