1. 程式人生 > >c#屏保2(斜著跑)

c#屏保2(斜著跑)

窗口裡添加個一個label;
程式碼:一個timer實現

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;

namespace _02氣泡屏保_斜著_
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        Timer timer1 = new Timer();
        private void Form1_Load(object sender, EventArgs e)
        {
            this.FormBorderStyle = FormBorderStyle.None;
            this.WindowState = FormWindowState.Maximized;
            label1.Text = "      不許碰  \r\n   不許碰不許碰 \r\n不許碰不許碰不許碰";
            label1.Font = new Font("宋體", 40);
            label1.AutoSize = true;
            timer1.Interval = 10;
            timer1.Tick += Timer1_Tick;
            timer1.Start();
        }
        int x = 5;
        int y = 5;
        private void Timer1_Tick(object sender, EventArgs e)
        {
            label1.Left += x;
            label1.Top += y;
            Random r = new Random();
            if (label1.Top + label1.Height >= this.Height)
            {
                y = -5;
                label1.ForeColor = Color.FromArgb(r.Next(255), r.Next(255), r.Next(255));
            }
             if (label1.Left + label1.Width >= this.Width)
            {
                x = -5;
                label1.ForeColor = Color.FromArgb(r.Next(255), r.Next(255), r.Next(255));
            }
             if (label1.Top <= 0)
            {
                y = 5;
                label1.ForeColor = Color.FromArgb(r.Next(255), r.Next(255), r.Next(255));
            }
             if (label1.Left <= 0)
            {
                x = 5;
                label1.ForeColor = Color.FromArgb(r.Next(255), r.Next(255), r.Next(255));
            }
        }
    }
}

一個timer雙判斷:視窗新增一個label一個timer
程式碼:

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;

namespace _03氣泡屏保_斜著_雙判斷
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this.FormBorderStyle = FormBorderStyle.None;
            this.WindowState = FormWindowState.Maximized;
            label1.Text = "不許碰";
            label1.Font = new Font("宋體", 40);
            label1.AutoSize = true;
        }
        int a = 10;
        int b = 10;
        private void timer1_Tick(object sender, EventArgs e)
        {
            label1.Left = label1.Left + a;
            label1.Top = label1.Top + b;
            if (a>0 && b>0)
            {
                if (label1.Top+label1.Height>=this.Height)
                {
                    a = 10;
                    b = -10;
                }
                if (label1.Left+label1.Width>=this.Width)
                {
                    a = -10;
                    b = 10;
                }
            }
            if (a>0 && b<0)
            {
                label1.Left = label1.Left + 3;
                label1.Top = label1.Top - 3;
                if (label1.Left+label1.Width>=this.Width)
                {
                    a = -10;
                    b = -10;
                }
                if (label1.Top<=0)
                {
                    a = 10;
                    b = 10;
                }
            }
            if (a<0 && b<0)
            {
                label1.Left = label1.Left - 3;
                label1.Top = label1.Top - 3;
                if (label1.Top<=0)
                {
                    a = -10;
                    b = 10;
                }
                if (label1.Left<=0)
                {
                    a = 10;
                    b = -10;
                }
            }
            if (a<0 && b>0)
            {
                label1.Left = label1.Left - 3;
                label1.Top = label1.Top + 3;
                if (label1.Left<=0)
                {
                    a = 10;
                    b = 10;
                }
                if (label1.Top+label1.Height>=this.Height)
                {
                    a = -10;
                    b = -10;
                }
            }
        }
    }
}