Skip to content

Commit d1fda33

Browse files
authored
Use system font (#69)
Win32 controls default to an old bitmap font for historial purposes. This isn't the recommended font to use, however. This commit uses the recommended system font and responds to the user changing the font.
1 parent 1444892 commit d1fda33

File tree

8 files changed

+36
-51
lines changed

8 files changed

+36
-51
lines changed

Code/maxGUI/ButtonImplementation.cpp

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -40,18 +40,7 @@ namespace maxGUI
4040

4141
Win32String win32_text = Utf8ToWin32String(std::move(text));
4242

43-
//return CreateWindowEx(0, TEXT("BUTTON"), win32_text.text_, win32_styles, rectangle.TopLeft.X(), rectangle.TopLeft.Y(), rectangle.Width, rectangle.Height, parent_window_handle, NULL, reinterpret_cast<HINSTANCE>(GetWindowLongPtr(parent_window_handle, GWLP_HINSTANCE)), NULL);
44-
HWND window_handle = CreateWindowEx(0, TEXT("BUTTON"), win32_text.text_, win32_styles, rectangle.TopLeft.X(), rectangle.TopLeft.Y(), rectangle.Width, rectangle.Height, parent_window_handle, NULL, reinterpret_cast<HINSTANCE>(GetWindowLongPtr(parent_window_handle, GWLP_HINSTANCE)), NULL);
45-
46-
const int font_height_in_points = 10;
47-
HDC screen_device_context = GetDC(NULL);
48-
const int font_height_in_logical_units = -MulDiv(font_height_in_points, GetDeviceCaps(screen_device_context, LOGPIXELSY), 72);
49-
//const int font_height_in_logical_units = -12;
50-
HFONT font = CreateFont(font_height_in_logical_units, 0, 0, 0, FW_DONTCARE, FALSE, FALSE, FALSE, ANSI_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, FF_DONTCARE | DEFAULT_PITCH, TEXT("Segoe UI"));
51-
52-
SendMessage(window_handle, WM_SETFONT, reinterpret_cast<WPARAM>(font), TRUE);
53-
54-
return window_handle;
43+
return CreateWindowEx(0, TEXT("BUTTON"), win32_text.text_, win32_styles, rectangle.TopLeft.X(), rectangle.TopLeft.Y(), rectangle.Width, rectangle.Height, parent_window_handle, NULL, reinterpret_cast<HINSTANCE>(GetWindowLongPtr(parent_window_handle, GWLP_HINSTANCE)), NULL);
5544
}
5645
#endif
5746

Code/maxGUI/CheckBoxImplementation.cpp

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -37,24 +37,7 @@ namespace maxGUI
3737
}
3838
#pragma warning(pop)
3939
Win32String win32_text = Utf8ToWin32String(std::move(text));
40-
//return CreateWindowEx(0, TEXT("BUTTON"), win32_text.text_, win32_styles, rectangle.TopLeft.X(), rectangle.TopLeft.Y(), rectangle.Width, rectangle.Height, parent_window_handle, NULL, reinterpret_cast<HINSTANCE>(GetWindowLongPtr(parent_window_handle, GWLP_HINSTANCE)), NULL);
41-
HWND window_handle = CreateWindowEx(0, TEXT("BUTTON"), win32_text.text_, win32_styles, rectangle.TopLeft.X(), rectangle.TopLeft.Y(), rectangle.Width, rectangle.Height, parent_window_handle, NULL, reinterpret_cast<HINSTANCE>(GetWindowLongPtr(parent_window_handle, GWLP_HINSTANCE)), NULL);
42-
43-
/*
44-
LOGFONT lf;
45-
SystemParametersInfo(SPI_GETICONTITLELOGFONT, sizeof(lf), &lf, 0);
46-
HFONT font = CreateFontIndirect(&lf);
47-
*/
48-
49-
const int font_height_in_points = 10;
50-
HDC screen_device_context = GetDC(NULL);
51-
const int font_height_in_logical_units = -MulDiv(font_height_in_points, GetDeviceCaps(screen_device_context, LOGPIXELSY), 72);
52-
//const int font_height_in_logical_units = -12;
53-
HFONT font = CreateFont(font_height_in_logical_units, 0, 0, 0, FW_DONTCARE, FALSE, FALSE, FALSE, ANSI_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, FF_DONTCARE | DEFAULT_PITCH, TEXT("Segoe UI"));
54-
55-
SendMessage(window_handle, WM_SETFONT, reinterpret_cast<WPARAM>(font), TRUE);
56-
57-
return window_handle;
40+
return CreateWindowEx(0, TEXT("BUTTON"), win32_text.text_, win32_styles, rectangle.TopLeft.X(), rectangle.TopLeft.Y(), rectangle.Width, rectangle.Height, parent_window_handle, NULL, reinterpret_cast<HINSTANCE>(GetWindowLongPtr(parent_window_handle, GWLP_HINSTANCE)), NULL);
5841
}
5942

6043
bool CheckBoxImplementation::IsChecked() const noexcept {

Code/maxGUI/Control.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,27 @@ namespace maxGUI
2525
#endif
2626
}
2727

