首先看一下运行之后的效果图
接下来就是代码了
using System;using System.Collections.Generic;using ponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using System.Drawing.Drawing2D;namespace FromPaoPao{public partial class Form1 : Form{public Form1(){InitializeComponent();}private void Form1_Load(object sender, EventArgs e){GraphicsPath g = new GraphicsPath();g.AddEllipse(0,0,this.Width,this.Height);this.Region = new Region(g);this.Location = new Point(Screen.PrimaryScreen.WorkingArea.Width - this.Width, Screen.PrimaryScreen.WorkingArea.Height - this.Height);timer1.Start();}private void timer1_Tick(object sender, EventArgs e){if (this.Top > 0 && this.Left > 0 && this.Left >= Screen.PrimaryScreen.WorkingArea.Width - this.Width){this.Top -= 3;}else if (this.Left >0 && this.Top<=0){this.Left -= 3;}else if (this.Bottom <= Screen.PrimaryScreen.WorkingArea.Height){this.Top += 3;}else if (this.Right <= Screen.PrimaryScreen.WorkingArea.Width ){this.Left+=3;}}private void timer2_Tick(object sender, EventArgs e){}private void button1_Click(object sender, EventArgs e){}}}
原理就是通过判断屏幕的距离来改变泡泡的方向!布局比较简单,就是在窗口上面放置一个震动的按钮
using System;using System.Collections.Generic;using ponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using System.Threading;using System.Drawing.Drawing2D;namespace FromPaoPao{public partial class Form2 : Form{public Form2(){InitializeComponent();}private void Form2_Load(object sender, EventArgs e){/* GraphicsPath g = new GraphicsPath();g.AddEllipse(0, 0, this.Width, this.Height);this.Region = new Region(g);*/}private void button1_Click(object sender, EventArgs e){int x = this.Location.X;int y = this.Location.Y;for(int i=0;i<=10;i++){this.Location = new Point(x-100,y);this.BackColor = Color.AntiqueWhite;Thread.Sleep(60);this.Location = new Point(x-100,y+100);this.BackColor = Color.Aqua;Thread.Sleep(60);this.Location = new Point(x, y + 100);this.BackColor = Color.Azure;Thread.Sleep(60);this.Location = new Point(x + 100, y + 100);this.BackColor = Color.BlanchedAlmond;Thread.Sleep(60);this.Location = new Point(x + 100, y);this.BackColor = Color.Crimson;Thread.Sleep(60);this.Location = new Point(x + 100, y - 100);this.BackColor = Color.DarkCyan;Thread.Sleep(60);this.Location = new Point(x, y - 100);this.BackColor = Color.DarkTurquoise;Thread.Sleep(60);this.Location = new Point(x - 100, y - 100);this.BackColor = Color.Red;Thread.Sleep(60);}MessageBox.Show("你知道震动了多少圈吗?猜出有奖","看一看");this.Location = new Point(x, y);}}}