C# User Controls - Flow Pipeline
How to draw a dynamic FlowPipe?
Drawing in two steps
- Define the attributes;
- Canvas repainted;
Primary Skills:
- Drawing of pipes (gradient color rectangles)
/// <summary>
/// How to draw a gradient color rectangle
/// </summary>
/// <param name="g">canvas (artist's painting surface)</param>
/// <param name="brush">paintbrush</param>
/// <param name="pen">classifier for sums of money, deals</param>
/// <param name="rectangle">rectangles</param>
private void PaintRectangle(Graphics g, Brush brush, Pen pen, Rectangle rectangle)
{
//填充rectangles
(brush, rectangle);
switch ()
{
case :
(pen, , , + , );
(pen, , + - 1, + , );
break;
case :
(pen, , , , + );
(pen, + - 1, , + - 1, );
break;
default:
break;
}
}
- Drawing of pipes (gradient color semicircle)
/// <summary>
/// How to draw a gradient color semicircle
/// </summary>
/// <param name="g">canvas (artist's painting surface)</param>
/// <param name="colorBlend"></param>
/// <param name="p"></param>
/// <param name="rect"></param>
/// <param name="startAngle"></param>
/// <param name="sweepAngle"></param>
private void PaintEllipse(Graphics g, ColorBlend colorBlend, Pen p, Rectangle rect, float startAngle, float sweepAngle)
{
//initial step:establishGPItrails
GraphicsPath path = new GraphicsPath();
(rect);
//second step:gradient color filling
PathGradientBrush brush = new PathGradientBrush(path);
= new Point( + / 2, + / 2);
= colorBlend;
//third step:Drawing Pipes
(brush, rect, startAngle, sweepAngle);
//fourth step:Drawing Edge Lines
(p, rect, startAngle, sweepAngle);
}
- Drawing of flow bars (dotted lines with pen)
//dotted line,The key is drawn with a pen and a path
Pen pen = new Pen(, );
= ;
= new float[]
{
flowLength,flowLengthGap
};
= ;
(pen, path);
//Mobility bar path
GraphicsPath path = new GraphicsPath();
//dotted path—the left side、interlocutory、right, the right
switch ()
{
case :
(new Rectangle( / 2, / 2 * (-1) -1, , ), 181.0f, -91.0f);
break;
case :
(new Rectangle( / 2, / 2, , ), 180.0f, 90.0f);
break;
default:
(-1, / 2, +1, / 2);
break;
}
-
Key to understanding: draw the ellipse, line (Rectangle) & lt; x, y [round cut rectangle relative to the origin of the control & lt; upper-left corner & gt; coordinates], width, height, start angle, scanning angle & gt; understanding of the good to draw!
(new Rectangle( / 2, / 2, , ), 180.0f, 90.0f);
(-1, / 2, +1, / 2);
-
Key elements of mobility
// Flow bar flow rate (refresh rate)
= new Timer();
= 50;
+= MyTimer_Tick; ;
}
#region Timer loop
private void MyTimer_Tick(object sender, EventArgs e)
{
= - ;
if ( > + || < ( + ) * (-1))
{ = 0; }
();
}
#endregion
1. Define attributes
- Pipeline's (turn at ends, style, edge color, middle color, activation)
- Flow strip's (speed, length, width, gap, color)
//Property example: add the above properties according to the example
private float moveSpeed = 0.3f;
[Browsable(true)]
[Category("Layout_G")]
[Description("Flow bar speed, negative numbers are reversed")] //Property description
public float MoveSpeed
{
get { return moveSpeed; }
set
{
= value; }
(); //redraw
}
}
2. Canvas repainting
【The pipeline is divided into left、center、right three parts。Draw the pipe first.(rectangles):unorthodox、center、right (-hand);Re-drawing of flow strips(dotted line):unorthodox、center、right (-hand)】
//rectangles画刷
LinearGradientBrush linearGradientBrush = new LinearGradientBrush(new Point(0, 0), new Point(0, ), pipeColorEdge, pipeColorEdge);
= colorBlend;
//绘制unorthodox部分
switch ()
{
case :
(g, colorBlend, p, new Rectangle(0, * (-1)-1, * 2, * 2), 90.0f, 90.0f);
break;
case :
(g, colorBlend, p, new Rectangle(0, 0, * 2, * 2), 180.0f, 90.0f);
break;
default:
(g, linearGradientBrush, p, new Rectangle(-1, 0, +1, ));
break;
}
//绘制right (-hand)部分
switch ()
{
case :
(g, colorBlend, p, new Rectangle( - * 2, * (-1)-1, * 2, * 2), 0.0f, 90.0f);
break;
case :
(g, colorBlend, p, new Rectangle( - * 2, 0, * 2, * 2), 270.0f, 90.0f);
break;
default:
(g, linearGradientBrush, p, new Rectangle( - , 0, , ));
break;
}
//绘制center间
if ( > * 2)
{
(g, linearGradientBrush, p, new Rectangle( - 1, 0, - * 2 + 2, ));
}
//Mobility bar path
GraphicsPath path = new GraphicsPath();
//dotted path—the left side
switch ()
{
case :
(new Rectangle( / 2, / 2 * (-1) -1, , ), 181.0f, -91.0f);
break;
case :
(new Rectangle( / 2, / 2, , ), 180.0f, 90.0f);
break;
default:
(-1, / 2, +1, / 2);
break;
}
//dotted path—interlocutory
if ( > * 2)
{
(, / 2, - -1, / 2);
}
//dotted path—right, the right
switch ()
{
case :
(new Rectangle( - 1 - * 3 / 2, - / 2-1 , , ), 88f, -91.0f);
break;
case :
(new Rectangle( - 1 - * 3 / 2, / 2, , ), 270.0f, 90.0f);
break;
default:
( - , / 2, , / 2);
break;
}
//dotted line,The key uses the pen and the path to
Pen pen = new Pen(, );
= ;
= new float[]
{
flowLength,flowLengthGap
};
= ;
(pen, path);
The format is all the same, get the key codes and the liver is right.
End