28+
void Control::NewSystemFont() noexcept {
29+
#if defined(MAX_PLATFORM_WINDOWS)
30+
// Win32 way
31+
LOGFONT lf = {0};
32+
SystemParametersInfo(SPI_GETICONTITLELOGFONT, sizeof(lf), &lf, 0);
33+
HFONT font = CreateFontIndirect(&lf);
34+
SendMessage(window_handle_, WM_SETFONT, reinterpret_cast<WPARAM>(font), TRUE);
35+
36+
// WinUI way
37+
/*
38+
//const int font_height_in_points = 10; // 14
39+
//HDC screen_device_context = GetDC(NULL);
40+
//const int font_height_in_logical_units = -MulDiv(font_height_in_points, GetDeviceCaps(screen_device_context, LOGPIXELSY), 72);
41+
const int font_height_in_logical_units = -14; // -12
42+
// Shouldn't it be "Segoe UI Variable"? Why does that look wrong?
43+
HFONT font = CreateFont(font_height_in_logical_units, 0, 0, 0, FW_DONTCARE, FALSE, FALSE, FALSE, ANSI_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, FF_DONTCARE | DEFAULT_PITCH, TEXT("Segoe UI"));
44+
SendMessage(window_handle_, WM_SETFONT, reinterpret_cast<WPARAM>(font), TRUE);
45+
*/
46+
#endif
47+
}
48+
2849
#if defined(MAX_PLATFORM_WINDOWS)
2950
void Control::OnCommand(WORD /*notification*/) noexcept
3051
{}

Code/maxGUI/Control.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ namespace maxGUI
7373

7474
//protected:
7575

76+
void NewSystemFont() noexcept;
77+
7678
#if defined(MAX_PLATFORM_WINDOWS)
7779
virtual void OnCommand(WORD notification) noexcept;
7880
#endif

Code/maxGUI/FormConcept.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
#include <utility>
88

9-
109
namespace maxGUI {
1110

1211
#if defined(MAX_PLATFORM_WINDOWS)

Code/maxGUI/FormConcept.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ namespace maxGUI
5353
auto* widget = T::Create(&window_, std::forward<Params>(params)...);
5454
auto control_ptr = std::make_unique<T>(std::move(widget));
5555
#endif
56+
control_ptr->NewSystemFont();
5657
T* raw_control_ptr = control_ptr.get();
5758
controls_.push_back(std::move(control_ptr));
5859
#if defined(MAX_PLATFORM_LINUX)

Code/maxGUI/FormContainer.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,15 @@ namespace maxGUI {
117117
}
118118
}
119119
return 0;
120+
case WM_SETTINGCHANGE:
121+
{
122+
// new font settings possible
123+
auto form = GetFormFromHWND(window_handle);
124+
for (auto& control : form->controls_) {
125+
control->NewSystemFont();
126+
}
127+
}
128+
return 0;
120129
}
121130

122131
auto form = GetFormFromHWND(window_handle);

Code/maxGUI/RadioButtonImplementation.cpp

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -40,26 +40,7 @@ namespace maxGUI
4040
}
4141
#pragma warning(pop)
4242
Win32String win32_text = Utf8ToWin32String(std::move(text));
43-
//return CreateWindowEx(0, TEXT("BUTTON"), win32_text.text_, win32_styles, rectangle.TopLeft.X(), rectangle.TopLeft.Y(), rectangle.Width, rectangle.Height, parent_window_handle, NULL, reinterpret_cast<HINSTANCE>(GetWindowLongPtr(parent_window_handle, GWLP_HINSTANCE)), NULL);
44-
HWND window_handle = CreateWindowEx(0, TEXT("BUTTON"), win32_text.text_, win32_styles, rectangle.TopLeft.X(), rectangle.TopLeft.Y(), rectangle.Width, rectangle.Height, parent_window_handle, NULL, reinterpret_cast<HINSTANCE>(GetWindowLongPtr(parent_window_handle, GWLP_HINSTANCE)), NULL);
45-
46-
static int count = 0;
47-
if (count == 0) {
48-
LOGFONT lf = {0};
49-
SystemParametersInfo(SPI_GETICONTITLELOGFONT, sizeof(lf), &lf, 0);
50-
HFONT font = CreateFontIndirect(&lf);
51-
SendMessage(window_handle, WM_SETFONT, reinterpret_cast<WPARAM>(font), TRUE);
52-
} else {
53-
//const int font_height_in_points = 14;
54-
//HDC screen_device_context = GetDC(NULL);
55-
//const int font_height_in_logical_units = -MulDiv(font_height_in_points, GetDeviceCaps(screen_device_context, LOGPIXELSY), 72);
56-
const int font_height_in_logical_units = -14;
57-
HFONT font = CreateFont(font_height_in_logical_units, 0, 0, 0, FW_DONTCARE, FALSE, FALSE, FALSE, ANSI_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, FF_DONTCARE | DEFAULT_PITCH, TEXT("Segoe UI Variable"));
58-
SendMessage(window_handle, WM_SETFONT, reinterpret_cast<WPARAM>(font), TRUE);
59-
}
60-
count++;
61-
62-
return window_handle;
43+
return CreateWindowEx(0, TEXT("BUTTON"), win32_text.text_, win32_styles, rectangle.TopLeft.X(), rectangle.TopLeft.Y(), rectangle.Width, rectangle.Height, parent_window_handle, NULL, reinterpret_cast<HINSTANCE>(GetWindowLongPtr(parent_window_handle, GWLP_HINSTANCE)), NULL);
6344
}
6445
#endif
6546

0 commit comments

Comments
 (0)