65.9K
CodeProject 正在变化。 阅读更多。
Home

ProgressPieControl:那个[无聊的 :) ] ProgressBar 的替代品

starIconstarIcon
emptyStarIcon
starIcon
emptyStarIconemptyStarIcon

2.27/5 (7投票s)

2005年5月27日

CPOL

1分钟阅读

viewsIcon

44913

downloadIcon

355

一个自定义的进度控制,使用图形和双缓冲。

Sample Image - ProgressPieControl.jpg

引言

好吧,我想每个人都会发生这样的事情(正常情况下 ^_^)会对那些控件感到厌倦,特别是进度条。

上个月,我在编码时就遇到了这种致命的情况……我决定创建一个没有条的自己的进度条,在CodeProject的用户贡献中搜索了替代方案之后。

注意

实际上,ProgressPie 使用了一些基本的三角学,但不用担心,一切都很容易学习,因为你想……

使用控件

就像每个控件一样,你只需要引用库文件,将其添加到你的工具箱(目前 ProgressPie 没有工具箱位图,我正在寻找一个),并填写它的属性,这些属性是

  • Comment:你想要在 ProgressPie 内部看到的文本。
  • MaximumProgressPie 的最大值。
  • MinimumProgressPie 的最小值。
  • Value:它的起始值。

在插入 ProgressPie 控制之后,只需以编程方式修改其值。在演示代码中,我使用了一个计时器来使其值在计时器的间隔处改变。

使用 ProgressPie 源代码

ProgressPie 类包含一些 getter 和 setter,这使得可以访问它的注释、最大值、最小值和值。一些属性使用以下方式设置

[Category("Appearance"),Description("The Maximal value of the ProgessPie.")]

但基本上它重写了 OnPaint 事件来执行一些其他的绘制

protected override void OnPaint(PaintEventArgs e)
{
    Graphics gr = e.Graphics; //get the graphics from the PaintEvent

    int nAngle = (int) Math.Floor ( (float) (nValue - nMinimum) / 
        (float) (nMaximum - nMinimum) * 360F);
        //convert from current value to degrees Angle

    SolidBrush redBrush = new SolidBrush(Color.Red);
    Rectangle rg = new Rectangle(this.Left - this.Location.X ,
        this.Top - this.Location.Y,this.Size.Width,this.Size.Width);
    GraphicsPath pthToDraw = new GraphicsPath();
    pthToDraw.AddRectangle(rg);
    PointF[] ptsArray = pthToDraw.PathPoints;
    PathGradientBrush pgBrush = new PathGradientBrush(ptsArray);
    //while less than alret value, draw in Green else in Red

    if (nValue < (int) Math.Floor( (double) 
                      (fAlert / 100 * ( nMaximum - nMinimum))) )
        pgBrush.CenterColor = Color.Green;         
    else
        pgBrush.CenterColor = Color.Red;
    Color[] srColor = {Color.Blue};
    pgBrush.SurroundColors = srColor;
    PointF ptCenter = new PointF( (float)(this.Left - 
                      this.Location.X + this.Size.Width / 2F ),
        (float)(this.Top - this.Location.Y + this.Size.Height / 2F ) );
    int nRay = this.Width / 2;
    double PiAngle = nAngle * Math.PI / 180 ;
    pgBrush.CenterPoint = new PointF (
            (float) ptCenter.X + nRay * (float)Math.Cos(PiAngle),
            (float) ptCenter.Y + nRay * (float)Math.Sin(PiAngle));
    gr.FillPie(pgBrush,rg,0,nAngle);
    base.OnPaint (e);
}

如果你发现 ProgressPie 并没有像你希望的那样工作,或者颜色不够丰富,你只需要修改 OnPaint 重写,以完全满意 :). 无论如何,ProgressPie 使用双缓冲,可以使用下面的代码片段访问,这使得它在高帧率下更加流畅

//dblBuffering

this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
this.SetStyle(ControlStyles.UserPaint, true);
this.SetStyle(ControlStyles.DoubleBuffer, true);

最后

任何评论都将非常有帮助 :)

© . All rights reserved.