Click here to Skip to main content
15,878,814 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 245.5K   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

 
GeneralTitle Bar Size is to big and border also big in MFC dialog Pin
Beniwal0130-Oct-17 21:36
Beniwal0130-Oct-17 21:36 
GeneralMy vote of 5 Pin
omindream6-Feb-13 22:05
omindream6-Feb-13 22:05 
QuestionWhy don't effect on multiline edit and checkbox? Pin
nhchmg21-Jul-11 23:10
nhchmg21-Jul-11 23:10 
GeneralHelp for use in Visual C Pin
symons6527-Jun-10 6:10
symons6527-Jun-10 6:10 
QuestionHow can use it in SDI or MDI type application? Pin
Le@rner26-May-09 23:53
Le@rner26-May-09 23:53 
Generalscaling factor for dialog resource [modified] Pin
athish12-Apr-07 2:23
athish12-Apr-07 2:23 
QuestionPropertySheet Tab title size is still too big Pin
Luckymmkay1-Dec-06 6:02
Luckymmkay1-Dec-06 6:02 
AnswerRe: PropertySheet Tab title size is still too big Pin
George Yohng1-Dec-06 6:37
George Yohng1-Dec-06 6:37 
GeneralRe: PropertySheet Tab title size is still too big Pin
Luckymmkay1-Dec-06 8:40
Luckymmkay1-Dec-06 8:40 
Questionan issue when using it in my dialogbar Pin
Albert19864-Apr-06 16:49
Albert19864-Apr-06 16:49 
AnswerRe: an issue when using it in my dialogbar Pin
Albert19866-Apr-06 16:01
Albert19866-Apr-06 16:01 
Generalthe issue using MSLU in windows 98/me Pin
ssliao21-Feb-06 20:50
ssliao21-Feb-06 20:50 
GeneralRe: the issue using MSLU in windows 98/me Pin
ssliao23-Feb-06 22:06
ssliao23-Feb-06 22:06 
GeneralOh no! Pin
Alexander Gräf26-Sep-05 0:46
Alexander Gräf26-Sep-05 0:46 
GeneralRe: Oh no! Pin
George Yohng26-Sep-05 0:55
George Yohng26-Sep-05 0:55 
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 
hehe, just go the other way around, dont use the class, instead, place an invisible static control on your images, and in oninitdialog, find out the size and location of the static, and realign/scale your images to this size.

btw: i faced and solved this problem back at 1999 when i developed a skinned application (and then assigning region to that window to make it non-rectangular (win9x doest support layered windows) ).
our support team stated, that people might have selected to use large fonts for a reason (and they are absolutely right), and it is not for you to judge wether their reasons are justified for your application or not (consider people with sight disabilities).
since i cared more about the visual aspect of the application (ie, a scaled image was out of the question) i decided to reject this support request.
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 

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.