如何在C#中实现平滑移动拨号?
我试图实现一个平滑移动的转盘。我拿了一些模拟时钟控制的代码,我正试图修改它。我的问题是图形闪烁很多(移动的刻度盘)。我有一个计时器调用表单。每10毫秒刷新一次。任何快的都会闪烁太多,任何慢的都会口吃。以下是代码的其余部分:如何在C#中实现平滑移动拨号?,c#,gdi,C#,Gdi,我试图实现一个平滑移动的转盘。我拿了一些模拟时钟控制的代码,我正试图修改它。我的问题是图形闪烁很多(移动的刻度盘)。我有一个计时器调用表单。每10毫秒刷新一次。任何快的都会闪烁太多,任何慢的都会口吃。以下是代码的其余部分: private void InitializeComponent() { ... this.Paint += new System.Windows.Forms.PaintEventHandler(this.AnalogClock_Pai
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
}
我将拉尔斯泰克的评论作为回答: 在构造函数中将
Form
的DoubleBuffer
属性设置为true
这大大减少了闪烁。尝试在模拟时钟控件的构造函数中将DoubleBuffer属性设置为true。如果它只是一个面板,则继承该面板以创建自己的控件来设置DoubleBuffer属性。
DoubleBuffered=true代码>修复它!