1 使用说明
- 添加一个Form1,将Form1.cs的代码替换掉
- 并引入工具类
2 Form代码
using System;
using System.Drawing;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;namespace MVC模式实例
{public partial class Form1 : Form{private const string BoyName = "张娇娇", GirlName = "刘亚弟";private int _nInterval = 10;private float _step = 5;private int _size = 400;private int _nColorIndex;private float _dK = -1, _dB = 3;private PointF _lastPoint;private int _dScreenWidth, _dScreenHeight;private E_LF _eLf = E_LF.RIGHT;private bool _isRunning = true;public Form1(){InitializeComponent();Init();}private void Form1_FormClosing(object sender, FormClosingEventArgs e){_isRunning = false;}private void Init(){SetBubbleStyle();this.Region = new Region(Bubble.CreateHeartPath(this, _size));_dScreenWidth = Screen.PrimaryScreen.Bounds.Width - this.Width;_dScreenHeight = Screen.PrimaryScreen.Bounds.Height - this.Height;Random ran = new Random();_dK = -1;_dB = ran.Next(0, _dScreenHeight);_lastPoint = new PointF(0, _dB);new Task(BubbleMoveThread).Start();}private void RefreshForm(Action eventRefresh){if (InvokeRequired){Invoke(new EventHandler(delegate { RefreshForm(eventRefresh); }));}else{eventRefresh?.Invoke();}}private void BubbleMoveThread(){while (_isRunning){try{Thread.Sleep(_nInterval);UpdataLocation(out float x, out float y);_lastPoint.X = x;_lastPoint.Y = y;RefreshForm(() =>{this.Left = (int)_lastPoint.X;this.Top = (int)_lastPoint.Y;});}catch (Exception ex){Console.WriteLine(ex.Message);}}}private void SetBubbleStyle(){RefreshForm(() =>{try{_nColorIndex = (_nColorIndex + 5) % Bubble.BubbleColor.Length;this.BackColor = Bubble.BubbleColor[_nColorIndex];label1.Text = _dK > 0 ? BoyName : GirlName;label1.ForeColor = Bubble.GetInverseColor(Bubble.BubbleColor[_nColorIndex]);}catch{}});}private void UpdataKB(){SetBubbleStyle();_dK *= -1;_dB = _lastPoint.Y - _dK * _lastPoint.X;}private void UpdataLocation(out float x, out float y){x = _lastPoint.X + (_eLf == E_LF.RIGHT ? _step : -_step);if (x > _dScreenWidth){UpdataKB();_eLf = E_LF.LEFT;}if (x < 0){UpdataKB();_eLf = E_LF.RIGHT;}x = _lastPoint.X + (_eLf == E_LF.RIGHT ? _step : -_step);y = GetY(x);if (y < 0 || y > _dScreenHeight){UpdataKB();UpdataLocation(out x, out y);}}private float GetY(float x) => _dK * x + _dB;public enum E_LF { LEFT, RIGHT }}
}
3 工具栏代码
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Reflection;
using System.Windows.Forms;namespace MVC模式实例
{public class Bubble{public static Color[] BubbleColor;static Bubble(){BubbleColor = GetCommonColors();}private static void SetFormStyle(Form1 form, float size){form.ShowInTaskbar = false;form.FormBorderStyle = FormBorderStyle.None; form.TopMost = true;form.Text = "气泡碰撞案例"; form.Opacity = 0.7; form.Size = new Size((int)size, (int)size);}public static GraphicsPath CreateCirclePath(Form1 form, float size){SetFormStyle(form, size);GraphicsPath path = new GraphicsPath();path.AddEllipse(0, 0, size, size);return path;}public static GraphicsPath CreateHeartPath(Form1 form, float size){SetFormStyle(form, size);GraphicsPath path = new GraphicsPath();float width = size;float height = size - size / 9;float t;for (t = 0; t <= 2 * Math.PI; t += 0.01f){float x = 16 * (float)Math.Pow(Math.Sin(t), 3);float y = -(13 * (float)Math.Cos(t) - 5 * (float)Math.Cos(2 * t) - 2 * (float)Math.Cos(3 * t) - (float)Math.Cos(4 * t));x = x * width / 32 + width / 2;y = y * height / 26 + height / 2;if (t == 0){path.StartFigure();}else{path.AddLine(new PointF(x, y), new PointF(x, y));}}path.CloseFigure();return path;}public static Color GetInverseColor(Color color){int red = 255 - color.R;int green = 255 - color.G;int blue = 255 - color.B;return Color.FromArgb(red, green, blue);}public static Color[] GetCommonColors(){var colors = new List<Color>();var properties = typeof(Color).GetProperties(BindingFlags.Public | BindingFlags.Static);for (var index = 0; index < properties.Length; index++){var property = properties[index];if (property.PropertyType == typeof(Color)){Color color = (Color)property.GetValue(null);colors.Add(color);}}return colors.ToArray();}}
}