Facet class file
#include "stdafx.h"
#include "Facet.h"
#include "EllipticalRegion.h"
EllipticalRegion* elp_region;
Facet::Facet(HWND hWnd, int window_width, int window_height)
{
elp_region = new EllipticalRegion(hWnd, window_width, window_height);
}
bool Facet::decorateButton(WPARAM wParam, LPARAM lParam) {
if (wParam == 12321)
{
LPDRAWITEMSTRUCT lpDIS = (LPDRAWITEMSTRUCT)lParam;
SetDCBrushColor(lpDIS->hDC, RGB(255, 192, 203));
SelectObject(lpDIS->hDC, GetStockObject(DC_BRUSH));
RECT rect = { 0 };
rect.left = lpDIS->rcItem.left;
rect.right = lpDIS->rcItem.right;
rect.top = lpDIS->rcItem.top;
rect.bottom = lpDIS->rcItem.bottom;
RoundRect(lpDIS->hDC, rect.left, rect.top, rect.right, rect.bottom, 50, 50);
DrawText(lpDIS->hDC, TEXT("MOVE"), -1, &rect, DT_SINGLELINE | DT_CENTER | DT_VCENTER);
return TRUE;
}
return FALSE;
}
void Facet::informPaint() {
elp_region->paint();
}
bool Facet::actionPerformed(HWND hWnd, WPARAM wParam, LPARAM lParam)
{
switch (LOWORD(wParam))
{
case 12321:
elp_region->ellipsedSquare();
return TRUE;
default:
return FALSE;
}
}
Facet::~Facet()
{
delete elp_region;
}
EllipticalRegion Header file
#pragma once
#define sqLENGTH 100
class EllipticalRegion
{
public:
EllipticalRegion(HWND, int, int);
virtual ~EllipticalRegion();
void paint();
void ellipsedSquare();
protected:
HWND hWindow;
HDC hdc;
int window_width;
int window_height;
COLORREF sq_colour;
int x_square;
int y_square;
int previous_x;
int previous_y;
int h;
int k;
const int a = 200;
const int b = 125;
int x;
int y;
HBRUSH background_brush;
HPEN circle_pen;
HBRUSH sq_brush;
};
C++ code for EllipticalRegion Class file
#include "stdafx.h"
#include "EllipticalRegion.h"
#include <math.h>
EllipticalRegion::EllipticalRegion(HWND hWnd, int window_width, int window_height)
{
hWindow = hWnd;
this->window_width = window_width;
this->window_height = window_height;
sq_colour = RGB(255, 255, 0);
x_square = 10;
y_square = 250;
previous_x = x_square;
previous_y = y_square;
h = 450;
k = 275;
x = h - a;
y = k - b;
background_brush = CreateSolidBrush(RGB(192, 192, 192));
circle_pen = CreatePen(PS_SOLID, 5, RGB(255, 0, 255));
sq_brush = CreateSolidBrush(sq_colour);
hdc = GetDC(hWindow);
}
void EllipticalRegion::paint() {
SelectObject(hdc, circle_pen);
SelectObject(hdc, background_brush);
Ellipse(hdc, x, y, h + a, k + b);
RECT prev_sq;
prev_sq.left = previous_x;
prev_sq.top = previous_y;
prev_sq.right = previous_x + sqLENGTH;
prev_sq.bottom = previous_y + sqLENGTH;
FillRect(hdc, &prev_sq, background_brush);
RECT sq;
sq.left = x_square;
sq.top = y_square;
sq.right = x_square + sqLENGTH;
sq.bottom = y_square + sqLENGTH;
FillRect(hdc, &sq, sq_brush);
previous_x = x_square;
previous_y = y_square;
}
void EllipticalRegion::ellipsedSquare() {
while (x_square + sqLENGTH < window_width) {
int square_left = x_square;
int square_right = x_square + sqLENGTH;
int square_top = y_square;
int square_bottom = y_square + sqLENGTH;
int x_left_det = (int)round(((double)b / a)
* sqrt(pow(a, 2) - pow((square_left - h), 2)));
int x_right_det = (int)round(((double)b / a)
* sqrt(pow(a, 2) - pow((square_right - h), 2)));
int y_up_det = (int)round(((double)a / b)
* sqrt(pow(b, 2) - pow((square_top - k), 2)));
int y_down_det = (int)round(((double)a / b)
* sqrt(pow(b, 2) - pow((square_bottom - k), 2)));
if (square_top > k - x_left_det && square_bottom < k + x_left_det
&& square_top > k - x_right_det && square_bottom < k + x_right_det
&& square_left > h - y_up_det && square_right < h + y_up_det
&& square_left > h - y_down_det && square_right < h + y_down_det) {
sq_colour = RGB(0, 255, 0);
}
else {
sq_colour = RGB(255, 255, 0);
}
DeleteObject(sq_brush);
sq_brush = CreateSolidBrush(sq_colour);
paint();
x_square += 10;
Sleep(50);
}
}
EllipticalRegion::~EllipticalRegion()
{
DeleteObject(background_brush);
DeleteObject(circle_pen);
DeleteObject(sq_brush);
ReleaseDC(hWindow, hdc);
}