winform 中 实现 动态线条背景效果
1.首先创建一个窗体。设置FormBoderStyle为 none;
2.拖入一个panle 到窗体中,设置Dock 属性,让panle填满整个 窗体。修改panel的Name为Login_back
3. 代码
1 public partial class Login1 : Form 2 { 3 private List<PointInfo> list =new List<PointInfo>(); 4 private Random random = new Random(); 5 private Timer timer ; 6 public Login1() 7 { 8 9 InitializeComponent(); 10 InitPoint(); 11 InitializeTimer(); 12 } 13 protected override void OnPaint(PaintEventArgs e) 14 { 15 base.OnPaint(e); 16 DrawBackground(e.Graphics,100); 17 } 18 private void DrawBackground(Graphics g,int maxdistance) 19 { 20 foreach(var item in list) 21 { 22 Point point = item.Location; 23 g.FillEllipse(Brushes.Gray, point.X - 2, point.Y - 2, 4, 4); 24 } 25 //连线 26 Pen pen = new Pen(Color.AliceBlue); 27 for (int i = 0; i < list.Count; i++) 28 { 29 Point p1 = list[i].Location; 30 for (int j = i + 1; j < list.Count; j++) 31 { 32 Point p2 = list[j].Location; 33 double distance = Math.Sqrt(Math.Pow(p1.X - p2.X, 2) + Math.Pow(p1.Y - p2.Y, 2)); 34 if (distance > maxdistance) continue; 35 36 pen.Color = Color.FromArgb(200, 200, 200); 37 g.DrawLine(pen, p1, p2); 38 39 } 40 } 41 42 43 } 44 //初始化点,移动方向和速度 45 private void InitPoint() 46 { 47 //list = new List<Point>(); 48 //random = new Random(); 49 //画点 50 for(int i = 0; i < 20; i++) 51 { 52 Point point = new Point() 53 { 54 X = random.Next(ClientSize.Width), 55 Y = random.Next(ClientSize.Height) 56 57 }; 58 MoveDirection direction = (MoveDirection)random.Next(4); 59 int speed = random.Next(1, 2); 60 PointInfo pointInfo = new PointInfo { Location = point, Direction = direction, Speed = speed }; 61 list.Add(pointInfo); 62 } 63 64 65 } 66 private void InitializeTimer() 67 { 68 timer = new Timer(); 69 timer.Interval = 50; 70 timer.Tick += Timer_Tick; 71 timer.Start(); 72 } 73 74 private void Timer_Tick(object sender, EventArgs e) 75 { 76 UpdatePoints(); 77 Login_back.Invalidate(); 78 } 79 80 //减少闪烁 81 protected override CreateParams CreateParams 82 { 83 get 84 { 85 var cp = base.CreateParams; 86 cp.ExStyle |= 0x02000000; 87 return cp; 88 } 89 } 90 91 92 93 private void UpdatePoints() 94 { 95 foreach (var item in list) 96 { 97 Point point = item.Location; 98 MoveDirection direction = item.Direction; 99 int speed = item.Speed; 100 switch (direction) 101 { 102 case MoveDirection.Left: 103 point.X -= speed; 104 if (point.X < 0) point.X = Login_back.Width; 105 break; 106 case MoveDirection.Right: 107 point.X += speed; 108 if (point.X > Login_back.Width) point.X = 0; 109 break; 110 case MoveDirection.Down: 111 point.Y += speed; 112 if (point.Y > Login_back.Height) point.Y = 0; 113 break; 114 115 case MoveDirection.Up: 116 point.Y -= speed; 117 if (point.Y < 0) point.Y = Login_back.Height; 118 break; 119 } 120 item.Location = point; 121 } 122 123 } 124 125 126 127 }
4.PointInfo 类
1 public class PointInfo 2 { 3 public Point Location { get; set; } 4 public MoveDirection Direction { get; set; } 5 public int Speed { get; set; } 6 } 7 8 public enum MoveDirection 9 { 10 Left, 11 Right, 12 Up, 13 Down 14 }
5.效果图