生活随笔
收集整理的這篇文章主要介紹了
C#实现简单气泡屏保(一)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
本次使用一個Timer實現
首先簡單分析下氣泡屏保原理
- 對窗體進行變形(圓形)
- 通過控制氣泡與屏幕左邊緣(this.Left)以及上邊緣的距離(this.Top)進而使氣泡運動
- 氣泡碰到屏幕四周進行反彈
- 通過對屏幕四個邊緣來分析(屏幕每個邊緣都有兩個方向碰撞)
具體實現代碼如下:
using System
;
using System
.Collections
.Generic
;
using System
.ComponentModel
;
using System
.Data
;
using System
.Drawing
;
using System
.Linq
;
using System
.Text
;
using System
.Threading
.Tasks
;
using System
.Windows
.Forms
;
using System
.Drawing
.Drawing2D
;namespace Test2_氣泡窗口
{public partial class Form1 : Form{public Form1(){InitializeComponent();}private void Form1_Load(object sender
, EventArgs e
){this.FormBorderStyle
= FormBorderStyle
.None
;this.Location
= new Point(0,0);this.Size
= new Size(200, 200);this.BackColor
= Color
.DeepPink
;label1
.Text
= "深夜食堂";label1
.Font
= new Font("楷體", 20);label1
.AutoSize
= true;timer1
.Start();GraphicsPath bianxing
= new GraphicsPath();bianxing
.AddEllipse(0, 0, this.Width
, this.Height
);this.Region
= new Region(bianxing
);this.Opacity
= 0.8;}int x
= 4;int y
= 4;private void timer1_Tick(object sender
, EventArgs e
){this.Left
+= x
;this.Top
+= y
;if (this.Top
+ this.Height
>= Screen
.PrimaryScreen
.WorkingArea
.Height
){if (x
> 0 && y
> 0){this.BackColor
= Color
.Blue
;x
= 4;y
= -4;}if (x
< 0 && y
> 0){this.BackColor
= Color
.Red
;x
= -4;y
= -4;}}if (this.Left
+ this.Width
>= Screen
.PrimaryScreen
.WorkingArea
.Width
){if (x
> 0 && y
> 0){this.BackColor
= Color
.DarkOrchid
;x
= -4;y
= 4;}if (x
> 0 && y
< 0){this.BackColor
= Color
.Yellow
;x
= -4;y
= -4;}}if (this.Top
<= 0){if (x
> 0 && y
< 0){this.BackColor
= Color
.Orange
;x
= 4;y
= 4;}if (x
< 0 && y
< 0){this.BackColor
= Color
.DeepSkyBlue
;x
= -4;y
= 4;}}if (this.Left
<= 0){if (x
< 0 && y
< 0){this.BackColor
= Color
.Green
;x
= 4;y
= -4;}if (x
< 0 && y
> 0){this.BackColor
= Color
.Purple
;x
= 4;y
= 4;}}}}
}
看完記得點贊嗷,后面還會有精彩素材!!!
總結
以上是生活随笔為你收集整理的C#实现简单气泡屏保(一)的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。