高 DPI モニターで見栄えを良くしたい Windows アプリケーションがあります。アプリケーションは多くの場所で DEFAULT_GUI_FONT を使用しており、この方法で作成されたフォントは正しくスケーリングされません。
あまり痛みを伴わずにこの問題を解決する簡単な方法はありますか?
構造体から、目的別に推奨されるフォントを取得できますNONCLIENTMETRICS
。
自動的に DPI スケーリングされたフォントの場合 (Windows 10 1607 以降、モニターごとの DPI 対応である必要があります):
// Your window's handle
HWND window;
// Get the DPI for which your window should scale to
UINT dpi = GetDpiForWindow(window);
// Obtain the recommended fonts, which are already correctly scaled for the current DPI
NONCLIENTMETRICSW non_client_metrics;
if (!SystemParametersInfoForDpi(SPI_GETNONCLIENTMETRICS, sizeof(non_client_metrics), &non_client_metrics, 0, dpi)
{
// Error handling
}
// Create an appropriate font(s)
HFONT message_font = CreateFontIndirectW(&non_client_metrics.lfMessageFont);
if (!message_font)
{
// Error handling
}
古いバージョンの Windows では、システム全体の DPI を使用してフォントを手動でスケーリングできます (Windows 7 以降、システム DPI に対応している必要があります)。
// Your window's handle
HWND window;
// Obtain the recommended fonts, which are already correctly scaled for the current DPI
NONCLIENTMETRICSW non_client_metrics;
if (!SystemParametersInfoW(SPI_GETNONCLIENTMETRICS, sizeof(non_client_metrics), &non_client_metrics, 0)
{
// Error handling
}
// Get the system-wide DPI
HDC hdc = GetDC(nullptr);
if (!hdc)
{
// Error handling
}
UINT dpi = GetDeviceCaps(hdc, LOGPIXELSY);
ReleaseDC(nullptr, hdc);
// Scale the font(s)
constexpr UINT font_size = 12;
non_client_metrics.lfMessageFont.lfHeight = -((font_size * dpi) / 72);
// Create the appropriate font(s)
HFONT message_font = CreateFontIndirectW(&non_client_metrics.lfMessageFont);
if (!message_font)
{
// Error handling
}
NONCLIENTMETRICS
には他にもたくさんのフォントがあります。目的に合わせて適切なものを選択してください。
最適な互換性を得るには、ここで説明されているように、アプリケーション マニフェストで DPI 認識レベルを設定する必要があります。