Лабораторная работа №3
Задание:
1. Создать проект, состоящий из главной формы и трех MDI-форм.
2. Нанести на главную форму меню, состоящее из пунктов Кружки, Мишень, Вертушка. Для каждого из пунктов обеспечить вызов соответствующей MDI-формы, присвоив ей соответствующий заголовок.
3. Заполнить форму «Кружки» окружностями диаметром 20 пикселей и отстающими друг от друга на 30 пикселей. Цвет выбрать любой. Предусмотреть возможность изменения размеров формы.
4. Заполнить форму «Мишень» концентрическими окружностями в количестве 20 штук. Диаметр первой окружности – 20 пикселей, шаг изменения диаметра – 10 пикселей. Первую окружность расположить в точке (50,50). Предусмотреть возможность изменения размеров формы.
5. Нарисовать на форме «Вертушка» круг, разделенный на 20 секторов по 30 градусов каждый. Каждый сектор закрасить в свой цвет. Предусмотреть возможность изменения размеров формы.
6. Нанести на данную форму кнопку с надписью «Верчение». Обеспечить с помощью данной кнопки динамическую смену цветов (вращение по часовой стрелке) круга, созданного в пункте 5 с помощью таймера.
7. Нанести на данную форму кнопку с надписью «Остановка». Обеспечить с помощью данной кнопки остановку смены цветов, реализованной в пункте 7. Кнопки «Верчение» и «Остановка» не должны быть доступны пользователю одновременно.
Текст программы:
Главная форма проекта:
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 Компьютерная_графика__3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void кружкиToolStripMenuItem_Click(object sender, EventArgs e)
{
Form f2 = new Form2();
f2.MdiParent = this;
f2.Show();
}
private void мишеньToolStripMenuItem_Click(object sender, EventArgs e)
{
Form f3 = new Form3();
f3.MdiParent = this;
f3.Show();
}
private void вертушкаToolStripMenuItem_Click(object sender, EventArgs e)
{
Form f4 = new Form4();
f4.MdiParent = this;
f4.Show();
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
foreach (Form f in this.MdiChildren)
{
f.Close();
}
}
}
}
Форма Кружки:
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 Компьютерная_графика__3
{
public partial class Form2 : Form
{
Graphics g;
public Form2()
{
InitializeComponent();
}
private void Form2_Paint(object sender, PaintEventArgs e)
{
Pen p;
int y = 2, x = 2;
p = new Pen(Color.Red, 5);
for (int i = 0; i<this.Size.Height / 50;i++)
{
Rectangle r = new Rectangle(x, y, 20, 20);
for (int j = 0; j < this.Size.Width / 50; j++)
{
g.DrawEllipse(p, r);
r = new Rectangle(x += 50, y, 20, 20);
}
y += 50;
x = 2;
}
}
private void Form2_Load(object sender, EventArgs e)
{
g = this.CreateGraphics();
}
private void Form2_SizeChanged(object sender, EventArgs e)
{
g = this.CreateGraphics();
}
}
}
Форма Мишень:
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 Компьютерная_графика__3
{
public partial class Form3 : Form
{
Graphics g;
public Form3()
{
InitializeComponent();
}
private void Form3_Load(object sender, EventArgs e)
{
g = this.CreateGraphics();
}
private void Form3_Paint(object sender, PaintEventArgs e)
{
g.Clear(this.BackColor);
Pen p;
int y = this.Size.Height / 2 - 20, x = this.Size.Width / 2 - 20, w = 20, h = 20;
p = new Pen(Color.Red, 5);
Rectangle r = new Rectangle(this.Size.Width / 2 - 20, this.Size.Height / 2 - 20, 20, 20);
g.DrawEllipse(p, r);
for (int j = 0; j < 20; j++)
{
g.DrawEllipse(p, r);
r = new Rectangle(x -= 10, y -= 10, w += 20, h += 20);
}
}
}
}
Форма Вертушка:
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 Компьютерная_графика__3
{
public partial class Form4 : Form
{
Graphics g;
SolidBrush b1 = new SolidBrush(Color.Green);
SolidBrush b2 = new SolidBrush(Color.LightSkyBlue);
SolidBrush b3 = new SolidBrush(Color.LightYellow);
SolidBrush b4 = new SolidBrush(Color.MediumVioletRed);
SolidBrush b5 = new SolidBrush(Color.Orange);
SolidBrush b6 = new SolidBrush(Color.Purple);
SolidBrush b7 = new SolidBrush(Color.Red);
SolidBrush b8 = new SolidBrush(Color.DarkSlateGray);
SolidBrush b9 = new SolidBrush(Color.DeepPink);
SolidBrush b10 = new SolidBrush(Color.DarkTurquoise);
SolidBrush b11 = new SolidBrush(Color.Gold);
SolidBrush b12 = new SolidBrush(Color.LemonChiffon);
SolidBrush b13 = new SolidBrush(Color.Lime);
SolidBrush b14 = new SolidBrush(Color.Magenta);
SolidBrush b15 = new SolidBrush(Color.MediumOrchid);
SolidBrush b16 = new SolidBrush(Color.Moccasin);
SolidBrush b17 = new SolidBrush(Color.Navy);
SolidBrush b18 = new SolidBrush(Color.OrangeRed);
SolidBrush b19 = new SolidBrush(Color.Pink);
SolidBrush b20 = new SolidBrush(Color.RosyBrown);
public Form4()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
button2.Visible = true;
button1.Visible = false;
timer1.Start();
}
private void button2_Click(object sender, EventArgs e)
{
button1.Visible = true;
button2.Visible = false;
timer1.Stop();
}
int j = 0;
private void timer1_Tick(object sender, EventArgs e)
{
int i = 0;
g.FillPie(b1, 50, 50, 400, 400, (i + j) * 18, 18); i++;
g.FillPie(b2, 50, 50, 400, 400, (i + j) * 18, 18); i++;
g.FillPie(b3, 50, 50, 400, 400, (i + j) * 18, 18); i++;
g.FillPie(b4, 50, 50, 400, 400, (i + j) * 18, 18); i++;
g.FillPie(b5, 50, 50, 400, 400, (i + j) * 18, 18); i++;
g.FillPie(b6, 50, 50, 400, 400, (i + j) * 18, 18); i++;
g.FillPie(b7, 50, 50, 400, 400, (i + j) * 18, 18); i++;
g.FillPie(b8, 50, 50, 400, 400, (i + j) * 18, 18); i++;
g.FillPie(b9, 50, 50, 400, 400, (i + j) * 18, 18); i++;
g.FillPie(b10, 50, 50, 400, 400, (i + j) * 18, 18); i++;
g.FillPie(b11, 50, 50, 400, 400, (i + j) * 18, 18); i++;
g.FillPie(b12, 50, 50, 400, 400, (i + j) * 18, 18); i++;
g.FillPie(b13, 50, 50, 400, 400, (i + j) * 18, 18); i++;
g.FillPie(b14, 50, 50, 400, 400, (i + j) * 18, 18); i++;
g.FillPie(b15, 50, 50, 400, 400, (i + j) * 18, 18); i++;
g.FillPie(b16, 50, 50, 400, 400, (i + j) * 18, 18); i++;
g.FillPie(b17, 50, 50, 400, 400, (i + j) * 18, 18); i++;
g.FillPie(b18, 50, 50, 400, 400, (i + j) * 18, 18); i++;
g.FillPie(b19, 50, 50, 400, 400, (i + j) * 18, 18); i++;
g.FillPie(b20, 50, 50, 400, 400, (i + j) * 18, 18); i++;
j++;
if (j == 20)
{
j = 0;
}
}
private void Form4_Paint(object sender, PaintEventArgs e)
{
g.FillPie(b1, 50, 50, 400, 400, 0, 18);
g.FillPie(b2, 50, 50, 400, 400, 18, 18);
g.FillPie(b3, 50, 50, 400, 400, 36, 18);
g.FillPie(b4, 50, 50, 400, 400, 54, 18);
g.FillPie(b5, 50, 50, 400, 400, 72, 18);
g.FillPie(b6, 50, 50, 400, 400, 90, 18);
g.FillPie(b7, 50, 50, 400, 400, 108, 18);
g.FillPie(b8, 50, 50, 400, 400, 126, 18);
g.FillPie(b9, 50, 50, 400, 400, 144, 18);
g.FillPie(b10, 50, 50, 400, 400, 162, 18);
g.FillPie(b11, 50, 50, 400, 400, 180, 18);
g.FillPie(b12, 50, 50, 400, 400, 198, 18);
g.FillPie(b13, 50, 50, 400, 400, 216, 18);
g.FillPie(b14, 50, 50, 400, 400, 234, 18);
g.FillPie(b15, 50, 50, 400, 400, 252, 18);
g.FillPie(b16, 50, 50, 400, 400, 270, 18);
g.FillPie(b17, 50, 50, 400, 400, 288, 18);
g.FillPie(b18, 50, 50, 400, 400, 306, 18);
g.FillPie(b19, 50, 50, 400, 400, 324, 18);
g.FillPie(b20, 50, 50, 400, 400, 342, 18);
}
private void Form4_Load(object sender, EventArgs e)
{
g = this.CreateGraphics();
}
}
}
Контрольный пример:
Образец главной формы проекта:
Форма Кружки:
Форма Мишень:
Форма Вертушка:
Результат работы таймера: