I'm writing a C++ wxWidgets calculator application, and I want the font of my wxTextCtrl's and my custom buttons to scale when I resize the window.
The problems are:
- The text in my buttons isn't always precisely in the center, but sometimes slightly off (especially in the green and red buttons)
- When I maximize the window, the wxTextCtrl's font size updates, but not when I minimize it, leaving it to cover half the screen until I resize the window, at which point it updates to the correct size.
I'm using this code:
text_controls.cpp
MainText = new wxTextCtrl(this, wxID_ANY, "", wxDefaultPosition, wxDefaultSize, wxTE_READONLY | wxNO_BORDER);
MainText->SetForegroundColour(wxColour(55, 55, 55));
MainText->Bind(wxEVT_TEXT, &Main::OnTextChange, this);
MainText->Bind(wxEVT_SIZE, [this](wxSizeEvent& evt) {
evt.Skip();
MainText->SetFont(
wxFontInfo(wxSize(0, MainText->GetSize().y / 1.3))
.Family(wxFONTFAMILY_SWISS)
.FaceName("Lato")
.Bold()
);
});
And I'm using very similar code to generate the font in my custom button class file:
ikeButton.cpp
void ikeButton::render(wxDC& dc)
{
unsigned int w = this->GetSize().GetWidth();
unsigned int h = this->GetSize().GetHeight();
wxColour* bCol;
if (pressed) {
dc.SetBrush(*pressedBackgroundColor);
dc.SetTextForeground(*pressedTextColor);
bCol = pressedBorderColor;
}
else if (hovered) {
dc.SetBrush(*hoveredBackgroundColor);
dc.SetTextForeground(*hoveredTextColor);
bCol = hoveredBorderColor;
}
else {
dc.SetBrush(*backgroundColor);
dc.SetTextForeground(*textColor);
bCol = borderColor;
}
dc.SetPen(*wxTRANSPARENT_PEN);
dc.DrawRectangle(0, 0, w, h);
//bordo
if (borderTh && bCol != NULL)
{
dc.SetBrush(*bCol);
dc.DrawRectangle(0, 0, w, borderTh);
dc.DrawRectangle(w - borderTh, 0, borderTh, h);
dc.DrawRectangle(0, h - borderTh, w, borderTh);
dc.DrawRectangle(0, 0, borderTh, h);
}
//testo
dc.SetFont(
wxFontInfo(wxSize(0, this->GetSize().GetHeight() / 3))
.Family(wxFONTFAMILY_SWISS)
.FaceName("Lato")
.Light()
);
dc.DrawText(text, w / 2 - (GetTextExtent(text).GetWidth()),
h / 2 - (GetTextExtent(text).GetHeight()));
}