Dymetric Header file
#pragma once
#include "resource.h"
Dymetric Class file
#include "stdafx.h"
#include "Dymetric.h"
#include "Facet.h"
#define MAX_LOADSTRING 100
HINSTANCE hInst;
WCHAR szTitle[MAX_LOADSTRING];
WCHAR szWindowClass[MAX_LOADSTRING];
COLORREF window_background_color = RGB(192, 192, 192);
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM);
int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
_In_opt_ HINSTANCE hPrevInstance,
_In_ LPWSTR lpCmdLine,
_In_ int nCmdShow)
{
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);
LoadStringW(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadStringW(hInstance, IDC_DYMETRIC, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance);
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}
HACCEL hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_DYMETRIC));
MSG msg;
while (GetMessage(&msg, nullptr, 0, 0))
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return (int) msg.wParam;
}
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEXW wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_DYMETRIC));
wcex.hCursor = LoadCursor(nullptr, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = MAKEINTRESOURCEW(IDC_DYMETRIC);
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));
return RegisterClassExW(&wcex);
}
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
hInst = hInstance;
HWND hWnd = CreateWindowW(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, nullptr, nullptr, hInstance, nullptr);
if (!hWnd)
{
return FALSE;
}
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
return TRUE;
}
HBITMAP hBitmap = NULL;
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int window_width = NULL;
int window_height = NULL;
RECT window_rect;
int button_x_position = 100;
int button_y_position = 10;
int button_width = 150;
int button_height = 50;
if (GetClientRect(hWnd, &window_rect))
{
window_width = window_rect.right - window_rect.left;
window_height = window_rect.bottom - window_rect.top;
button_x_position = window_width / 2 - button_width / 2;
}
Facet * window_details = new Facet(hWnd, window_width, window_height);
switch (message)
{
case WM_CREATE:
{
if (window_width != NULL)
{
CreateWindow(L"BUTTON", L"", WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON | BS_OWNERDRAW,
button_x_position, button_y_position, button_width, button_height, hWnd, (HMENU)12321, hInst, NULL);
}
return 0;
}
case WM_COMMAND:
{
int wmId = LOWORD(wParam);
switch (wmId)
{
case IDM_ABOUT:
DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
break;
case IDM_EXIT:
DestroyWindow(hWnd);
break;
default:
if (window_details->actionPerformed(hWnd, wParam, lParam))
return 0;
return DefWindowProc(hWnd, message, wParam, lParam);
}
}
break;
case WM_DRAWITEM:
if (window_width != NULL)
return window_details->decorateButton(wParam, lParam);
break;
case WM_CTLCOLORBTN:
{
return (LRESULT)CreateSolidBrush(window_background_color);
}
case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hWnd, &ps);
if (window_width != NULL) {
FillRect(hdc, &ps.rcPaint, (HBRUSH)CreateSolidBrush(window_background_color));
MoveToEx(hdc, 0, button_height + 2 * button_y_position, NULL);
LineTo(hdc, window_width, button_height + 2 * button_y_position);
window_details->informPaint();
}
else {
MessageBox(hWnd, L"Something is not up to specification!", L"Version Error!", MB_ICONEXCLAMATION);
}
EndPaint(hWnd, &ps);
}
break;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
delete window_details;
return 0;
}
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
UNREFERENCED_PARAMETER(lParam);
switch (message)
{
case WM_INITDIALOG:
return (INT_PTR)TRUE;
case WM_COMMAND:
if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
{
EndDialog(hDlg, LOWORD(wParam));
return (INT_PTR)TRUE;
}
break;
}
return (INT_PTR)FALSE;
}
Frame/Button Header file
#pragma once
class Facet
{
public:
Facet(HWND, int, int);
virtual ~Facet();
bool decorateButton(WPARAM, LPARAM);
bool actionPerformed(HWND, WPARAM, LPARAM);
void informPaint();
};
Frame/Button Class file
#include "stdafx.h"
#include "Facet.h"
#include "MovingBody.h"
MovingBody* traction;
Facet::Facet(HWND hWnd, int window_width, int window_height)
{
traction = new MovingBody(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("GLIDE"), -1, &rect, DT_SINGLELINE | DT_CENTER | DT_VCENTER);
return TRUE;
}
return FALSE;
}
void Facet::informPaint() {
traction->paint();
}
bool Facet::actionPerformed(HWND hWnd, WPARAM wParam, LPARAM lParam)
{
switch (LOWORD(wParam))
{
case 12321:
traction->doGlide();
return TRUE;
default:
return FALSE;
}
}
Facet::~Facet()
{
delete traction;
}
MovingBody Header file
#pragma once
class MovingBody
{
public:
MovingBody(HWND, int, int);
virtual ~MovingBody();
void paint();
void doGlide();
protected:
HWND hWindow;
HDC hdc;
int window_width;
int window_height;
COLORREF ball_colour;
int x;
int y;
HPEN background_pen;
HBRUSH background_brush;
HPEN ball_pen;
HBRUSH ball_brush;
};
C++ code for MovingBody Class file
#include "stdafx.h"
#include "MovingBody.h"
#define aWIDTH 80
#define aHEIGHT 80
MovingBody::MovingBody(HWND hWnd, int window_width, int window_height)
{
hWindow = hWnd;
this->window_width = window_width;
this->window_height = window_height;
ball_colour = RGB(255, 0, 0);
x = 10;
y = 110;
background_pen = CreatePen(PS_SOLID, 1, RGB(192, 192, 192));
background_brush = CreateSolidBrush(RGB(192, 192, 192));
ball_pen = CreatePen(PS_SOLID, 1, RGB(0, 0, 0));
ball_brush = CreateSolidBrush(ball_colour);
hdc = GetDC(hWindow);
}
void MovingBody::paint() {
SelectObject(hdc, background_pen);
SelectObject(hdc, background_brush);
Ellipse(hdc, x - 10, y, x - 10 + aWIDTH, y + aHEIGHT);
SelectObject(hdc, ball_pen);
SelectObject(hdc, ball_brush);
Ellipse(hdc, x, y, x + aWIDTH, y + aHEIGHT);
}
void MovingBody::doGlide() {
while (x + aWIDTH <= window_width) {
paint();
x += 10;
Sleep(50);
}
}
MovingBody::~MovingBody()
{
DeleteObject(background_pen);
DeleteObject(background_brush);
DeleteObject(ball_pen);
DeleteObject(ball_brush);
ReleaseDC(hWindow, hdc);
}