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 { /// 一个类似瓶子的控件对象模型 [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; /// 必需的设计器变量。 private IContainer components = (IContainer)null; /// 实例化一个新的控件对象 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); } /// 获取或设置控件的背景色 [Browsable(true)] [Description("获取或设置控件的背景色")] [Category("HslControls")] [DefaultValue(typeof(Color), "Transparent")] [EditorBrowsable(EditorBrowsableState.Always)] public override Color BackColor { get => base.BackColor; set => base.BackColor = value; } /// 获取或设置瓶子的液位值。 [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(); } } /// 获取或设置瓶子是否处于打开的状态。 [Browsable(true)] [Description("获取或设置瓶子是否处于打开的状态。")] [DefaultValue(typeof(bool), "false")] [Category("HslControls")] public virtual bool IsOpen { get => this.isOpen; set { this.isOpen = value; this.Invalidate(); } } /// 获取或设置瓶子的标签信息,用于绘制在瓶子上的信息。 [Browsable(true)] [Description("获取或设置瓶子的标签信息,用于绘制在瓶子上的信息。")] [DefaultValue(typeof(string), "")] [Category("HslControls")] public virtual string BottleTag { get => this.bottleTag; set { this.bottleTag = value; this.Invalidate(); } } /// 获取或设置瓶子的备注信息,用于绘制在瓶子顶部的信息。 [Browsable(true)] [Description("获取或设置瓶子的备注信息,用于绘制在瓶子顶部的信息。")] [DefaultValue(typeof(string), "原料1")] [Category("HslControls")] public virtual string HeadTag { get => this.headTag; set { this.headTag = value; this.Invalidate(); } } /// 获取或设置控件底座的高度 [Browsable(true)] [DefaultValue(30f)] [Description("获取或设置控件底座的高度")] [Category("HslControls")] public virtual float DockHeight { get => this.dockHeight; set { this.dockHeight = value; this.Invalidate(); } } /// 获取或设置前景色的边缘颜色内容 [Browsable(true)] [DefaultValue(typeof(Color), "[194, 190, 77]")] [Description("获取或设置前景色的边缘颜色内容")] [Category("HslControls")] public virtual Color ForeColorEdge { get => this.foreColorEdge; set { this.foreColorEdge = value; this.Invalidate(); } } /// 获取或设置前景色的中心颜色内容 [Browsable(true)] [DefaultValue(typeof(Color), "[226, 221, 98]")] [Description("获取或设置前景色的中心颜色内容")] [Category("HslControls")] public virtual Color ForeColorCenter { get => this.foreColorCenter; set { this.foreColorCenter = value; this.Invalidate(); } } /// 获取或设置前景色的顶部颜色内容 [Browsable(true)] [DefaultValue(typeof(Color), "[243, 245, 139]")] [Description("获取或设置前景色的顶部颜色内容")] [Category("HslControls")] public virtual Color ForeColorTop { get => this.foreColorTop; set { this.foreColorTop = value; this.Invalidate(); } } /// 获取或设置背景色的边缘颜色内容 [Browsable(true)] [DefaultValue(typeof(Color), "[142, 196, 216]")] [Description("获取或设置背景色的边缘颜色内容")] [Category("HslControls")] public virtual Color BackColorEdge { get => this.backColorEdge; set { this.backColorEdge = value; this.Invalidate(); } } /// 获取或设置背景色的中心颜色内容 [Browsable(true)] [DefaultValue(typeof(Color), "[240, 240, 240]")] [Description("获取或设置背景色的中心颜色内容")] [Category("HslControls")] public virtual Color BackColorCenter { get => this.backColorCenter; set { this.backColorCenter = value; this.Invalidate(); } } /// 获取或设置背景色的顶部颜色内容 [Browsable(true)] [DefaultValue(typeof(Color), "[151, 232, 244]")] [Description("获取或设置背景色的顶部颜色内容")] [Category("HslControls")] public virtual Color BackColorTop { get => this.backColorTop; set { this.backColorTop = value; this.Invalidate(); } } /// 重绘控件的外观 /// 事件 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); } /// 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(); } /// 清理所有正在使用的资源。 /// 如果应释放托管资源,为 true;否则为 false。 protected override void Dispose(bool disposing) { if (disposing && this.components != null) this.components.Dispose(); base.Dispose(disposing); } /// /// 设计器支持所需的方法 - 不要修改 /// 使用代码编辑器修改此方法的内容。 /// private void InitializeComponent() { this.SuspendLayout(); this.AutoScaleMode = AutoScaleMode.None; this.BackColor = Color.Transparent; this.Name = nameof(HslBottle); this.ResumeLayout(false); } } }