生活随笔
收集整理的这篇文章主要介绍了
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#实现简单气泡屏保(一)的全部内容,希望文章能够帮你解决所遇到的问题。
如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。