为什么用线程去移动图片会闪呀闪的啊?
我用线程对图片的位置进行操作,产生移动效果,为什么会闪呀闪的啊?using System;
using System.Windows.Forms;
using System.Drawing;
using System.Threading;namespace 多线程带参数
{
class Form1 : Form
{
private PictureBox pic;
Graphics g;
private int Bit_X,Loca;
Bitmap bit; public Form1()
{
Initialize();
} private void Initialize()
{
pic = new PictureBox();
pic.Size = new Size(32, 48);
pic.Location = new Point(0,0);
g = pic.CreateGraphics(); this.SuspendLayout();
this.Controls.Add(pic);
this.Size = new Size(500, 280);
this.StartPosition = FormStartPosition.CenterScreen;
this.ResumeLayout();
bit = new Bitmap("MAN.png");
Loca=Bit_X = 0;
pic.Paint += new PaintEventHandler(pic_Paint);
// CheckForIllegalCrossThreadCalls = false;
} void pic_Paint(object sender, PaintEventArgs e)
{
Thread thread1 = new Thread(new ThreadStart(ManMove));
thread1.IsBackground = true;
thread1.Start();
} private void ManMove()
{
while (pic.Location.X<460)
{
//bit.RotateFlip(RotateFlipType.Rotate90FlipY);
Rectangle rect = new Rectangle(Bit_X, 96, 32, 48);
Rectangle rectLoca = new Rectangle(0, 0, 32, 48);
g.Clear(pic.BackColor);
g.DrawImage(bit, rectLoca,rect,GraphicsUnit.Pixel);
Thread.Sleep(200);
Bit_X += 32;
if (Bit_X >= 128)
Bit_X = 0;
MethodInvoker m = new MethodInvoker(Move_Loca);
this.BeginInvoke(m); //异步执行委托 }
MessageBox.Show("d");
} private void Move_Loca()
{
pic.Location = new Point(Loca+=3, 0);
}
}
}
我用线程对图片的位置进行操作,产生移动效果,为什么会闪呀闪的啊?using System;
using System.Windows.Forms;
using System.Drawing;
using System.Threading;namespace 多线程带参数
{
class Form1 : Form
{
private PictureBox pic;
Graphics g;
private int Bit_X,Loca;
Bitmap bit; public Form1()
{
Initialize();
} private void Initialize()
{
pic = new PictureBox();
pic.Size = new Size(32, 48);
pic.Location = new Point(0,0);
g = pic.CreateGraphics(); this.SuspendLayout();
this.Controls.Add(pic);
this.Size = new Size(500, 280);
this.StartPosition = FormStartPosition.CenterScreen;
this.ResumeLayout();
bit = new Bitmap("MAN.png");
Loca=Bit_X = 0;
pic.Paint += new PaintEventHandler(pic_Paint);
// CheckForIllegalCrossThreadCalls = false;
} void pic_Paint(object sender, PaintEventArgs e)
{
Thread thread1 = new Thread(new ThreadStart(ManMove));
thread1.IsBackground = true;
thread1.Start();
} private void ManMove()
{
while (pic.Location.X<460)
{
//bit.RotateFlip(RotateFlipType.Rotate90FlipY);
Rectangle rect = new Rectangle(Bit_X, 96, 32, 48);
Rectangle rectLoca = new Rectangle(0, 0, 32, 48);
g.Clear(pic.BackColor);
g.DrawImage(bit, rectLoca,rect,GraphicsUnit.Pixel);
Thread.Sleep(200);
Bit_X += 32;
if (Bit_X >= 128)
Bit_X = 0;
MethodInvoker m = new MethodInvoker(Move_Loca);
this.BeginInvoke(m); //异步执行委托 }
MessageBox.Show("d");
} private void Move_Loca()
{
pic.Location = new Point(Loca+=3, 0);
}
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货