Fight the dialog units, DPI and Large Fonts
Introduction
Resource-based dialogs, made with a dialog editor do have a potential problem of being scaled improperly for larger Windows font resolutions, no matter whether MFC, ATL, WTL or bare Win32 is used.
The given class allows to pin down and fix a particular DPI mode for a particular dialog resource. Furthermore, DPI resolution/scaling of a resource-based dialog can be dynamically changed in run-time.
Background
For example, static bitmaps on the dialogs normally do not get resized, thus if one would want to make some fancy bitmap background or matched illustration, one will obviously run into problems with "Large Fonts" Windows mode. This is especially useful for wizard-style and login dialogs.
Some of the users tend to have "Large size (120 DPI)" font mode set, which is problematic for the developers, as triple checks are to be made to find out whether dialogs designed look properly in 120 DPI mode.
If the program interface is mostly bitmap based, the best way is to lock the resolution down to 96 DPI and disallow any further dialog scaling. Well, unfortunately, Windows does not seem to have an easy way of turning off DPI-dependent dialog scaling and "dialog units". I've been looking through the network and so far found no easy solutions for the given problem.
Therefore, I have written a class. Once Attach
method is invoked in the WM_INITDIALOG
handler before the dialog is shown, the dialog will be resized and adjusted in run-time to match the specified resolution.
The supplied code contains a subroutine to re-parse of the dialog resource and to re-calculate DPI-related values such as control positions and sizes. Resolution in DPI is specified as the parameter to the Attach
method, and the standard Windows resolution is 96 DPI.
Using the code
The code is tested and functional within MFC, ATL/WTL and Win32 frameworks.
MFC Example:
... BOOL CMyDlg::OnInitDialog() { CDialog::OnInitDialog(); dpi.Attach(AfxFindResourceHandle(IMAKEINTRESOURCE(IDD), RT_DIALOG), m_hWnd,IDD,96.0); // 96 is the DPI // The rest of your initialization code goes here return TRUE; }
ATL/WTL Example:
... BOOL CMyDlg::OnInitDialog() { CDialog::OnInitDialog(); dpi.Attach(_AtlBaseModule.GetResourceInstance(),m_hWnd,IDD,96.0); // ^^^^ DPI // The rest of your initialization code goes here return TRUE; }
Caveats
I have looked for an easier way, but so far I have found none. The parser will only work with DIALOGEX
structures and will not work with obsolete DIALOG
structures.
Also you have to explicitly specify the dialog font. For proper sizing, you need to use Microsoft Sans Serif
or Tahoma
(and NOT MS Sans Serif
or MS Shell Dlg
). Tahoma
has exactly the same metrics as Microsoft Sans Serif
. You can use any other TrueType/OpenType font, avoid using bitmap fonts, as they will not scale well.
Due to obvious reasons, the size of a checkbox square is not affected, though it still will get proper placement and align.