如何在C#中实现流畅的表盘?

时间:2013-12-17 21:26:31

标签: c# gdi

我正在尝试实现平稳移动的转盘。我为模拟时钟控制采用了一些代码,我正在尝试修改它。我的问题是图形闪烁很多(移动拨号)。我有一个计时器每隔10毫秒调用一次Form.Refresh。任何事情都会更快,它会闪烁太多,任何事情都会变得更慢而且口吃。这是代码的其余部分:

private void InitializeComponent()
    {
        ...
        this.Paint += new System.Windows.Forms.PaintEventHandler(this.AnalogClock_Paint);
    }

    private void AnalogClock_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
    {
        double ms = ( DateTime.UtcNow-_startTime).TotalMilliseconds;
        double fRadHr=(ms/CYCLE_MS) *2*PI;
        e.Graphics.FillEllipse(new SolidBrush(Color.White), fCenterX - fCenterCircleRadius / 2, fCenterY - fCenterCircleRadius / 2, fCenterCircleRadius, fCenterCircleRadius);
        DrawPolygon(this.fHourThickness, this.fHourLength, hrColor, fRadHr, e);
        e.Graphics.SmoothingMode = SmoothingMode.HighQuality;
    }

    private void DrawPolygon(float fThickness, float fLength, Color color, double fRadians, System.Windows.Forms.PaintEventArgs e)
    {

        PointF A=new PointF( (float)(fCenterX+ fThickness*2*System.Math.Sin(fRadians+PI/2)), 
            (float)(fCenterY - fThickness*2*System.Math.Cos(fRadians+PI/2)) );
        PointF B=new PointF( (float)(fCenterX+ fThickness*2*System.Math.Sin(fRadians-PI/2)),
            (float)(fCenterY - fThickness*2*System.Math.Cos(fRadians-PI/2)) );
        PointF C=new PointF( (float)(fCenterX+ fLength*System.Math.Sin(fRadians)), 
            (float) (fCenterY - fLength*System.Math.Cos(fRadians)) );
        PointF D=new PointF( (float)(fCenterX- fThickness*4*System.Math.Sin(fRadians)), 
            (float)(fCenterY + fThickness*4*System.Math.Cos(fRadians) ));
        PointF[] points={A,D,B,C};
        e.Graphics.FillPolygon( new SolidBrush(color), points );
    }

    protected override void OnPaintBackground(PaintEventArgs e)
    {
        // draws background once so at least that doesn't flicker
    }

1 个答案:

答案 0 :(得分:1)

我发布了LarsTech的评论作为答案:

在构造函数中将DoubleBuffer的{​​{1}}属性设置为true。

这大大减少了闪烁。