Просто демонстрация работы графики) В проекте используется таймер и 4 формы)
Форма1: Рисуется случайным образом прямоугольник и эллипс в любой части формы
Форма2: Форма заполняется линиями в случайном порядке, которые имеют разную толщину и цвет. Для заполнения используется таймер
Форма3: Рисует на форме кривую вершина которой находится в точке клика мышки по форме
Форма4: С помощью таймера происходит перемещение круга и прямоугольника на форме.
Форма1:
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Random rnd=new Random();
private int x, y;
private void Form1_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
g.DrawLine(Pens.Coral, x = rnd.Next(0, 300), y = rnd.Next(0, 300), x = rnd.Next(0, 300), y = rnd.Next(10, 300));
g.FillEllipse(Brushes.Crimson, x = rnd.Next(10, 300), y = rnd.Next(10, 300), 50, 30);
g.FillRectangle(Brushes.GreenYellow, x = rnd.Next(10, 300), y = rnd.Next(10, 300), 70, 40);
}
private void button1_Click(object sender, EventArgs e)
{
Form Form2 = new Form2();
this.Hide();
Form2.Show();
}
private void button2_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void button3_Click(object sender, EventArgs e)
{
Form3 Form3 = new Form3();
this.Hide();
Form3.Show();
}
private void button4_Click(object sender, EventArgs e)
{
Form4 Form4 = new Form4();
this.Hide();
Form4.Show();
}
}
}
Форма2:
namespace WindowsFormsApplication1
{
public partial class Form2 : Form
{
Random rnd = new Random();
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form Form1 = new Form1();
this.Hide();
Form1.Show();
}
private void Form2_Load(object sender, EventArgs e)
{
timer1.Start();
}
private void timer1_Tick(object sender, EventArgs e)
{
Graphics g=this.CreateGraphics();
Pen pen=new Pen(Color.FromArgb(rnd.Next(0,256),rnd.Next(0,256),rnd.Next(0,256)),rnd.Next(1,10));
g.DrawLine(pen, rnd.Next(0, 300), rnd.Next(0, 300), rnd.Next(0, 300), rnd.Next(10, 300));
}
private void button2_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void button3_Click(object sender, EventArgs e)
{
Form Form3 = new Form3();
this.Hide();
Form3.Show();
}
private void button4_Click(object sender, EventArgs e)
{
Form Form4 = new Form4();
this.Hide();
Form4.Show();
}
}
}
Форма3:
namespace WindowsFormsApplication1
{
public partial class Form3 : Form
{
Random rnd = new Random();
public Form3()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form Form1 = new Form1();
this.Hide();
Form1.Show();
}
private void button3_Click(object sender, EventArgs e)
{
Form Form2 = new Form2();
this.Hide();
Form2.Show();
}
private void button4_Click(object sender, EventArgs e)
{
Form Form4 = new Form4();
this.Hide();
Form4.Show();
}
private void button2_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void Form3_MouseClick(object sender, MouseEventArgs e)
{
int x = e.X, y = e.Y;
Point[] pn = new Point[10];
pn[0].X = x; pn[0].Y = y;
for (int i = 1; i < 10; i++)
{
pn[i].X = rnd.Next(0, 300);
pn[i].Y = rnd.Next(0, 300);
}
Graphics g = this.CreateGraphics();
Pen pen = new Pen(Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256)), rnd.Next(1, 10));
g.DrawCurve(pen, pn);
}
}
}
Форма4:
namespace WindowsFormsApplication1
{
public partial class Form4 : Form
{
public int x, y;
Random rnd = new Random();
public Form4()
{
InitializeComponent();
g = panel1.CreateGraphics();
}
Graphics g;
private void rect(Graphics g, int x, int y)
{
g.FillEllipse(Brushes.SteelBlue, x=rnd.Next(10,300), y=rnd.Next(10,300), 20, 20);
g.FillRectangle(Brushes.Salmon, x = rnd.Next(10, 300), y = rnd.Next(10, 300), 40, 20);
}
private void button2_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void button1_Click(object sender, EventArgs e)
{
Form1 Form1 = new Form1();
this.Hide();
Form1.Show();
}
private void button3_Click(object sender, EventArgs e)
{
Form2 Form2 = new Form2();
this.Hide();
Form2.Show();
}
private void button4_Click(object sender, EventArgs e)
{
Form3 Form3 = new Form3();
this.Hide();
Form3.Show();
}
private void timer1_Tick(object sender, EventArgs e)
{
g.Clear(BackColor);
this.rect(g,x,y);
}
private void Form4_Load(object sender, EventArgs e)
{
timer1.Start();
}
}
}
Форма1: Рисуется случайным образом прямоугольник и эллипс в любой части формы
Форма2: Форма заполняется линиями в случайном порядке, которые имеют разную толщину и цвет. Для заполнения используется таймер
Форма3: Рисует на форме кривую вершина которой находится в точке клика мышки по форме
Форма4: С помощью таймера происходит перемещение круга и прямоугольника на форме.
Форма1:
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Random rnd=new Random();
private int x, y;
private void Form1_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
g.DrawLine(Pens.Coral, x = rnd.Next(0, 300), y = rnd.Next(0, 300), x = rnd.Next(0, 300), y = rnd.Next(10, 300));
g.FillEllipse(Brushes.Crimson, x = rnd.Next(10, 300), y = rnd.Next(10, 300), 50, 30);
g.FillRectangle(Brushes.GreenYellow, x = rnd.Next(10, 300), y = rnd.Next(10, 300), 70, 40);
}
private void button1_Click(object sender, EventArgs e)
{
Form Form2 = new Form2();
this.Hide();
Form2.Show();
}
private void button2_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void button3_Click(object sender, EventArgs e)
{
Form3 Form3 = new Form3();
this.Hide();
Form3.Show();
}
private void button4_Click(object sender, EventArgs e)
{
Form4 Form4 = new Form4();
this.Hide();
Form4.Show();
}
}
}
Форма2:
namespace WindowsFormsApplication1
{
public partial class Form2 : Form
{
Random rnd = new Random();
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form Form1 = new Form1();
this.Hide();
Form1.Show();
}
private void Form2_Load(object sender, EventArgs e)
{
timer1.Start();
}
private void timer1_Tick(object sender, EventArgs e)
{
Graphics g=this.CreateGraphics();
Pen pen=new Pen(Color.FromArgb(rnd.Next(0,256),rnd.Next(0,256),rnd.Next(0,256)),rnd.Next(1,10));
g.DrawLine(pen, rnd.Next(0, 300), rnd.Next(0, 300), rnd.Next(0, 300), rnd.Next(10, 300));
}
private void button2_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void button3_Click(object sender, EventArgs e)
{
Form Form3 = new Form3();
this.Hide();
Form3.Show();
}
private void button4_Click(object sender, EventArgs e)
{
Form Form4 = new Form4();
this.Hide();
Form4.Show();
}
}
}
Форма3:
namespace WindowsFormsApplication1
{
public partial class Form3 : Form
{
Random rnd = new Random();
public Form3()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form Form1 = new Form1();
this.Hide();
Form1.Show();
}
private void button3_Click(object sender, EventArgs e)
{
Form Form2 = new Form2();
this.Hide();
Form2.Show();
}
private void button4_Click(object sender, EventArgs e)
{
Form Form4 = new Form4();
this.Hide();
Form4.Show();
}
private void button2_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void Form3_MouseClick(object sender, MouseEventArgs e)
{
int x = e.X, y = e.Y;
Point[] pn = new Point[10];
pn[0].X = x; pn[0].Y = y;
for (int i = 1; i < 10; i++)
{
pn[i].X = rnd.Next(0, 300);
pn[i].Y = rnd.Next(0, 300);
}
Graphics g = this.CreateGraphics();
Pen pen = new Pen(Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256)), rnd.Next(1, 10));
g.DrawCurve(pen, pn);
}
}
}
Форма4:
namespace WindowsFormsApplication1
{
public partial class Form4 : Form
{
public int x, y;
Random rnd = new Random();
public Form4()
{
InitializeComponent();
g = panel1.CreateGraphics();
}
Graphics g;
private void rect(Graphics g, int x, int y)
{
g.FillEllipse(Brushes.SteelBlue, x=rnd.Next(10,300), y=rnd.Next(10,300), 20, 20);
g.FillRectangle(Brushes.Salmon, x = rnd.Next(10, 300), y = rnd.Next(10, 300), 40, 20);
}
private void button2_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void button1_Click(object sender, EventArgs e)
{
Form1 Form1 = new Form1();
this.Hide();
Form1.Show();
}
private void button3_Click(object sender, EventArgs e)
{
Form2 Form2 = new Form2();
this.Hide();
Form2.Show();
}
private void button4_Click(object sender, EventArgs e)
{
Form3 Form3 = new Form3();
this.Hide();
Form3.Show();
}
private void timer1_Tick(object sender, EventArgs e)
{
g.Clear(BackColor);
this.rect(g,x,y);
}
private void Form4_Load(object sender, EventArgs e)
{
timer1.Start();
}
}
}
Комментариев нет:
Отправить комментарий
Примечание. Отправлять комментарии могут только участники этого блога.