C#控件开发2—流动管道

如何绘制一个动态的流动管道(FlowPipe)?

三步

  1. 定义属性
  2. 画布重绘
  3. 图形方法

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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

GesLuck

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值