Start a new Forms application. Drop a Panel and a Timer control on it. Set up a Timer_Tick, Form_Load and a Panel_Paint eventhandler and fill in the code. Run the app and watch the string rotate.
Animation: Rotate a string with a timer.
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
namespace StringRotation
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
string RotaStr = "123"; // The string to rotate
float RotationAngle = 0f; // The "amount" of rotation
Point loc = new Point(50, 50); // Where we first start to draw the string
private void DrawIt(Graphics G)
{
const int cFontSize = 36;
// Don't make the chars look edgy
G.SmoothingMode = SmoothingMode.AntiAlias;
// Set up string stuff
FontFamily family = new FontFamily("Arial");
int fontStyle = (int)FontStyle.Bold;
int emSize = family.GetEmHeight(FontStyle.Bold) / cFontSize;
Point origin = loc;
StringFormat format = StringFormat.GenericDefault;
Size TxSz = TextRenderer.MeasureText(RotaStr, new Font(family, cFontSize));
// Define rotation matrix
Matrix RotationTransform = new Matrix(1, 0, 0, 1, 1, 1);
// Calculate rotation point
PointF RotationPoint = new PointF(loc.X + TxSz.Width / 2, loc.Y + TxSz.Height / 2);
// Set up the path
GraphicsPath gp = new GraphicsPath();
// Add the string to the path.
gp.AddString(RotaStr, family, fontStyle, emSize, origin, format);
// Make the rotation transformation
RotationTransform.RotateAt(RotationAngle, RotationPoint);
gp.Transform(RotationTransform);
// Color the path and fill it
SolidBrush B = new SolidBrush(Color.OliveDrab);
G.FillPath(B, gp);
}
private void timer1_Tick(object sender, EventArgs e)
{
RotationAngle += 20f; // Rotate by 20 degrees every tick
if (RotationAngle == 360f) RotationAngle = 0f;
Refresh();
}
private void Form1_Load(object sender, EventArgs e)
{
this.timer1.Interval = 200;
timer1.Start();
}
private void panel1_Paint(object sender, PaintEventArgs e)
{
DrawIt(e.Graphics);
}
}
}
DdoubleD 315 Posting Shark
ddanbe 2,724 Professional Procrastinator Featured Poster
Diamonddrake 397 Master Poster
ddanbe 2,724 Professional Procrastinator Featured Poster
Diamonddrake 397 Master Poster
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.