C#的两个控件在窗体移动。

C#编写一个Windows应用程序,在程序中实现两个线程,分别控制两个LABEL标签的向右移动,每次移动的距离用随机数生成,到达窗体的最右边就关闭程序,初始的Left值相... C#编写一个Windows应用程序,在程序中实现两个线程,分别控制两个LABEL标签的向右移动,每次移动的距离用随机数生成,到达窗体的最右边就关闭程序,初始的Left值相同。
请老鸟帮我解答。。
展开
 我来答
yzy_130
推荐于2016-01-30 · TA获得超过810个赞
知道小有建树答主
回答量:402
采纳率:0%
帮助的人:486万
展开全部
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Threading;
using System.Security.Cryptography;

namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

Thread thread1; //线程1,控制label1
Thread thread2; //线程2,控制label2

private void Form1_Load(object sender, EventArgs e)
{
//实例化线程
thread1 = new Thread(MoveControl);
thread2 = new Thread(MoveControl);
//启动线程
thread1.Start(label1);
thread2.Start(label2);
}

//线程执行的移动方法
private void MoveControl(object o)
{
Random r = new Random(GetRandomSeed());
Control c = o as Control;
if (c != null)
{
while (c.Location.X + c.Size.Width < Width)
{
Thread.Sleep(100); //每100毫秒移动一次,可以自己改
Invoke(new Action(() =>
{
int i = r.Next(1, 10); //每次移动的最大值为10px,最小值为1px,也可以自己改
c.Location = new Point(c.Location.X + i, c.Location.Y);
}));
}
Invoke(new Action(() => Close())); //关闭程序
}
}

//获取随机种子
private int GetRandomSeed()
{
byte[] bytes = new byte[4];
new RNGCryptoServiceProvider().GetBytes(bytes);
return BitConverter.ToInt32(bytes, 0);
}
}
}

不懂的可以hi我
zy0827
2010-10-20 · TA获得超过172个赞
知道小有建树答主
回答量:479
采纳率:0%
帮助的人:288万
展开全部
试一下,我测试是没问题:
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.Threading;

namespace test
{
public partial class Form1 : Form
{
public bool isRuning = false;
public Form1()
{
InitializeComponent();
}
private void MoveOne()
{
while (label1.Location.X < this.Size.Width)
{
if (!isRuning)
break;
Random rd = new Random();
int i = rd.Next(10, 20);
label1.Invoke(new MethodInvoker(delegate
{
label1.Location = new Point(label1.Location.X + i, label1.Location.Y);
Thread.Sleep(100);
}
));
Application.DoEvents();
Thread.Sleep(100);
}
if (isRuning)
this.Invoke(new MethodInvoker(delegate
{
this.Close();
}
));
}
private void MoveTwo()
{
while (label2.Location.X < this.Size.Width)
{
if (!isRuning)
break;
Random rd = new Random();
int i = rd.Next(10, 20);
label2.Invoke(new MethodInvoker(delegate
{
label2.Location = new Point(label2.Location.X + i, label2.Location.Y);
Thread.Sleep(100);
}
));
Application.DoEvents();
Thread.Sleep(100);
}
if (isRuning)
this.Invoke(new MethodInvoker(delegate
{
this.Close();
}
));
}

private void button1_Click(object sender, EventArgs e)
{
isRuning = true;
Thread threadA = new Thread(new ThreadStart(this.MoveOne));
threadA.Start();
Thread threadB = new Thread(new ThreadStart(this.MoveTwo));
threadB.Start();
}

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
isRuning = false;
}
}
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式