#include <windows.h>
#include <iostream>
using namespace std;
// Базовый класс Фигура
class Figure
{
protected:
HDC hdc;
int x, y;
public:
Figure(HDC dc, int x0, int y0)
{
hdc = dc;
x = x0;
y = y0;
cout << "Constructor Figure" << endl;
}
virtual ~Figure()
{
cout << "Destructor Figure" << endl;
}
virtual void Draw() = 0;
};
// Класс Прямоугольник
class Rectangle : virtual public Figure
{
public:
Rectangle(HDC dc, int x0, int y0)
: Figure(dc, x0, y0)
{
cout << "Constructor Rectangle" << endl;
}
void Draw() override
{
Rectangle(hdc, x, y, x + 100, y + 60);
}
~Rectangle()
{
cout << "Destructor Rectangle" << endl;
}
};
// Класс Окружность
class Circle : virtual public Figure
{
protected:
int radius;
public:
Circle(HDC dc, int x0, int y0, int r)
: Figure(dc, x0, y0)
{
radius = r;
cout << "Constructor Circle" << endl;
}
void Draw() override
{
Ellipse(hdc, x - radius, y - radius,
x + radius, y + radius);
}
~Circle()
{
cout << "Destructor Circle" << endl;
}
};
// Составная фигура: соприкасающиеся окружности
class TouchingCircles : public Rectangle, public Circle
{
public:
TouchingCircles(HDC dc, int x0, int y0)
: Figure(dc, x0, y0),
Rectangle(dc, x0, y0),
Circle(dc, x0, y0, 30)
{
cout << "Constructor TouchingCircles" << endl;
}
void Draw() override
{
Circle::Draw();
int oldX = x;
x += 60;
Circle::Draw();
x = oldX;
int oldY = y;
y += 60;
Circle::Draw();
y = oldY;
Rectangle::Draw();
}
~TouchingCircles()
{
cout << "Destructor TouchingCircles" << endl;
}
};
// Оконная процедура
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg,
WPARAM wParam, LPARAM lParam)
{
switch (msg)
{
case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hwnd, &ps);
TouchingCircles figure(hdc, 150, 120);
figure.Draw();
EndPaint(hwnd, &ps);
break;
}
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
}
return 0;
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE,
LPSTR, int nCmdShow)
{
const char CLASS_NAME[] = "FigureWindow";
WNDCLASS wc = {};
wc.lpfnWndProc = WndProc;
wc.hInstance = hInstance;
wc.lpszClassName = CLASS_NAME;
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
RegisterClass(&wc);
HWND hwnd = CreateWindowEx(
0, CLASS_NAME, "Laboratory 4 - Variant 1.6",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, 500, 400,
NULL, NULL, hInstance, NULL);
if (!hwnd)
return 0;
ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);
MSG msg = {};
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}