Fix font and thumb size of wxSlider after DPI change

This commit is contained in:
Maarten Bent
2019-08-27 23:26:27 +02:00
parent 587f894f96
commit 4713301cf5
2 changed files with 32 additions and 2 deletions

View File

@@ -122,6 +122,9 @@ protected:
WXHBRUSH DoMSWControlColor(WXHDC pDC, wxColour colBg, WXHWND hWnd) wxOVERRIDE; WXHBRUSH DoMSWControlColor(WXHDC pDC, wxColour colBg, WXHWND hWnd) wxOVERRIDE;
virtual void MSWUpdateFontOnDPIChange(const wxSize& newDPI) wxOVERRIDE;
void OnDPIChanged(wxDPIChangedEvent& event);
// the labels windows, if any // the labels windows, if any
wxSubwindows *m_labels; wxSubwindows *m_labels;

View File

@@ -163,13 +163,17 @@ bool wxSlider::Create(wxWindow *parent,
m_labels->Set(n, wnd, lblid); m_labels->Set(n, wnd, lblid);
} }
m_labels->SetFont(GetFont());
} }
// now create the main control too // now create the main control too
if ( !MSWCreateControl(TRACKBAR_CLASS, wxEmptyString, pos, size) ) if ( !MSWCreateControl(TRACKBAR_CLASS, wxEmptyString, pos, size) )
return false; return false;
if ( m_labels )
{
m_labels->SetFont(GetFont());
}
// and initialize everything // and initialize everything
SetRange(minValue, maxValue); SetRange(minValue, maxValue);
SetValue(value); SetValue(value);
@@ -183,6 +187,8 @@ bool wxSlider::Create(wxWindow *parent,
SetSize(size); SetSize(size);
} }
Bind(wxEVT_DPI_CHANGED, &wxSlider::OnDPIChanged, this);
return true; return true;
} }
@@ -541,7 +547,7 @@ void wxSlider::DoMoveWindow(int x, int y, int width, int height)
wxSize wxSlider::DoGetBestSize() const wxSize wxSlider::DoGetBestSize() const
{ {
// this value is arbitrary: // this value is arbitrary:
static const int length = FromDIP(100); const int length = FromDIP(100);
const int thumbSize = GetThumbLength(); const int thumbSize = GetThumbLength();
const int tickSize = FromDIP(TICK); const int tickSize = FromDIP(TICK);
@@ -619,6 +625,27 @@ WXHBRUSH wxSlider::DoMSWControlColor(WXHDC pDC, wxColour colBg, WXHWND hWnd)
return hBrush; return hBrush;
} }
void wxSlider::MSWUpdateFontOnDPIChange(const wxSize& newDPI)
{
wxSliderBase::MSWUpdateFontOnDPIChange(newDPI);
if ( m_labels && m_font.IsOk() )
{
m_labels->SetFont(m_font);
}
}
void wxSlider::OnDPIChanged(wxDPIChangedEvent& event)
{
int thumbLen = GetThumbLength();
const double scaleFactor = (double)event.GetNewDPI().x / event.GetOldDPI().x;
const double thumbLenScaled = thumbLen * scaleFactor;
thumbLen = (int)(scaleFactor > 1.0 ? ceil(thumbLenScaled) : floor(thumbLenScaled));
SetThumbLength(thumbLen);
}
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
// slider-specific methods // slider-specific methods
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------