I am trying to make a spinning (rotating) label. I have inherited from control. Code (which is given below) works, but animation is not smooth. Text is not stable around the center, but it "shakes". Copy the code there is also a timer with 10ms interval) and see what I'm talking about.
Is it too soon to blame Microsoft for this?
If someone wants me to send him entire zipped project, send e-mail to dzenanz at gmail
Code (optimized for viewing):
Code (optimized for copy&paste):
public partial class RotateLabel : Control
{
public RotateLabel()
{
InitializeCompo nent();
DoubleBuffered = true;//Flickers a lot without this
aTimer.Start();
}
private sbyte _MinRotate=-90;
private sbyte _MaxRotate=+90;
private sbyte _RotateStep = 1;
private sbyte _CurrentRotate;//rotation in degrees
protected override void OnPaint(PaintEv entArgs e)
{
base.OnPaint(e) ;
e.Graphics.Tran slateTransform( Size.Width / 2, Size.Height / 2);
e.Graphics.Rota teTransform(_Cu rrentRotate);
SizeF tsize = e.Graphics.Meas ureString(Text, Font);
e.Graphics.Draw String(Text, Font, new SolidBrush(Fore Color), -tsize.Width / 2, -tsize.Height / 2);
e.Graphics.Draw Ellipse(Pens.Re d, -1, -1, 2, 2);//red dot in center - debugging
}
private void aTimer_Tick(obj ect sender, EventArgs e)//animation timer tick
{
if (_CurrentRotate == _MaxRotate)
_RotateStep = (sbyte)-1;
if (_CurrentRotate == _MinRotate)
_RotateStep = (sbyte)+1;
_CurrentRotate = (sbyte)(_Curren tRotate + _RotateStep);
Refresh();
}
}
Is it too soon to blame Microsoft for this?
If someone wants me to send him entire zipped project, send e-mail to dzenanz at gmail
Code (optimized for viewing):
Code:
public partial class RotateLabel : Control
{
public RotateLabel()
{
InitializeComponent();
DoubleBuffered = true;//Flickers a lot without this
aTimer.Start();
}
private sbyte _MinRotate=-90;
private sbyte _MaxRotate=+90;
private sbyte _RotateStep = 1;
private sbyte _CurrentRotate;//rotation in degrees
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
e.Graphics.TranslateTransform(Size.Width / 2, Size.Height / 2);
e.Graphics.RotateTransform(_CurrentRotate);
SizeF tsize = e.Graphics.MeasureString(Text, Font);
e.Graphics.DrawString(Text, Font, new SolidBrush(ForeColor), -tsize.Width / 2, -tsize.Height / 2);
e.Graphics.DrawEllipse(Pens.Red, -1, -1, 2, 2);//red dot in center - debugging
}
private void aTimer_Tick(object sender, EventArgs e)//animation timer tick
{
if (_CurrentRotate == _MaxRotate)
_RotateStep = (sbyte)-1;
if (_CurrentRotate == _MinRotate)
_RotateStep = (sbyte)+1;
_CurrentRotate = (sbyte)(_CurrentRotate + _RotateStep);
Refresh();
}
}
public partial class RotateLabel : Control
{
public RotateLabel()
{
InitializeCompo nent();
DoubleBuffered = true;//Flickers a lot without this
aTimer.Start();
}
private sbyte _MinRotate=-90;
private sbyte _MaxRotate=+90;
private sbyte _RotateStep = 1;
private sbyte _CurrentRotate;//rotation in degrees
protected override void OnPaint(PaintEv entArgs e)
{
base.OnPaint(e) ;
e.Graphics.Tran slateTransform( Size.Width / 2, Size.Height / 2);
e.Graphics.Rota teTransform(_Cu rrentRotate);
SizeF tsize = e.Graphics.Meas ureString(Text, Font);
e.Graphics.Draw String(Text, Font, new SolidBrush(Fore Color), -tsize.Width / 2, -tsize.Height / 2);
e.Graphics.Draw Ellipse(Pens.Re d, -1, -1, 2, 2);//red dot in center - debugging
}
private void aTimer_Tick(obj ect sender, EventArgs e)//animation timer tick
{
if (_CurrentRotate == _MaxRotate)
_RotateStep = (sbyte)-1;
if (_CurrentRotate == _MinRotate)
_RotateStep = (sbyte)+1;
_CurrentRotate = (sbyte)(_Curren tRotate + _RotateStep);
Refresh();
}
}

Comment