Changed an MFC app to per-monitor DPI aware. On the WM_DPICHANGED message I wanted to changed the fonts of the CMFCMenuBar to be scaled correctly. So I added:
LOGFONT logfont;
GetGlobalData()->fontRegular.GetLogFont(&logfont);
logfont.lfHeight=g_DPIHelper.ScaleNonClientMetricsFont(logfont.lfHeight);
if (!m_wndMenuBar.SetMenuFont(&logfont)) {
TRACE0("Unable to set menu font\n");
}
I confirmed that logfont.lfHeight went from -11 to -17 in the test case. I confirmed the SetMenuFont() call returned success. Yet the CMFCMenuBar font shown on the menu bar is the same size as it was before. What am I missing?
TIA!!
The problem is that after
WM_DPICHANGEDaWM_SETTINGCHANGEDis sent and MFC then callsAFX_GLOBAL_DATA::OnSettingsChange()which callsUpdateFonts()which usesGetNonClientMetrics()which resets it back to the wrong size because the size is based on when the application started.On
WM_DPICHANGEDcalculate your scaling factor. Then do the actual font change inWM_SETTINGCHANGEDwhich allows the font change size to take affect when you change the DPI of the screen (not sure ifWM_SETTINGCHANGEDcalled if moved to another window, if not the change can be done inWM_DPICHANGED. Here's one method: