Загрузка данных
#include <windows.h>
#include <iostream>
using namespace std;
// Базовый класс Фигура
class Figure
{
protected:
HDC hdc;
public:
Figure(HDC dc = NULL) : hdc(dc)
{
cout << "Constructor Figure" << endl;
}
virtual ~Figure()
{
cout << "Destructor Figure" << endl;
}
};
// Класс Прямоугольник
class RectangleFigure : virtual public Figure
{
protected:
int x1, y1, x2, y2;
public:
RectangleFigure(HDC dc, int a, int b, int c, int d)
: Figure(dc), x1(a), y1(b), x2(c), y2(d)
{
cout << "Constructor RectangleFigure" << endl;
}
void DrawRectangle()
{
::Rectangle(hdc, x1, y1, x2, y2);
}
~RectangleFigure()
{
cout << "Destructor RectangleFigure" << endl;
}
};
// Класс Окружность
class Circle : virtual public Figure
{
protected:
int radius;
public:
Circle(HDC dc, int r)
: Figure(dc), radius(r)
{
cout << "Constructor Circle" << endl;
}
void DrawCircle(int cx, int cy)
{
::Ellipse(
hdc,
cx - radius,
cy - radius,
cx + radius,
cy + radius
);
}
~Circle()
{
cout << "Destructor Circle" << endl;
}
};
// Составная фигура
class CompositeFigure :
public RectangleFigure,
public Circle
{
private:
int circleX;
int circleY;
int circleSpacing;
public:
CompositeFigure(
HDC dc,
int x1, int y1, int x2, int y2,
int cx, int cy, int r, int spacing
)
: Figure(dc),
RectangleFigure(dc, x1, y1, x2, y2),
Circle(dc, r),
circleX(cx),
circleY(cy),
circleSpacing(spacing)
{
cout << "Constructor CompositeFigure" << endl;
}
void Draw()
{
// Создаём красное перо толщиной 2 пикселя
HPEN redPen = CreatePen(PS_SOLID, 2, RGB(255, 0, 0));
if (redPen == NULL)
{
cout << "Ошибка создания пера!" << endl;
return;
}
// Сохраняем прежние настройки рисования
HGDIOBJ oldPen = SelectObject(hdc, redPen);
HGDIOBJ oldBrush = SelectObject(
hdc, GetStockObject(HOLLOW_BRUSH)
);
// Рисуем прямоугольник без заливки
DrawRectangle();
// Рисуем три соприкасающиеся окружности
int step = 2 * radius + circleSpacing;
DrawCircle(circleX, circleY);
DrawCircle(circleX + step, circleY);
DrawCircle(circleX + 2 * step, circleY);
// Восстанавливаем прежние настройки
SelectObject(hdc, oldBrush);
SelectObject(hdc, oldPen);
// Удаляем созданное перо
DeleteObject(redPen);
}
~CompositeFigure()
{
cout << "Destructor CompositeFigure" << endl;
}
};
int main()
{
// Получаем существующее консольное окно
HWND hwnd = GetConsoleWindow();
if (hwnd == NULL)
{
cout << "Окно не найдено!" << endl;
return 1;
}
// Получаем контекст устройства
HDC hdc = GetDC(hwnd);
if (hdc == NULL)
{
cout << "Ошибка получения HDC!" << endl;
return 1;
}
cout << "=== Создание объекта ===" << endl;
{
CompositeFigure figure(
hdc,
50, 50, 250, 150, // Координаты прямоугольника
100, 250, // Центр первой окружности
30, 0 // Радиус и расстояние между окружностями
);
// Рисуем фигуры красным цветом
figure.Draw();
cout << "Нажмите Enter для завершения..." << endl;
cin.get();
}
// Освобождаем контекст устройства
ReleaseDC(hwnd, hdc);
return 0;
}