如何绘制一个动态的流动管道(FlowPipe)?
三步
- 定义属性
- 画布重绘
- 图形方法
1.定义属性
- 管道的(两端转向、样式、边沿颜色、中间颜色、激活)
- 流动条的(速度、长度、宽度、间隙、颜色)
//属性示例:按照示例添加以上各种属性
private float moveSpeed = 0.3f;
[Browsable(true)]
[Category("布局_G")]
[Description("流动条速度,负数为反向")] //属性说明
public float MoveSpeed
{
get {
return moveSpeed; }
set
{
this.moveSpeed = value;
this.Invalidate(); //重绘
}
}
2.画布重绘
- 水平管道分为左、中、右三部分;垂直管道分为上、中、下;分别绘制(两端不动朝向的情况)
- 设置画布质量、渐近线比例、颜色
- 画椭圆:PaintEllipse(图形、笔、绘制路径…)
- 画矩形:PaintRectangle(图形、笔、绘制路径…)
- 画虚线(流动条):AddArc(添加椭圆)、AddLine(添加直线)
- 运用两个画椭圆、画矩形的方法,见3
#region 画布
//字段
private Graphics g; //GPI绘图
private Pen p; //画笔
private float startOffset = 0; //短划线起始位置
private Timer myTimer;
protected override void OnPaint(PaintEventArgs e)
{
//画布质量
g = e.Graphics;
g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
ColorBlend colorBlend = new ColorBlend(); //渐变色设置
//渐近线比例、颜色
colorBlend.Positions = new float[] {
0.0f, 0.5f, 1.0f };
colorBlend.Colors = new Color[] {
this.pipeColorEdge, this.pipeColorCenter, this.pipeColorEdge };
//画笔初始化
this.p = new Pen(this.pipeColorBorder, 1.0f);
//管道绘制(水平)
if (this.pipeStyle == PipeStyle.Horizontal)
{
//矩形画刷
LinearGradientBrush linearGradientBrush = new LinearGradientBrush(new Point(0, 0), new Point(0, this.Height), pipeColorEdge, pipeColorEdge);
linearGradientBrush.InterpolationColors = colorBlend;
//绘制左部分
switch (this.pipeTurnLeft)
{
case PipeTurn.Up:
this.PaintEllipse(g, colorBlend, p, new Rectangle(0, this.Height * (-1)-1, this.Height * 2, this.Height * 2), 90.0f, 90.0f);
break;
case PipeTurn.Down:
this.PaintEllipse(g, colorBlend, p, new Rectangle(0, 0, this.Height * 2, this.Height * 2), 180.0f, 90.0f);
break;
default:
this.PaintRectangle(g, linearGradientBrush, p, new Rectangle(-1, 0, this.Height+1, this.Height));
break;
}
//绘制右部分
switch (this.pipeTurnRight)
{
case PipeTurn