110,571
社区成员
发帖
与我相关
我的任务
分享
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);
}
}
}