控件移动,触碰边缘改变方向
private async void Form1_Load(object sender, EventArgs e) { var pic = new PictureBox(); pic.ImageLocation = "Image\\1.jpg"; pic.Location = new Point(0, 0); pic.Size = new Size(120, 80); pic.SizeMode = PictureBoxSizeMode.Zoom; Controls.Add(pic); void SetDirection(out int a, out int b) { var rnd = new Random(); a = (int) (rnd.NextDouble() * 50) - 25; b = (int) (rnd.NextDouble() * 50) - 25; } SetDirection(out int x, out int y); while (true) { pic.Location = new Point(pic.Location.X + x, pic.Location.Y + y); if (pic.Left >= 0 && pic.Left <= ClientSize.Width - pic.Size.Width && pic.Top >= 0 && pic.Top <= ClientSize.Height - pic.Size.Height) { await Task.Delay(50); } else { pic.Location = new Point(pic.Location.X - x, pic.Location.Y - y); SetDirection(out x, out y); } } }
相关推荐
-
将银行卡号按4位数字空格分隔 csharp
2019-1-7
-
rsa加签验签 csharp
2019-1-7
-
C#创建/读取/删除txt文件 csharp
2019-1-7
-
assetbundle导出 csharp
2019-1-7
-
压缩图片大小(像素) csharp
2019-1-7
-
asp.net core 图片文件上传 csharp
2019-1-7
-
.NET Core 使用RSA算法 加密\解密\签名\验证签名 csharp
2019-1-8
-
PDF文件转换 excel、world、PowerPoing文件转换成PDF csharp
2019-1-7
-
C#相对地址转绝对路径 csharp
2019-1-7
-
将一个数字转换为对应中文大写的正则表达式实现,非常简洁 csharp
2019-1-7