勇哥注:
阀门控件可用于组态页面,构建自动化逻辑的控制面板。
基本套路就是用GDI代码进行绘制。
下图是组态页面:
下图是阀门控件的绘制效果。
它有横向与竖向两种形态。
注意多边形的5个点如下:
其它没什么好讲的。
源码:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace 绘制阀门控件 { public enum DirectionEnum { 横向=0,竖向 } internal class FamenControl:Button { private Color myColor = Color.Yellow; [Description("阀门颜色"),Category("自定义")] public Color MyColor { get { return myColor; } set { myColor = value; } } private DirectionEnum myDirection; [Description("阀门方向"),Category("自定义")] public DirectionEnum MyDirection { get { return myDirection; } set { myDirection = value; } } Point p1, p2, p3, p4, p5,p7,p8; protected override void OnPaint(PaintEventArgs pevent) { this.Text = ""; switch(MyDirection) { case DirectionEnum.横向: p1 = new Point(this.Width / 4, this.Height / 3); p2 = new Point(this.Width / 4, this.Height / 3 * 2); p3 = new Point(this.Width / 2, this.Height / 2); p4 = new Point(this.Width / 4 * 3, this.Height / 3); p5 = new Point(this.Width / 4 * 3, this.Height / 3 * 2); p7 = new Point(0, this.Height/ 2); p8 = new Point(this.Width, this.Height / 2); break; case DirectionEnum.竖向: p1 = new Point(this.Width / 3, this.Height / 4); p2 = new Point(this.Width / 3*2, this.Height / 4); p3 = new Point(this.Width / 2, this.Height / 2); p4 = new Point(this.Width / 3, this.Height /4*3); p5 = new Point(this.Width / 3 * 2, this.Height / 4 * 3); p7 = new Point(this.Width/2, this.Height); p8 = new Point(this.Width/2, 0); break; } var ptArr = new Point[] { p1, p2, p3, p4, p5}; base.OnPaint(pevent); Graphics g = pevent.Graphics; g.DrawPolygon(new Pen(MyColor), ptArr); g.FillPolygon(new SolidBrush(MyColor), ptArr); g.DrawLine(new Pen(myColor,3), p7, p8); } } }
本文出自勇哥的网站《少有人走的路》wwww.skcircle.com,转载请注明出处!讨论可扫码加群:

本帖最后由 勇哥,很想停止 于 2023-05-23 21:35:11 编辑 
