实现效果如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApp2
{
internal class Mybutton : Button
{
int offset = 0;//定义流动条得偏移量
Timer timer = null;//流动条流动定时器
//定义多边形点位
Point point1, point2, point3, point4, point5, point7, point8;
Point[] points = null;
private void InitializeComponent()
{
this.SuspendLayout();
this.ResumeLayout(false);
}
//构造方法用于处理事件
public Mybutton()
{
timer = new Timer();
timer.Interval = 100; // 设置定时器间隔为50毫秒
timer.Tick += new EventHandler(TimerTick);
timer.Start();
}
//定义阀门的颜色属性
private Color _valuecolor = Color.Red;
[Description("阀门颜色"), Category("自定义")]
public Color ValueColor
{
get
{
return _valuecolor;
}
set
{
_valuecolor = value;
Refresh();//刷新控件
}
}
protected override void OnPaint(PaintEventArgs pevent)//重绘方法
{
base.OnPaint(pevent);//执行基类自己得方法
this.Text = "";
point1 = new Point(this.Width / 4, this.Height / 3);
point2 = new Point(this.Width / 4, this.Height / 3 * 2);
point3 = new Point(this.Width / 2, this.Height / 2);
point4 = new Point(this.Width / 4 * 3, this.Height / 3);
point5 = new Point(this.Width / 4 * 3, this.Height / 3 * 2);
points = new Point[] { point1, point2, point3, point4, point5 };
Graphics graphics = pevent.Graphics;//获得画板
graphics.DrawPolygon(new Pen(ValueColor), points);//画多边形
graphics.FillPolygon(new SolidBrush(ValueColor), points);//填充图形
//画流动线
Pen pen1 = new Pen(ValueColor, 10);
pen1.DashStyle = DashStyle.Dash;
pen1.DashPattern = new float[] { 5, 2 };
graphics.DrawLine(pen1, offset, this.Height / 2, this.Width + offset, this.Height / 2);
}
private void TimerTick(object sender, EventArgs e)
{
offset += 2; // 每次定时器触发,移动偏移量
if (offset > 100) offset = 0; // 如果偏移量过大,重置为0,这里可以根据需要调整重置条件
this.Invalidate();// 请求重绘窗体
}
}
}