C#桌面两球体随机移动效果源码

新建winform窗体,添加timer并绑定事件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 | using System; using System.Drawing; using System.Drawing.Drawing2D; using System.Windows.Forms; namespace test.winForm { public partial class Form4 : Form { public Form4() { InitializeComponent(); } private void Form4_Load( object sender, EventArgs e) { //设置窗体初始位置 this .Location = new Point(0, 0); //去掉窗体边框 this .FormBorderStyle = FormBorderStyle.None; //设置窗体大小 this .Size = new Size(300, 300); //设置窗体背景颜色 this .BackColor = Color.Pink; //设置窗体不透明度 this .Opacity = 2.9; //将窗体变为圆形 GraphicsPath path = new GraphicsPath(); path.AddEllipse(0, 0, this .Width, this .Height); this .Region = new Region(path); //设置计时器频率 timer1.Interval = 10; //开启计时器 timer1.Start(); } //定义两个局部变量 int x = 8; int y = 8; Random ys = new Random(); private void timer1_Tick( object sender, EventArgs e) { //窗体距容器左距离 this .Left += x; //窗体距容器上距离 this .Top += y; //窗体碰到容器下方和上方 if ( this .Top + this .Height >= Screen.PrimaryScreen.WorkingArea.Height || this .Top <= 0) { //取反,加y变成减y y = -y; this .BackColor = Color.FromArgb(ys.Next(256), ys.Next(256), ys.Next(256)); } //窗体碰到容器右方和左方 if ( this .Left + this .Width >= Screen.PrimaryScreen.WorkingArea.Width || this .Left <= 0) { //取反,加x变成减x x = -x; this .BackColor = Color.FromArgb(ys.Next(0, 255), ys.Next(0, 255), ys.Next(0, 255)); } } } } |
再加一个窗体,同样添加timer和绑定事件
using System; using System.Drawing; using System.Drawing.Drawing2D; using System.Windows.Forms; namespace test.winForm { public partial class Form3 : Form { public Form3() { InitializeComponent(); } private void Form3_Load(object sender, EventArgs e) { //设置窗体初始位置 this.Location = new Point(0, 0); //去掉窗体边框 this.FormBorderStyle = FormBorderStyle.None; //设置窗体大小 this.Size = new Size(200, 200); //设置窗体背景颜色 this.BackColor = Color.Red; //设置窗体不透明度 this.Opacity = 2.9; //将窗体变为圆形 GraphicsPath path = new GraphicsPath(); path.AddEllipse(0, 0, this.Width, this.Height); this.Region = new Region(path); //设置计时器频率 timer1.Interval = 10; //开启计时器 timer1.Start(); Form4 fm2 = new Form4(); fm2.Show(); } //定义两个局部变量 int x = 7; int y = 7; Random ys = new Random(); private void timer1_Tick(object sender, EventArgs e) { //窗体距容器左距离 this.Left += x; //窗体距容器上距离 this.Top += y; //窗体碰到容器下方和上方 if (this.Top + this.Height >= Screen.PrimaryScreen.WorkingArea.Height || this.Top <= 0) { //取反,加y变成减y y = -y; this.BackColor = Color.FromArgb(ys.Next(256), ys.Next(256), ys.Next(256)); } //窗体碰到容器右方和左方 if (this.Left + this.Width >= Screen.PrimaryScreen.WorkingArea.Width || this.Left <= 0) { //取反,加x变成减x x = -x; this.BackColor = Color.FromArgb(ys.Next(0, 255), ys.Next(0, 255), ys.Next(0, 255)); } } } }
启动方法中设置启动窗体为form3运行起来即可
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构