做了一个简单的例子:在Form1 中放置一个 PictureBox 和 一个 Timer,
运行后会在 PictureBox 的中央画一个矩形,然后每秒旋转10度。
public partial class Form1 : Form
{
private float angle = 0;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
pictureBox1.BackColor = Color.White;
pictureBox1.Dock = DockStyle.Fill;
timer1.Interval = 1000;
timer1.Enabled = true;
}
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
int width = 20;
int height = 40;
int x = (pictureBox1.Width - width) / 2;
int y = (pictureBox1.Height - height) / 2;
e.Graphics.TranslateTransform(x, y);
e.Graphics.RotateTransform(angle);
angle += 10;
e.Graphics.DrawRectangle(new Pen(Color.Black, 1), new Rectangle(0 - width/2, 0 - height/2, width, height));
}
private void timer1_Tick(object sender, EventArgs e)
{
pictureBox1.Invalidate();
pictureBox1.Update();
}
楼上的很经典!可以试试!