C#实现简单气泡屏保(二)

上期续集–强化版

具体代码如下:

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;//要应用GraphicsPath这个类,命名空间中必须有Drawing.Drawing2D,因为原命名空间中没有,所以需要自行添加此命名空间.

namespace Test_PaoPao
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this.FormBorderStyle = FormBorderStyle.None;//去边框
            this.Size = new Size(200,200);
            this.Location = new Point(0,0);
            this.Opacity = 0.7;//透明度0.3
            Random s = new Random();//随机生成器
            //气泡背景颜色随机
            this.BackColor = Color.FromArgb(s.Next(0, 255), s.Next(0, 255), s.Next(0, 255));
            label1.Text = "深夜食堂";
            label1.Font = new Font("黑体",20f);//设置字体
            //实例化GraphicsPath类,拿到Bx对象
            GraphicsPath Bx = new GraphicsPath();
            //使用AddEllipse方法使窗体变为圆形
            Bx.AddEllipse(0,0,this.Width,this.Height);
            //设置与控件关联的窗体区域
            this.Region = new Region(Bx);
            timer1.Interval = 10;
            timer1.Start();
        }

        //设置变量使小球运动
        int x = 5;//水平
        int y = 5;//垂直
        private void timer1_Tick(object sender, EventArgs e)
        {
            //分析:碰到顶部和底部x不变,y取反;碰到左右两边y不变,x取反
            //分两种情况:1、先碰到底部(left不变化,top正负不断变化)  2、先碰到右边(left正负不断变化,top不变)
            this.Left += x;
            this.Top += y;
            if (this.Top+this.Height>=Screen.PrimaryScreen.WorkingArea.Height||this.Top<=0)
            {
                y = -y;
                //Form1 form1 = new Form1();//实例化Form1
                //form1.Show();//应用Form1全部属性
                //form1.Location = new Point(300,100);
                //form1.label1.Text = "久睡成瘾";
            }
            if (this.Left+this.Width>=Screen.PrimaryScreen.WorkingArea.Width||this.Left<=0)
            {
                x = -x;
                //Form1 form2 = new Form1();
                //form2.Show();
                //form2.Location = new Point(900,200);
                //form2.label1.Text = "疯狂GANK";
            }
        }
    }
}

看完的朋友记得点赞嗷,下期更精彩!!!

你可能感兴趣的:(C#实现气泡屏保,win7系统气泡屏保,气泡屏保)