Click here to Skip to main content
15,884,298 members
Articles / Desktop Programming / WTL
Article

Fight the dialog units, DPI and Large Fonts

Rate me:
Please Sign up or sign in to vote.
4.95/5 (25 votes)
16 Nov 2003Zlib2 min read 246.1K   5.1K   44   51
Guarantees pixel-to-pixel matching appearance of resource-based dialogs for different font DPIs

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.

License

This article, along with any associated source code and files, is licensed under The zlib/libpng License


Written By
Web Developer
Austria Austria
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralRe: Oh no! Pin
Nic Wilson3-Apr-06 18:17
Nic Wilson3-Apr-06 18:17 
GeneralRe: Oh no! Pin
CodeSafe Hawk12-Mar-09 4:22
CodeSafe Hawk12-Mar-09 4:22 
GeneralMFC and IMAKEINTRESOURCE Pin
George Yohng10-May-05 2:47
George Yohng10-May-05 2:47 
GeneralRe: MFC and IMAKEINTRESOURCE Pin
bensabat10-May-05 4:27
bensabat10-May-05 4:27 
GeneralRe: MFC and IMAKEINTRESOURCE Pin
George Yohng15-May-05 3:41
George Yohng15-May-05 3:41 
GeneralMFC example Pin
Anonymous1-Feb-05 0:53
Anonymous1-Feb-05 0:53 
GeneralRe: MFC example Pin
George Yohng12-Feb-05 5:21
George Yohng12-Feb-05 5:21 
GeneralRe: MFC example Pin
bensabat10-May-05 2:28
bensabat10-May-05 2:28 
GeneralRe: MFC example Pin
George Yohng15-May-05 3:49
George Yohng15-May-05 3:49 
GeneralUse it in Windows98 Pin
hansworscht16-Jun-04 4:23
hansworscht16-Jun-04 4:23 
GeneralRe: Use it in Windows98 Pin
pma10-Sep-04 11:15
pma10-Sep-04 11:15 
GeneralProblem with ActiveX controls.. Pin
vallikumar30-Apr-04 0:57
vallikumar30-Apr-04 0:57 
Generalit's working only for main Dialog box in VC++6.0.. Pin
dingudikka@yahoo.com1-Apr-04 20:48
sussdingudikka@yahoo.com1-Apr-04 20:48 
GeneralRe: it's working only for main Dialog box in VC++6.0.. Pin
George Yohng1-Apr-04 23:13
George Yohng1-Apr-04 23:13 
GeneralRe: it's working only for main Dialog box in VC++6.0.. Pin
vallikumar2-Apr-04 2:03
vallikumar2-Apr-04 2:03 
GeneralRe: it's working only for main Dialog box in VC++6.0.. Pin
George Yohng5-Apr-04 11:20
George Yohng5-Apr-04 11:20 
GeneralRe: it's working only for main Dialog box in VC++6.0.. Pin
vallikumar7-Apr-04 19:09
vallikumar7-Apr-04 19:09 
GeneralCould not compile in VC++ Pin
KmAshif24-Mar-04 18:32
KmAshif24-Mar-04 18:32 
GeneralI could not compile in vc++(vc6) Pin
KmAshif24-Mar-04 18:30
KmAshif24-Mar-04 18:30 
GeneralRe: I could not compile in vc++(vc6) Pin
George Yohng25-Mar-04 1:41
George Yohng25-Mar-04 1:41 
GeneralDialog - Fullscreen Pin
pma1-Jan-04 23:48
pma1-Jan-04 23:48 
GeneralRe: Dialog - Fullscreen Pin
George Yohng2-Jan-04 1:04
George Yohng2-Jan-04 1:04 
GeneralRe: Dialog - Fullscreen Pin
pma3-Jan-04 11:35
pma3-Jan-04 11:35 
GeneralWhy don't have effect in windows98 Pin
chendana7-Dec-03 18:58
chendana7-Dec-03 18:58 
GeneralRe: Why don't have effect in windows98 Pin
George Yohng7-Dec-03 19:18
George Yohng7-Dec-03 19:18 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.