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 WindowsFormsApp2
{
public partial class Form1 : Form
{
private Rectangle[] savedRects;
private Color[] savedColors;
public Form1()
{
InitializeComponent();
this.DoubleBuffered = true;
SaveSquares();
}
private void SaveSquares()
{
Random rand = new Random();
savedRects = new Rectangle[50];
savedColors = new Color[50];
for (int i = 0; i < 50; ++i)
{
int w = rand.Next(20, 100);
int h = rand.Next(20, 100);
int x = rand.Next(this.ClientSize.Width - w);
int y = rand.Next(this.ClientSize.Height - h);
savedRects[i] = new Rectangle(x, y, w, h);
savedColors[i] = Color.FromArgb(rand.Next(256), rand.Next(256), rand.Next(256));
}
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
for (int i = 0; i < 50; ++i)
{
using (SolidBrush brush = new SolidBrush(savedColors[i]))
e.Graphics.FillRectangle(brush, savedRects[i]);
using (Pen pen = new Pen(Color.Black, 1))
e.Graphics.DrawRectangle(pen, savedRects[i].X, savedRects[i].Y, savedRects[i].Width, savedRects[i].Height);
}
}
private void Form1_Resize(object sender, EventArgs e)
{
Invalidate();
}
}
}