Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
using System.Threading;
using System.Diagnostics;
namespace _123
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
bitmap = new Bitmap(500, 500);
g = Graphics.FromImage(bitmap);
stopwatch = new Stopwatch();
this.DoubleBuffered = true;
this.Paint += Draw;
stopwatch.Start();
(new Thread(new ThreadStart(Drawing_Thread))).Start();
}
Stopwatch stopwatch;
Bitmap bitmap;
Graphics g;
int x = 20;
int add = 1;
private void Drawing_Thread()
{
while (true)
if (stopwatch.Elapsed.Milliseconds >= 1)
{
stopwatch.Stop();
if (x == 300) add *= -1;
if (x == 0) add *= -1;
g.Clear(Color.Transparent);
g.FillRectangle(Brushes.Red, x += add, 50, 100, 100);
this.Invoke(new Action(this.Invalidate));
stopwatch.Restart();
}
}
private void Draw(object sender, PaintEventArgs e)
{
e.Graphics.DrawImage(bitmap, 0, 0);
}
}
}