From 572fe4c07c634c11010731d1f131b94cc1cf76cf Mon Sep 17 00:00:00 2001 From: Pat Thoyts Date: Mon, 21 Sep 2020 08:47:13 +0100 Subject: [PATCH] Draw the polygon rather than a rect. --- PolyButton.cpp | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/PolyButton.cpp b/PolyButton.cpp index 65d6932..122e972 100644 --- a/PolyButton.cpp +++ b/PolyButton.cpp @@ -1,8 +1,12 @@ +#define _USE_MATH_DEFINES + #include "PolyButton.h" #include "ButtonProvider.h" #include +#include #include #include +#include static LRESULT CALLBACK WndProc(HWND hwnd, UINT messageId, WPARAM wparam, LPARAM lparam); @@ -128,6 +132,24 @@ inline Gdiplus::RectF RectItoF (const Gdiplus::Rect &rc) return Gdiplus::RectF(float(rc.X), float(rc.Y), float(rc.Width), float(rc.Height)); } +std::vector CalculatePoints(Gdiplus::Rect rc, int sidesCount) +{ + std::vector points; + Gdiplus::Point center(rc.Width / 2, rc.Height / 2); + Gdiplus::Size radius(rc.Width / 2, rc.Height / 2); + double angle = 3 * M_PI_2; + double delta = 2 * M_PI / sidesCount; + for (int n = 0; n < sidesCount; ++n) + { + Gdiplus::Point pt; + pt.X = static_cast(radius.Width * cos(angle) + center.X + 0.5); + pt.Y = static_cast(radius.Height * sin(angle) + center.Y + 0.5); + points.push_back(pt); + angle += delta; + } + return points; +} + void PolyButton::OnPaint() { PAINTSTRUCT ps = {0}; @@ -141,9 +163,10 @@ void PolyButton::OnPaint() Gdiplus::SolidBrush redBrush(Gdiplus::Color::Red); Gdiplus::SolidBrush greenBrush(Gdiplus::Color::Green); Gdiplus::Brush *brush = IsChecked() ? &redBrush : &greenBrush; - g.FillRectangle(brush, clientRect); - g.DrawRectangle(&pen, clientRect); - + std::vector points = CalculatePoints(clientRect, _sides); + g.FillPolygon(brush, points.data(), static_cast(points.size())); + g.DrawPolygon(&pen, points.data(), static_cast(points.size())); + Gdiplus::SolidBrush textBrush(Gdiplus::Color::Black); Gdiplus::Font font(L"Segoe UI", 10); Gdiplus::RectF layoutRect(float(clientRect.X), float(clientRect.Y), float(clientRect.Width), float(clientRect.Height)); -- 2.23.0