if(this.Top+this.Height>=Screen.PrimaryScreen.Bounds.Height||this.Top<=0)//判斷窗體高度加上上邊緣高度與桌面高度{
y =-y;}if(this.Left+this.Width>=Screen.PrimaryScreen.Bounds.Width||this.Left<=0)//判斷窗體寬度加上左邊緣高度與桌面寬度{
x =-x;}
(4).整體代碼的實現
privatevoid 氣泡案例_Load(object sender,EventArgs e){this.Location =newPoint(0,0);//設置窗體位置this.FormBorderStyle = FormBorderStyle.None;//設置窗體無邊框this.Size =newSize(200,200);//設置窗體大小this.Opacity =0.4;//設置不透明度this.BackColor = Color.Blue;//設置背景顏色//將窗體畫成圓形,實例化GraphicsPath path =newGraphicsPath();
path.AddEllipse(0,0,this.Width,this.Height);this.Region =newRegion(path);
timer1.Start();//開啓計時器}int x =5;int y =8;privatevoidtimer1_Tick(object sender,EventArgs e){this.Top += y;this.Left += x;if(this.Top+this.Height>=Screen.PrimaryScreen.Bounds.Height||this.Top<=0)//判斷窗體高度加上上邊緣高度與桌面高度{
y =-y;}if(this.Left+this.Width>=Screen.PrimaryScreen.Bounds.Width||this.Left<=0)//判斷窗體寬度加上左邊緣高度與桌面寬度{
x =-x;}}